C++ Program: Association in class Example :Write a code to associate Two class

 


The association is in a relationship between two classes. In this type of relationship, both classes know about the connection.

Following Example :

  • Make two-class Birthday and Person
  • Birthday class  object inside person class
#include<iostream>
#include<string>
using namespace std;
class Birthday // Date class
{
private:
int day;int month; int year;
public:
Birthday(int d,int m,int y)// constructor
{
day=d;
month=m;
year=y;
}
Birthday(){}//default constructor
void printD()
{
cout<<day<<"."<<month<<"."<<year<<endl;
}
};
class Person //Person class contain name
{
private:
string name;
Birthday DOB;
public:
Person(string n,Birthday b)
{
name=n;
DOB=b;
}
void print()
{
cout<<name<<" was born on :";
DOB.printD();//callig Birthday class function
}
};
int main()
{
Birthday d(24,04,1992);
Person s("Summer",d);
s.print();
return 0;
}
view raw .cc hosted with ❤ by GitHub
Summer was born on :24.4.1992   

0 Comments