1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Function Chaining in C++ (Call more than one function in a sequence )

Discussion in 'C++' started by Shishir191, Jul 27, 2007.

  1. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    1) This Simple program explain one of the use of this pointer in C++.
    2) With the help of "this" pointer you can call as many as functions in a sequence.
    3) This procedure is called function chaining
    4) In this program i have used three different function and through main i have called all these function in a sequence with the help of this pointer
    Code:
    #include<iostream.h>
    #include<conio.h>
    
    class Test
    {
    	
    public:
    	
    	Test Display(); // Function 1
    	Test Show();    // Function 2
    	Test Result();  // Function 3
    	
    };
    
    Test Test::Display()
    {
    	cout<<"Function 1"<<endl;
    	return *this;  // Return this pointer
    }
    
    Test Test::Show()
    {
    	cout<<"Function 2"<<endl;
    	return *this;
    }
    
    Test Test::Result()
    {
    	cout<<"Result 3"<<endl;
    	return *this;
    }
    
    void main()
    {
    	Test Obj;
    	
    	//Here i have called all the three function in a sequence.
    	
    	Obj.Display().Show().Result(); //This is called Function Chaining
    	
    	getch();
    }
    
     
  2. oleber

    oleber New Member

    Joined:
    Apr 23, 2007
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer (Perl, C/C++ and Java)
    Location:
    Hamburg, Germany
    Home Page:
    You need to return a reference :eek:, otherwise you are doing a copy of the object :confused:.
    Try to add the method:

    Code:
    Test::Test(Test& test) {
        cout<<"Test::Test(Test& test)\n";
    }
    
    Better shall be: :cool:

    Code:
    #include <iostream>
    using namespace std;Correct should be:
    class Test
    {
        
    public:
        Test() {};
        Test(Test& test);
        Test& Display(); // Function 1
        Test& Show();    // Function 2
        Test& Result();  // Function 3
    };
    
    Test::Test(Test& test) {
        cout<<"Test::Test(Test& test)\n";
    }
    
    Test& Test::Display()
    {
        cout<<"Function 1"<<endl;
        return *this;  // Return this pointer
    }
    
    Test& Test::Show()
    {
        cout<<"Function 2"<<endl;
        return *this;
    }
    
    Test& Test::Result()
    {
        cout<<"Result 3"<<endl;
        return *this;
    }
    
    int main()
    {
        Test Obj;
        
        //Here i have called all the three function in a sequence.    
        Obj.Display().Show().Result(); //This is called Function Chaining
    }
     
     
  3. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Yes, You are right. Thanks for your suggestion and reply.
     

Share This Page