c/c++课的作业合集,都是很简单的文件。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cpp-work/archive/20220310.cpp

103 lines
2.0 KiB

3 years ago
#include "iostream"
using namespace std;
class Date {
public:
Date();
Date(int year, int month, int day);
Date(const Date &date);
~Date();
int getYear() const;
int getMonth() const;
int getDay() const;
private:
int year = 1970;
int month = 1;
int day = 1;
};
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
std::cout << "Date::Date(int year, int month, int day)" << std::endl;
}
Date::Date() : year(1970), month(1), day(1) {}
Date::Date(const Date &date) {
cout << "Date::Date(const Date &p)" << endl;
Date::year = date.year;
Date::month = date.month;
Date::day = date.day;
}
Date::~Date() {
std::cout << "Date::~Date()" << std::endl;
}
int Date::getYear() const {
return year;
}
int Date::getMonth() const {
return month;
}
int Date::getDay() const {
return day;
}
//-------------------------------------------------
class Person {
public:
Person();
Person(char *name, const Date &birthday);
~Person();
void showPerson();
private:
string name;
Date birthday;
};
Person::Person() {
cout << "Person::Default constructor Function is called." << endl;
}
Person::Person(char *name, const Date &birthday) : name(name), birthday(birthday) {
cout << "Person::Constructor Function is called." << endl;
}
Person::~Person() {
std::cout << "Person::~Person()" << std::endl;
}
void Person::showPerson() {
cout << "Person::showPerson()" << endl;
cout << "Name:" << name << " ";
cout << "Birthday:" << birthday.getYear() << '-' << birthday.getMonth() << '-' << birthday.getDay();
cout << endl;
}
int main() {
char name[20];
int y, m, d;
int Cas = 0;
cout << "Please input name & birthday:\n";
while (cin >> name >> y >> m >> d) {
Cas++;
cout << "CASE #" << Cas << ":" << endl;
Date d1(y, m, d);
Person person(name, d1);
person.showPerson();
}
}