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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Summer was born on :24.4.1992
0 Comments
If you have any doubt and suggestion Please let me know