Tuesday, 21 April 2015

Write a program that will demonstrate constructor function

Write a program that will demonstrate constructor function. The program that uses the following properties – class names- test; two integer type private member variables- e,f; one constructor function test(); one public member function add_display(). Constructor function test() assigns the values for e, f; add_display() member function do the addition for e, f and displays the result as output.

#include<iostream>
using namespace std;
class test
{
    int e,f;
public:
    test();
    void add_display();
};
test::test()
{
    cin>>e>>f;
}
void test::add_display()
    {
       cout<<"Addition is : "<<e+f<<endl;
    }
 int main()
 {
     test p;
     p.add_display();
     return 0;
 }

No comments:

Post a Comment