Monday 5 March 2018

Member Functions In C++

Member functions are the functions which have their declaration inside the class definition. The definition of member functions can be inside or outside the definition of class.
If the member function is defined inside the class definition it can be defined directly, but if it is defined outside the class, then we have to use the scope resolution :: operator along with class name along with function name
1)member functions can be inside the definition class
Example:
class  Employee
{
     public:  
     int id;     //field or data member or instance variable     
     public:
    void display() // member functions
    {
   //statements
     }  
   };
Complete Example:

class Employee
{
     public:  
     int id;     //field or data member or instance variable     
     public:
    void display() // member functions definition
    {
 cout<<"Id is"<<id<<endl;
        }  
       };
   void main()
   {
 Employee e1;
  e1.id=102;
 e1.display();
  getch();
        }
        Output:
        Id is:102

2)member functions can be outside the definition class
Syntax:
  Return_type  class_name::function_name(parameter)
{
//statements
}
Where ::  is called as scope resolution operator 
    Example:
    class  Employee
   {
     public:  
     int id;     //field or data member or instance variable     
     public:
    void display(); //member function declaration
       };

  void  Employee::display()  //member function definition
   {
    cout<<"id is"<<id<<endl;
    }

Complete Example:
class  Employee
{
     public:  
     int id;     //field or data member or instance variable     
     public:
    void display();  //member function declaration
};


void  Employee::display() //member function definition
{
cout<<"Id is"<<id<<endl;
}
void main()
{
Employee e1;
e1.id=102;
e1.display();
 getch();
 }
Output:  
Id is :102




No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...