64,654
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<string>
using namespace std;
class Person{
private:
public:
Person(int age){
cout<<"Tom"<<endl;
}
virtual void Say(){
cout<<"Hello!I'm a person."<<endl;
}
};
class Teacher:public Person{
public:
Teacher(int age){
}
void Say(){
cout<<"Hello!I'm a teacher."<<endl;
}
};
int main(){
Person p(10); //出错
return 0;
}
class Person{
private:
public:
Person(int age){
cout<<"Tom"<<endl;
}
virtual void Say(){
cout<<"Hello!I'm a person."<<endl;
}
};
class Teacher:public Person{
public:
Teacher(int age):Person(age){ //子类显式调用一下基类的构造
}
void Say(){
cout<<"Hello!I'm a teacher."<<endl;
}
};
int main(){
Person p(10); //出错
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Person{
private:
public:
Person(int age){
cout<<"Tom"<<endl;
}
virtual void Say(){
cout<<"Hello!I'm a person."<<endl;
}
};
class Teacher:public Person{
public:
Teacher(int age):Person(age){
}
void Say(){
cout<<"Hello!I'm a teacher."<<endl;
}
};
int main(){
Person p(10);
system("pause");
return 0;
}