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
     

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses