Member Functions of C++ Classes
In C++, Member Functions can be defined in two places:
- Outside the class definition
- Inside the class definition
Outside the class definition:
Member functions that are declared inside a class have to be defined separately outside the class. It’s like a normal function. It should have a function header and a function body.
The main difference between a member function and a normal function is that a member function incorporates a membership ‘identity lebel’ in the header. ‘lebel’ tells the compiler which class the function belongs to. It has the following Syntax:
return-type class-name::function-name(argument declaration) { Function body }
Inside the class definition:
Defining a member function is to replace the function declaration by the actual function definition inside the class.
Example:
class item { int number; float cost; public: void getdata(int a, float b) // declaration Inline function void putdata(void) // definition inside the class { cout<<number<<"\n"; cout<<cost<<"\n"; } };