【C++程序设计实验】类的继承和派生

2025-10-01 04:19:56

1、编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。

2、要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。

3、编写一个程序计算出球、圆柱和圆锥的表面积和体积。

 

要求:

 

i. 定义一个基类圆,至少含有一个数据成员半径;

 

ii. 定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。

 

iii. 定义主函数,求球、圆柱、圆锥的和体积。

1、#include <iostream>

 

using namespace std;

 

class person

 

{public:

 

void set_information( ); //公用成员函数

 

void show_information( ); //公用成员函数

 

private: //数据成员为私有

 

int number;

 

char name[30];

 

};

 

class Student: public person

 

{

 

public:

 

void show_information_1( )

 

{

 

cout<<endl;

 

cout<<"请输入学生信息: "<<endl;

 

cout<<endl;

 

cout<<"请输入班级: "<<endl;

 

cin>>cls;

 

cout<<"请输入成绩: "<<endl;

 

cin>>gra;

 

cout<<endl;

 

cout<<"学生附加信息:"<<endl;

 

cout<<" 班级: "<<cls<<endl; //引用派生类的私有成员,正确

 

cout<<" 成绩: "<<gra<<endl;

 

cout<<endl;

 

} //引用派生类的私有成员,正确

 

private:

 

int cls;

 

int gra;

 

};

 

class Teacher: public person

 

{

 

public:

 

void show_information_2( )

 

{

 

cout<<endl;

 

cout<<"请输入教师信息: "<<endl;

 

cout<<endl;

 

cout<<"请输入职称: "<<endl;

 

cin>>cls2;

 

cout<<"请输入部门: "<<endl;

 

cin>>gra2;

 

cout<<endl;

 

cout<<"教师附加信息:"<<endl;

 

cout<<" 职称: "<<cls2<<endl; //引用派生类的私有成员,正确

 

cout<<" 部门: "<<gra2<<endl;

 

cout<<endl;

 

} //引用派生类的私有成员,正确

 

private:

 

char cls2[30];

 

char gra2[30];

 

};

 

int main( )

 

{

 

Student t1;

 

Teacher t2; //定义对象t1

 

t1.set_information( ); //调用对象t1的成员函数set_time,向t1的数据成员输入数据

 

t1.show_information_1();

 

t2.set_information( ); //调用对象t1的成员函数set_time,向t1的数据成员输入数据

 

t2.show_information_2();

 

cout<<endl;

 

return 0;

 

}

 

void person::set_information( ) //在类外定义set_time函数

 

{

 

cout<<endl;

 

cout<<"请输入基本信息: "<<endl;

 

cout<<endl;

 

cout<<" "<<"姓名:";

 

cin>>name;

 

cout<<" "<<"号码:";

 

cin>>number;

 

}

 

void person::show_information( ) //在类外定义show_time函数

 

{

 

cout<<endl;

 

cout<<"人员信息: "<<endl;

 

cout<<endl;

 

cout<<"姓名:"<<name<<" "<<"号码: "<<number<<endl;

 

}

 

【C++程序设计实验】类的继承和派生

2、#include <iostream>

 

using namespace std;

 

class Ctriangle

 

{

 

public:

 

void set_R( ); //公用成员函数

 

void show_R( ); //公用成员函数

 

private: //数据成员为私有

 

int r;

 

int h;

 

};

 

class Student: private Ctriangle

 

{

 

public:

 

void show_information_1( )

 

{

 

cout<<endl;

 

cout<<"请输入圆柱底面半径: "<<endl;

 

cout<<endl;

 

cin>>r;

 

cout<<"请输入圆柱的高: "<<endl;

 

cin>>h;

 

cout<<endl;

 

cout<<"底面半径为"<<r<<"高为"<<h<<"的圆柱:"<<endl;

 

cout<<" 面积: "<<2*3.14*r*r+2*3.14*r*h<<endl; //引用派生类的私有成员,正确

 

cout<<" 体积: "<<3.14*r*r*h<<endl; //引用派生类的私有成员,正确

 

cout<<endl;

 

} //引用派生类的私有成员,正确

 

private:

 

int r;

 

int h;

 

};

 

class Teacher: private Ctriangle

 

{

 

public:

 

void show_information_2( )

 

{

 

cout<<endl;

 

cout<<"请输入圆锥底面半径: "<<endl;

 

cout<<endl;

 

cin>>r;

 

cout<<"请输入圆锥的高: "<<endl;

 

cin>>h;

 

cout<<endl;

 

cout<<"底面半径为"<<r<<"高为"<<h<<"的圆锥:"<<endl;

 

cout<<" 面积: "<<3.14*r*h+3.14*r*r<<endl; //引用派生类的私有成员,正确

 

cout<<" 体积: "<<3.14*r*r*h/3<<endl; //引用派生类的私有成员,正确

 

cout<<endl;

 

} //引用派生类的私有成员,正确

 

private:

 

int r;

 

int h;

 

};

 

int main( )

 

{

 

Ctriangle t;

 

t.set_R();

 

t.show_R();

 

Student t1;

 

Teacher t2; //定义对象t1

 

t1.show_information_1();

 

t2.show_information_2();

 

cout<<endl;

 

return 0;

 

}

 

void Ctriangle::set_R( ) //在类外定义set_time函数

 

{

 

cout<<endl;

 

cout<<"请输入球体半径: "<<endl;

 

cout<<endl;

 

cout<<" "<<"球体半径:";

 

cin>>r;

 

}

 

void Ctriangle::show_R( ) //在类外定义show_time函数

 

{

 

cout<<endl;

 

cout<<"半径为"<<r<<"的球体:"<<endl;

 

cout<<endl;

 

cout<<"表面积:"<<4*3.14*r*r<<endl;

 

cout<<"体积:"<<(4/3)*3.14*r*r*r<<endl;

 

}

 

【C++程序设计实验】类的继承和派生

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢