定义两个类,一个人,一个学生。人为父类,学生为子类
#include <iostream>
class Person{
protected:
std::string name;
int age;
int height;
int weight;
public:
Person(){
this->name = "";
this->age = 0;
this->height = 0;
this->weight = 0;
}
Person(std::string name, int age, int height, int weight):name(name),age(age),height(height),weight(weight){
}
std::string toString(){
return "name: " + this->name + ",age: " + std::to_string(this->age) + ",height: " + \
std::to_string(this->height) + ",weight: " + std::to_string(this->weight);
}
};
class Student : public Person{
private:
int score;
public:
Student(){
this->score = 0;
}
Student(std::string name, int age, int height, int weight, int score):Person(name,age,height,weight),score(score){
}
std::string toString(){
return this->Person::toString() + ",score: " + std::to_string(this->score);
}
};