Ticker

6/recent/ticker-posts

DEFINING MEMBER FUNCTIONS IN C++ PROGRAMMING

DEFINING MEMBER FUNCTIONS

Member functions can be defined in two ways. 

  1. Outside the class definition 
  2. Inside the class definition.

1) Outside the class definition. 

1) Member function that is declared inside a class has to be defined separately outside the class. These member functions associate a membership identify label in the header. This ëlabelí tells the compiler which class the function belongs to. The general format of a member function definition is. 

Return type class name:: function-name(argument declaration) 
    Function body 

2) The membership label class-name:: tells the compiler that the function function-name belongs to the class-name. The symbol :: is called as scope resolution operator. 

3) Ex- the function getdata is coded as 

void item:: getdata(int a, float b) 

        number = a; 
        cost = b; 

4) The member function have some special characteristics :- 

  • Several different classes can use the same function name the ëmembership labelí will resolve their scope. 
  • Member function can access the private data of the class. A non member function cannot do so.
  • A member function can call another member function directly, without using the dot operator.

2) Inside the class definition. 

In this method the function declaration inside the class is replaced by actual function definition. 

For Ex-
            class item 
            
                    int number; 
                    float cost; 
            pubic :
                    void getdata (int a float b); //declaration 
                    void putdata (void) 
                    
                            cout << number << endl; 
                            cout << cost << endl; 
                    
            

Remember only small functions can be defined inside the class.

ALSO SEARCH:

"types of member functions in C++"

"what is member function in C++"

"define member function"

"overloading member function in C++"

"member function example"

"defining member function inside and outside the class in C++"

"types of member functions in C++"

"defining member function inside and outside the class in C++"

"defining member function outside the class in c++"

"what is member function in C++"

"member function example"

"member function inside and outside the class example"