Thursday, 29 March 2018

Defining Member Function

The Member function of the class can be defined in any one of the two places
 1. Inside the class definition
 2. Outside the class definition

Inside the class definition :
  Here actual function definition is included within class definition:

  Ex:
     Class class_name
  {
       Data Type datamember_1;
       Data Type datamember_2;
        .
        . 
        public:
        Return_type functionname_1(Actual Argument)
         {
          //Code of Function
         }
          .
          .
          .
          Return_type functionname_n(Actual Argument)
           {
             //Code of function
            }
     };

When a function defined inside the class, it is treated as an inline function.

Outside The class Definition :
    This is another way of defining member function. Here the member function declaration and definition is written separately i.e. function declaration is taken place inside the class and function definition is taken place outside the class.

Function definition outside the class uses scope resolution operator (::) which is used to tell the compiler the scope of the member function or tells that which function belongs to which class .


Syntax :

  class class_name
  {
     Datatype datamember_1;
     Datatype datamember_2;
     .
     .
     .
    Datatype datamember_n;
    public :
    returntype function (Argument list);    //Function Declaration
    .
    .
    .
};

returntype class_name :: function_name (Argument_list)
 {
   //Body of the Function
  }

Ex : Function Definition inside class
       
 WAP in cpp to create a class having name 'student' having members roll_no, name, age and display it on screen.

#include<iostream.h>
#include<conio.h>
class student
{
 private:
 int roll_no;
 char name[10];
 int age;
 public:
 void getdata( )
 {
  cout<<"\n\tEnter Roll no";
  cin>>roll_no;
  cout<<"\n\tEnter Name";
  cin>>name;
  cout<<"\n\tEnter Age";
  cin>>age;
  }
 void display( )
  {
    cout<<"\n\tRoll No is --->"<<roll_no;
    cout<<"\n\tName is --->"<<name;
    cout<<"\n\tAge is--->"<<age;
   }
};
void main( )
{
 student s1;
 clrscr( );
 s1.getdata( );
 s1.display( );
 getch( );
}

Output :-
 
Enter Roll no101  
  

  Enter NameIrawen
   
  Enter Age8

   Roll No is --->101
   Name is --->Irawen
   Age is--->8
      

Ex: Function definition outside class
     
#include<iostream.h>
#include<conio.h>
class student
{
 private:
 int roll_no;
 char name[10];
 int age;
 public:
 void getdata( );
 void display( );
};
  void student::getdata( )
 {
  cout<<"\n\tEnter Roll no";

  cin>>roll_no;
  cout<<"\n\tEnter Name";
  cin>>name;
  cout<<"\n\tEnter Age";
  cin>>age;
  }
  void student::display( ) 
{

    cout<<"\n\tRoll No is --->"<<roll_no;
    cout<<"\n\tName is --->"<<name;
    cout<<"\n\tAge is--->"<<age;
   }
};
void main( )
{
 student s1;
 clrscr( );
 s1.getdata( );
 s1.display( );
 getch( );
}


Output :-
  Enter Roll no101  
  

  Enter NameIrawen
   
  Enter Age8

   Roll No is --->101
   Name is --->Irawen
   Age is--->8
     

1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)