First of all, what exactly is a Composition? Can someone explain the below code samples by breaking it into simplicity? I have just started C++ and I need help in understanding this.
main.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include "Birthday.h"
#include "People.h"
#include "Birthday.cpp"
#include "People.cpp"
usingnamespace std;
int main(){
Birthday birthObj(3, 8, 1988);
People buckyRoberts("Daniel the King", birthObj);
buckyRoberts.printInfo();
return 0;
};
birthday.h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
class Birthday
{
public:
Birthday(int m, int d, int y);
void printDate();
private:
int month;
int day;
int year;
};
#endif // BIRTHDAY_H
Birthday.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "Birthday.h"
#include "People.h"
#include <iostream>
usingnamespace std;
Birthday::Birthday(int m, int d, int y){
month = m;
day = d;
year = y;
}
void Birthday::printDate(){
cout << month << "/" << day << "/" << year << endl;
}
#ifndef PEOPLE_H
#define PEOPLE_H
#include <string> //include the String Class
#include "Birthday.h" // include Birthday Header file to store Birthday Object
usingnamespace std;
class People{
public:
People(string x, Birthday bo); // Passing the Constructor
void printInfo();
private:
string name;
Birthday dateofBirth; // Using object of another class
};
#endif // PEOPLE_H
people.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "People.h"
#include "Birthday.h"
#include <iostream>
usingnamespace std;
People::People(string x, Birthday bo) // Assign the name and Birthday to pricate variales.
// Working with a Class inside another Class. Therefore, we use a (MIL)
: name(x), dateofBirth(bo){ // Creating the Member Initializer List(MIL)
}
void People::printInfo(){
cout << name << " was born on ";
dateofBirth.printDate();
}