问题及代码:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:project.cpp
*作 者:陈文青
*完成日期:2015年5月21日
*版 本 号:v1.0
*
*问题描述:请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:
Name: 春哥
Grade: 19
*程序输入:
*程序输出:
*/
#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;
}
运行结果:
知识点总结:
1.继承时,定义为 class 派生类:继承方式 基类
2.派生类构造函数要先调用基类的构造函数
3.派生类可以调用基类中的数据成员和成员函数
591

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



