Defining Member Function in C++

The data member of a class is declared within the body of the class. However, the member functions can be defined in one of the two places.
  • Inside the class definition
  • Outside the class definition

No matter whether the function is defines inside or outside the class definition, the function performs the same operation. But the syntax of the member function definition is different if declared inside or outside. The program code written inside the body of member function is the same whether it is declared inside or outside.

  1. Inside the class definition:

The general syntax of the function definition inside class definition will be as:

class classname
{
    //……………..
    public:
        return_type member_function(args…)
        {
             //function body
        }
};

Following example illustrates the definition of function inside class definition:

Source Code:
//definition inside the class
#include<iostream>
using namespace std;
class student
{
    private:
       int roll;
       char name[20];
       char phone[10];
    public:
       void getdata()
       {
           cout<<"\nEnter Roll Number: ";
           cin>>roll;
           cout<<"Enter Name: ";
           cin>>name;
           cout<<"Enter Phone Number: ";
           cin>>phone;
       }
       void showdata()
       {
           cout<<"Name: "<<name<<endl;
           cout<<"Roll Number: "<<roll<<endl;
           cout<<"Phone Number: "<<phone<<endl;
       }
}; //end of class

int main()
{
    student s1, s2;
    s1.getdata();
    s2.getdata();
    cout<<"First Student"<<endl;
    s1.showdata();
    cout<<"Second Student"<<endl;
    s2.showdata();
    return 0;
}

Sample Run:
Enter Roll Number: 1
Enter Name: John
Enter Phone Number: 1234567890

Enter Roll Number: 2
Enter Name: Marrie
Enter Phone Number: 9876543210

First Student
Name: John
Roll Number: 1
Phone Number: 1234567890
Second Student
Name: Marrie
Roll Number: 2
Phone Number: 9876543210



     2. Outside the class definition:


The general syntax of the function definition outside class definition will be as:

class classname
{
    //……………..
    public:
        return_type member_function(args…);
        //…………….
};

Return_type class_name::member_function(args…)
{
    //function body
}

The declaration can be done in private or public section.
We have presented the same example presented above where member functions are defined outside the class specification.

Source Code:
//definition outside the class
#include<iostream>
using namespace std;
class student
{
    private:
        int roll;
        char name[20];
        char phone[10];
    public:
        void getdata(); //function declaration
        void showdata();
}; //end of class

void student::getdata()  //function definition
{
     cout<<"\nEnter Roll Number: ";
     cin>>roll;
     cout<<"Enter Name: ";
     cin>>name;
     cout<<"Enter Phone Number: ";
     cin>>phone;
}

void student::showdata()
{
     cout<<"Name: "<<name<<endl;
     cout<<"Roll Number: "<<roll<<endl;
     cout<<"Phone Number: "<<phone<<endl;
}

int main()
{
     student s1, s2;
     s1.getdata();
     s2.getdata();
     cout<<"First Student"<<endl;
     s1.showdata();
     cout<<"Second Student"<<endl;
     s2.showdata();
     return 0;
}

Sample Run:
Enter Roll Number: 1
Enter Name: John
Enter Phone Number: 1234567890

Enter Roll Number: 2
Enter Name: Marrie
Enter Phone Number: 9876543210

First Student
Name: John
Roll Number: 1
Phone Number: 1234567890
Second Student
Name: Marrie
Roll Number: 2
Phone Number: 9876543210



No comments:

Post a Comment