program for Object Composition and Delegation
Write a program in program notebook
#include<iostream.h>
#include<conio.h>
class fclass
{
public:
int data;
fclass() // here is constructor declared
{
data = 0; // Data member is initialized
}
fclass(int a) // here is constructor again declared
{
cout << "Constructor of Class fclass is calling " << endl;
data = a; // Data member is initialized
}
};
class sclass
{
private:
int b;
fclass fobject; //here fobject is object for fclass
public:
sclass(int a) : fobject(a) // Constructor of class sclass
{
b = a; // initialized
}
void pdata() // here pdata function is used to print value
{
cout<<"Data of class SClass object is: "<<b<<endl;
cout<<"Data of class AClass object in class SClass is:"<<fobject.data;
}
};
int main()
{
sclass s(99); // Here s is created for sclass
s.pdata(); //calling pdata member function of seconed class
getch();
return 0;
}
Output:
Comments
Post a Comment