构造日期类,包括年、月、日等私有属性,构造函数和操作这些私有属性的公有函数以及将该对象转换成字符串的转换函数(就是能强制类型转换成string类,比如:有一个日期对象d=date(2019,3,26), 则string(d)就会返回一个字符串“2019年3月26日”);构造人员类,包括姓名,出生日期(复合了日期类的对象)等私有属性,构造函数和操作这些属性的公有函数;构造学生类,该类从人员类派生而来,包括学号和专业私有属性,构造函数和操作私有属性的公有函数。
时间: 2024-03-14 19:46:13 浏览: 9
以下是构造日期类的代码:
```cpp
#include <string>
#include <sstream>
class Date {
private:
int year_;
int month_;
int day_;
public:
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
int getYear() const { return year_; }
int getMonth() const { return month_; }
int getDay() const { return day_; }
std::string toString() const {
std::stringstream ss;
ss << year_ << "年" << month_ << "月" << day_ << "日";
return ss.str();
}
};
```
以下是构造人员类的代码:
```cpp
#include <string>
class Person {
private:
std::string name_;
Date birthdate_;
public:
Person(const std::string& name, const Date& birthdate) : name_(name), birthdate_(birthdate) {}
std::string getName() const { return name_; }
Date getBirthdate() const { return birthdate_; }
void setName(const std::string& name) { name_ = name; }
void setBirthdate(const Date& birthdate) { birthdate_ = birthdate; }
};
```
以下是构造学生类的代码:
```cpp
#include <string>
class Student : public Person {
private:
std::string id_;
std::string major_;
public:
Student(const std::string& name, const Date& birthdate, const std::string& id, const std::string& major)
: Person(name, birthdate), id_(id), major_(major) {}
std::string getId() const { return id_; }
std::string getMajor() const { return major_; }
void setId(const std::string& id) { id_ = id; }
void setMajor(const std::string& major) { major_ = major; }
};
```