Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

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



Function Overloading in C++

In a program there can be many functions with different name. However, some functions conceptually perform the same task on objects of different types and numbers. In such a case it is convenient to give them same name. When the same name is used for different operation, it is called function overloading. When an overloaded function is called the function with matched arguments is invoked.
Examples of overloaded functions are as follows:
void display(); //function with no arguments
void display(int); //function with one int argument
void display(float); //function with one float argument
void display(int, float) //function with one int and
                         //one float argument

A Complete Example to demonstrate the function overloading is as follows:

Source Code:
//function overloading
#include<iostream>
using namespace std;
int max(int, int);
long max(long, long);
float max(float, float);
char max(char, char);

int main()
{
int i1 = 12, i2 = 22;
cout<<"Greater is "<<max(i1, i2)<<endl;
long l1 = 400000, l2 = 380000;
cout<<"Greater is "<<max(l1, l2)<<endl;
float f1 = 34.04, f2 = 54.455;
cout<<"Greater is "<<max(f1, f2)<<endl;
char c1 = 'f', c2 = 'F';
cout<<"Greater is "<<max(c1, c2)<<endl;
return 0;
}

int max(int i1, int i2)
{
return(i1 > i2 ? i1 : i2);
}

long max(long l1, long l2)
{
return(l1 > l2 ? l1 : l2);
}

float max(float f1, float f2)
{
return(f1 > f2 ? f1 : f2);
}

char max(char c1, char c2)
{
return(c1 > c2 ? c1 : c2);
}

Sample Run
Greater is 22
Greater is 400000
Greater is 54.455
Greater is f