Just as a function takes an object as its argument, it can also return an object. The following program illustrates how objects are returned. The program declares a class integer representing an integer variable x and define a function to calculate the sum of two integer values. This function finally return an object which store the sum in its data member x.

#include<iostream.h>
#include<conio.h>

class integer
{
   int x;

public:
   void getdata(int x1)
    
    {
          x = x1;
    }
       
       void disp()
      {
         cout << x;
      }   
   
        integer sum(integer int2)
       {
          integer int3;

          int3, x = x + int2.x;

          return(int3);
       
       }


};
  

main()

{
   integer int1, int2, int3;

   int1.getdata(15);

   int2.getdata(25);

  cout << "\nthe value of x for object int1";

  int1.disp();

  cout << "\nthe value of x for object int2";

  int2.disp();

  cout << "\nthe sum of private data values of x belonging to object int1 and        
  int2 is";

  int3 = int1.sum(int2);

  int3.disp();

  getche();

}

You should see  the following output from the program.

  The value of x for the object int1  15


  The value of x for the object int2  25
   
  The sum of private data values of x belonging to object int1 int2 is 40



















           


The following program illustrates the use of array of abject to store information of student.The program declared class student having  data members rollno age height and weight,and two function  to get and display this information.Since information of more than one student is stored, an array of object that is std[Max] is creadted

// array of calss objects

#include <iostream.h>
#include<conio.h>

#define.max 30

class student
{
  private:
             int rollno;
             int age;
             float height, weight;
 public:
            void grtinfo()
{
           cout<<"roll no: ";
           cin>> rollno;
           cout<<"age: ";
           cin>>age;
           cout<<"Height:";
           cin>>height;
           cout<<"Weight:";
           cin>>weight;
}
void disinfo()
{
           cout<<endl;
           cout<<"Roll no="<<rollno<< endl;
           cout<<"age="<<age<<endl;
           cout<<"Height="<<Height<<endl;
           cout<<"Weight="<<Weight<<endl;
}
};

void main()
{
           student std[max];  // array of object having max=30
           int i,n;
           cout<<"How many students?\n"<<endl;
           cin>>n;
           cout<<"enter the student details \n"<<endl;
           for(i=0;i<n;i++)
{
          cout<<endl;
          std[i].getinfo();
}
cout<<"The list of student's is as follow \n";
for(i=0; i<n; i++);
          std[i].disinfo();
               
getche();
}


You should get the result

How many student?
3
roll no:1
age :18
Height:134
Weight:45


roll no:4
age :20
Height:143
Weight:46



roll no:27
age :20
Height:147
Weight:50

The list of student's is as follows

roll no:1
age :18
Height:134
Weight:45


roll no:4
age :20
Height:143
Weight:46


roll no:27
age :20
Height:147
Weight:50















































Let us now have a look  at a C++ Program with a class. The following program show how the member function getdata () is defined within the class. The calss data in the program represented the day,month and year of the date. The function written to initialized the same is defined  within the class.

#include<iostream.h>

#include<conio.h>

class data

{
  private:
              int day, mth, yr;

  public:
             void getdata(int dl, int ml, int yl)
           
{
         day=dl;
         mth=ml;
         yr=yl;
}
    void disp(void)
{
    cout<<"Today's date is "<< day<<"/"<<mth<<"/"<<yr<<"\n"
}
};


void main()

{
  class date today;
 
    today.getdata(15,6.2017);
 
     today.disp();

     getche();
}

















#include<iostream.h>

#include<conio.h>

class name

{
    private:
                 char name[10];
    public:
                 void disp()


{
      cout<<"Enter your name:\n";
      cin>>name;
      cout<<"your name is<<name;"
}

};

void main()
{
       
         clrscr();
        name obj;
        obj.disp();
}



Like any other data type argument, object can also be passed to a function. As you know argument are passed to a function in tow ways.

* By value 
* By reference 

The following Program illustrates the calling of function by value. The program declares a class integer representing a integer variable x. Only the value of the object are passed to the function written to swap them.

# include <iostream.h>
#include <conio.h>

class integer

{
   int x;

    public

   void get data()

{
   cout << "Enter a value for x";
   cin<< x;
}

void disp()
{
   cout<< x;
}

void swap (integer a1 ,  integer a2)
{
   int temp;
   temp =  a2.x;
   a2 .x=al.x;
   al.x = temp
}
};

main();

{
   integer int1, int2;

   int1.getdata();
   
   int2.getdata();

  cout <<"\nthe value of x belonging to object int1 is ";
  int1.disp();

  cout <<"\nthe value of  x belonging to object int2 is";
  int2.disp();

  integer  int3;

  int3.swap (int1, int2); // int3 is just used to invoke the function

  cout <<"\nafter swaping";

  cout <<"\nthe vaue of x belonging to object inti  is ";
  int1.disp();

  cout <<\"nthe value of x belonging to object int2 is ";
  int2.disp();

  cout<< "\n";

 getche();

}


You should see the value following output 

 Enter a value for x 15

 Enter a value for x 50

 the value of x belonging  to object int1 is 15

 the value of x belonging  to object int1 is 15

 after awapping

 the value of x belonging to object int 1 is 50

 the value of x belonging to object int 1 is 50


































A static member function is not a part of object of class . It is instance dependent and can be accessed directly by using the class name and scope resolution operator . If it is declared and defined in a class,the keyword static should be used only in declaration part .

The following program demonstrates the use of static member which accesses static data. The data member count and the function disp() are declared static in the class counter.But the variable count is initialized to 0 outside the class.

#include <iostream.h>

#include <conio.h>

Class counter

{
Private:
static int count;

Public:
counter()

{
   ++count
}

static void disp();
}

int counter::count=0;
void counter::disp()

{
   cout << count  <<  "\n";
}

main ()

{
  cout << "Number of object created  before =";

  counter:: disp();             // calling function disp() belonging  to class counter

   counter cntl,  cnt2,  cnt3,  cnt4,  cnt5;

   cout  <<"Number of subject created recently = ";

   counter::disp();
 
    getche();

}

  you should get the following output on running the program.

  Number of objects created before =0

  Number of object created recently =5



































Consider the following program which demonstrates the use of static data member count. 
The variable count is declared static in the class but initialized to 0 out side the class .


#include <iostream.h>

#include <conio.h>

Class counter
{
private:

static int count;
public:
          void disp ();
};

int counter::count=0;

void counter::disp()

{
count++

cout<< "The present value of count is" <<count<<"/n";

}

Main()

{
counter cntl;

for (int i=0; i<5; i++)

cnt1.disp();

getche();

}

you should get the following output from this program.

The present value of count is 1

The present value of count is 2

The present value of count is 3

The present value of count is 4

The present value of count is 5