#include <iostream>
#include <cstring>
using namespace std;
class Person{
public:
Person(char* s){
strcpy(name,s);
}
void display( ){
cout<<"Name: "<<name<<endl;
}
private:
char name [20];
};
class Student:public Person//(1)
{
public:
Student(char* s, int g):Person(s)// (2)
{grade=g;}
void display1( ) {
display(); // (3)
cout<<"Grade: "<<grade<<endl;
}
private:
int grade;
};
int main( )
{
Student s("春哥",19);
s.display1(); // (4)
return 0;
}
是春哥啊
最新推荐文章于 2016-06-13 15:24:09 发布
本文介绍了一个 C++ 程序示例,该示例演示了如何使用类继承和多态来创建一个基类 Person 和一个派生类 Student。Person 类包含姓名属性,而 Student 类继承了 Person 类并添加了年级属性。示例展示了如何在构造函数中初始化基类,并在派生类的方法中调用基类的方法。
2502

被折叠的 条评论
为什么被折叠?



