美文网首页
47 - Composition Part 2

47 - Composition Part 2

作者: 社交帐号直接注册 | 来源:发表于2018-01-02 20:18 被阅读0次
1. main.cpp
#include <iostream>
#include "People.h"
#include "Birthday.h"
using namespace std;

int main()
{
    Birthday bithObj(12, 28, 198);
    People buckyRoberts("Bucky the King", bithObj);
    buckyRoberts.printInfo();
    system("pause");
}
2. Birthday.h
#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
3. Birthday.cpp
#include "Birthday.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;
}
4. People.h
#ifndef PEOPLE_H
#define PEOPLE_H

#include <string>
#include "Birthday.h"
using namespace std;

class People
{
public:
    People(string x,Birthday bo);
    void printInfo();
private:
    string name;
    Birthday dateOfBirth;
};

#endif
5. People.cpp
#include "People.h"
#include "Birthday.h"
#include <iostream>
using namespace std;

People::People(string x, Birthday bo):name(x),dateOfBirth(bo)
{

}

void People::printInfo()
{
    cout << name << "  was born on  ";
    dateOfBirth.printDate();
}

相关文章

网友评论

      本文标题:47 - Composition Part 2

      本文链接:https://www.haomeiwen.com/subject/zkbqnxtx.html