All about programming in GNU/LINUX

Simple C++ Code clearly illustrating flow of control when Object is passed to a function and constructors and distructors are used …

//Tested in GCC and Code BLocks COmpilers
#include <iostream>
using namespace std;
 class r
{
    int i;
    public :
     r(int n);
     ~r();
     void seti(int n)
     {
         i=n;
     }
     int geti()
     {
         return i;
     }

};
r::r(int n)
{
    i=n;
    cout <<"\nconstructor in action : "<<i <<"\n";
}
r::~r()
{
    cout << " \ndestructor in action: " << i << " \n ";
}
int pass(r s );//This is the function to which object of a function can be passed as parameter
void another();
void first();
void second();
int main()
{
    cout <<"\nStarting point main";
    r k(3);
    cout<<"\n"<<pass(k)<<"\t";
    another();
    cout <<"\n end of  main : " <<k.geti() << "\n " ;
    return 0;
}
int pass(r s )//This is the function to which object of a function can be passed as parameter

{
    cout <<"\nStarting point of pass function ";
    s.seti(222);
    cout << " \n this end of pass function i: "<<s.geti();
    return s.geti();

}
void another ()
{
    cout <<"\nConstructor call from another";
    r ls(77);
    cout <<"\nCalling pass from another";
    pass(ls);
    void second();
    cout<<"\nBack in another ";
}
void second()
{
    cout <<"\nInside second";
    void first();
    cout <<"back in second ";
}
void first()
{
    cout <<"\nConstructor call from first";
    r fir(30);
    cout<<"\nPass call from first";
    pass(fir);
    cout<<"\nBack in first";
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s