public 继承

View Code
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class student
{
public:
student( ) { name = "tang"; num = "104080219"; sex = "M"; }
void display( );
void input( );
private:
string name;
string num;
string sex;
};

class student1: public student
{
public:
student1( ): student( ) { add = "HuNanyongzhou"; school = "HUT"; } // 派生类的构造函数
void display2( );
void input2( );
private:
string add;
string school;
};

void student1::display2( )
{
display( );
cout<<"地址:"<<add<<endl;
cout<<"学校:"<<school<<endl;

}
void student1::input2( )
{
input( );
cout<<"请输入地址:"<<endl;
cin>>add;
cout<<"请输入学校:"<<endl;
cin>>school;
}

void student::display( )
{
cout<<"该生信息如下:"<<endl;
cout<<"名字:"<<name<<endl;
cout<<"学号:"<<num<<endl;
cout<<"性别:"<<sex<<endl;
}

void student::input( )
{
cout<<"请输入名字:"<<endl;
cin>>name;
cout<<"请输入学号:"<<endl;
cin>>num;
cout<<"请输入性别:"<<endl;
cin>>sex;
}

int main( )
{
student p;
p.display( );
p.input( );
p.display( );
cout<<endl;
student1 q;
q.display2( );
cout<<endl;
q.input2( );
q.display2( );
system("pause");
return 0;
}

 

posted on 2012-03-19 15:25  more think, more gains  阅读(161)  评论(0编辑  收藏  举报

导航