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

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (23) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

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