Composition

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"

using namespace 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>
using namespace std;

Birthday::Birthday(int m, int d, int y){
    month = m;
    day = d;
    year = y;
}

void Birthday::printDate(){
    cout << month << "/" << day << "/" << year << endl;
}




People.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#ifndef PEOPLE_H
#define PEOPLE_H

#include <string> //include the String Class
#include "Birthday.h" // include Birthday Header file to store Birthday Object
using namespace 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>
using namespace 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();
}

What exactly is it that you don't understand? Because if you don't understand any of it, maybe you should start on a simpler level.
closed account (LN7oGNh0)
Here is a good tutorial on this:
http://www.learncpp.com/cpp-tutorial/102-composition/
Topic archived. No new replies allowed.