main.cpp
#include <iostream>
#include "Person.h"
using namespace std;
int main()
{
Person myPerson;
// Person myPerson("S.M.Wang", 070145, "莲花路200号");
cout << "请输入姓名:" ;
string name;
cin >> name;
cout << "请输入ID:" ;
int id;
cin >> id;
cout << "请输入住址:" ;
string address;
cin >> address;
Date myDate;
cout << "请输入生日年:" ;
int year;
cin >> year;
cout << "请输入生日月:" ;
int month;
cin >> month;
cout << "请输入生日天:" ;
int day;
cin >> day;
myPerson.setName(name);
myPerson.setID(id);
myPerson.setAddress(address);
myDate.setYear(year);
myDate.setMonth(month);
myDate.setDay(day);
myPerson.setBirthday(myDate); // 调用的形参为类
myPerson.print();
return 0;
}
Person.h
#include <iostream>
#include "Date.h"
using namespace std;
class Person
{
public:
Person();
Person(string name, int id, string address);
~Person();
void setPerson(string name, int id, string address);
void setName(string name);
void setID(int id);
void setAddress(string address);
void setBirthday(Date d);
string getName();
int getID();
string getAddress();
Date getBirthday();
void print(); // outPutResult
private:
string Name;
int ID;
string Address;
Date Birthday;
};
Person.cpp
#include "Person.h"
#include <iostream>
using namespace std;
Person::Person()
{
Name = "S.M.Wang";
ID = 070145;
Address = "莲花路200号";
}
Person::Person(string name, int id, string address)
{
setPerson(name, id, address);
}
Person::~Person()
{
//cout << "object Destructor is called" << endl;
}
void Person::setPerson(string name, int id, string address)
{
Name = name;
ID = id;
Address = address;
}
void Person::setName(string name)
{
Name = name;
}
void Person::setID(int id)
{
ID = id;
}
void Person::setAddress(string address)
{
Address = address;
}
string Person::getName()
{
return Name;
}
int Person::getID()
{
return ID;
}
string Person::getAddress()
{
return Address;
}
void Person::setBirthday(Date d) // 调用的形参是类
{
Birthday = d;
}
Date Person::getBirthday() // 返回的是类
{
return Birthday;
}
void Person::print()
{
cout << "Name:" << getName() << endl;
cout << "ID:" << getID() << endl;
cout << "Address:" << getAddress() << endl;
cout << "Birthday:" << getBirthday().getYear(); // getBirthday()返回的是类,再调用类中的子函数满足cout的返回值。
cout << " " << getBirthday().getMonth();
cout << " " << getBirthday().getDay() << endl;
}
Date.h
#include <iostream>
using namespace std;
class Date
{
friend class Person;
public:
Date();
Date(int y, int m, int d);
~Date();
void setYear(int y);
void setMonth(int m);
void setDay(int d);
int getYear();
int getMonth();
int getDay();
void print();
private:
int Year;
int Month;
int Day;
};
Date.cpp
#include "Date.h"
#include <iostream>
using namespace std;
Date::Date()
{
Year = 0;
Month = 0;
Day = 0;
}
Date::Date(int year, int month, int day)
{
setYear(year);
setMonth(month);
setDay(day);
}
Date::~Date()
{
//cout << "object Destructor is called" << endl;
}
void Date::setYear(int year)
{
Year = year;
}
void Date::setMonth(int month)
{
Month = month;
}
void Date::setDay(int day)
{
Day = day;
}
int Date::getYear()
{
return Year;
}
int Date::getMonth()
{
return Month;
}
int Date::getDay()
{
return Day;
}
void Date::print()
{
cout << "Year :" << getYear() << endl;
cout << "Month :" << getMonth() << endl;
cout << "Day :" << getDay() << endl;
}
Result: