object composition and delegation (examples)

Write after object composition  

For Example : Write a program to demonstrate the use object composition.

#include<iostream.h>

#include<conio.h>

class first

{

private:

 int a;

public:

void setValue(int b)

{

a = b;

}

 

void getSum(int c)

{

cout<<" Addition of  "<<a<<" and "<<c<<" = "<<(a + c)<<endl;

}

};

 

class second

{

public:

first x; //here x is object of first class and it is composition

void showResult()

{

x.getSum(10);

}

};

 

void main()

{

second s;

s.x.setValue(10);

s.x.getSum(90);

s.showResult();

getch();

}

Output :


Write after object delegation  

For Example : Write a program to demonstrate the use object delegation

#include<iostream.h>

#include<conio.h>

class first

{

public:

 

void display()

{

cout << "The First  Class is calling";

}

};

 

class second

{

first f;  // Here object of first class is created

public:

void display()

{

f.display();

}

};

int main()

{

second s; // Object of class second is created

s.display(); //display is calling

getch();

return 0;

}

output:






Comments

Popular posts from this blog

Multiple Assignment and Data types

Pointer to the derived class and base class

Virtual Base Classes..