2023年8月5日,周六上午
在C++中,结构体可以继承另一个类
#include<iostream>
class People{
public:
std::string name;
int age;
void showName(){
std::cout<<"Name:"<<name<<std::endl;
}
void showAge(){
std::cout<<"Age:"<<age<<std::endl;
}
};
struct student : public People{
std::string id;
void showId(){
std::cout<<"Id:"<<id<<std::endl;
}
};
int main(){
student XiaoMing;
XiaoMing.age=20;
XiaoMing.id="202301001";
XiaoMing.name="小明";
XiaoMing.showName();
XiaoMing.showAge();
XiaoMing.showId();
}