0
点赞
收藏
分享

微信扫一扫

河工大廊坊实验六(实验 6 继承与派生)


2.编写一个学生和教师数据输入和显示程序,学生数据包含编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类CPerson,并作为学生类CStudent和教师类CTeacher的基类。要求:
(1)为CPerson类、CStudent类和CTeacher类提供必要的成员函数;
(2)提供派生类构造函数,显式调用基类CPerson的构造函数。

#include<iostream>
#include<string>
using namespace std;
class CPerson{
public:
int date;//编号
string name;
CPerson(){}
CPerson(string n,int d):name(n),date(d){}
void set(string n,int d){
name=n;
date=d;
}
void output(){
cout<<"姓名:"<<name<<"\n编号:"<<date<<endl;
}

};
class CStudent :public CPerson{
public:
int Myclass;//班号
float score;
CStudent(){}
CStudent(string n,int d,int mc,float s):CPerson(n,d),Myclass(mc),score(s){}
void set(string n,int d,int mc,float s){
CPerson::set(n,d);
Myclass=mc;
score=s;
}
void output(){
cout<<"姓名:"<<name<<"\n编号:"<<date<<endl;
cout<<"班号:"<<Myclass<<"\n成绩:"<<score<<endl;
}
};
class CTtudent :public CPerson{
public:
string Myzhicheng;//职称
string bumeng;//部门
CTtudent(){};
CTtudent(string n,int d,string mc,string s):CPerson(n,d),Myzhicheng(mc),bumeng(s){}
void set(string n,int d,string mc,string s){
CPerson::set(n,d);
Myzhicheng=mc;
bumeng=s;
}
void output(){
cout<<"姓名:"<<name<<"\n编号:"<<date<<endl;
cout<<"职称:"<<Myzhicheng<<"\n部门:"<<bumeng<<endl;
}
};
int main(){
CStudent a1("笑霸final",101600,4,750);
CStudent a2;
a2.set("张三",101601,4,600);
CTtudent b1("刘老师",102600,"优秀老师","计科部");
CTtudent b2;
b2.set("张老师",102601,"优秀老师","基础部");
a1.output();
a2.output();
cout<<endl;
b1.output();
b2.output();
system("pause");
return 0;
}

3.定义类CPoint,描述坐标系中的一个点;在其基础上派生出圆CCircle类,增加圆半径信息;在CCircle类基础上派生出Cylinder类,增加圆柱高信息。要求必须具有的成员函数:带参的构造函数、对数据成员进行赋值的函数、获取数据值的函数,同时派生类CCircle类应具有计算并返回圆面积、圆周长的函数;派生类Cylinder类应具有计算并返回圆柱体表面积、圆柱体体积的函数。编写主函数,测试这个层次结构,输出圆柱体类的相关信息。`*

*
3. 定义类CPoint,描述坐标系中的一个点;
在其基础上派生出圆CCircle类,增加圆半径信息;
在CCircle类基础上派生出Cylinder类,增加圆柱高信息。
要求必须具有的成员函数:
带参的构造函数、对数据成员进行赋值的函数、获取数据值的函数,
同时派生类CCircle类应具有计算并返回圆面积、圆周长的函数;
派生类Cylinder类应具有计算并返回圆柱体表面积、圆柱体体积的函数。
编写主函数,测试这个层次结构,输出圆柱体类的相关信息。
*/
#include<iostream>
#include<cmath>
using namespace std;
class CPoint{
public:
float x,y;
CPoint(){}
CPoint(float a,float b):x(a),y(b){}
};
class CCircle:public CPoint{
protected:
float r;
public:
CCircle(){}
CCircle(float a,float b,float c)
:CPoint(a,b),r(c){}

void set(float a,float b,float c){
x=a;
y=b;
r=c;
}

float gte_x()const{return x;}
float gte_y()const{return y;}
float gte_r()const{return r;}
double area(){
return 3.14*pow(double(r),2.0);
}
float perimeter(){
return 2*3.14*r;
}

};
class Cylinder:public CCircle{
private:
float h;
public:
Cylinder(float a,float b,float c,float d)
:CCircle(a,b,c),h(d){}
Cylinder(){}
void set(float a,float b,float c,float d){
x=a;
y=b;
r=c;
h=d;
}
double B_area(){
return area()*2+perimeter()*h;
}
double volume(){
return area()*h;
}
float get_h()const{return h;}

};
int main(){
Cylinder a1(0,0,1,1),a2;
cout<<"圆柱体底面圆心坐标:"<<'('<<a1.gte_x()<<','<<a1.gte_y()<<')'<<endl;
cout<<"圆柱体底面圆半径:"<<a1.gte_r()<<endl;
cout<<"圆柱体的高:"<<a1.get_h()<<endl;
cout<<"圆柱体的体积:"<<a1.volume()<<endl;
float x,y,r,h;
cout<<"输入圆柱体底面圆的圆心坐标及半径:";
cin>>x>>y>>r;
cout<<"输入圆柱的高:";
cin>>h;
a2.set(x,y,r,h);
cout<<"圆柱体底面圆心坐标:"<<'('<<a2.gte_x()<<','<<a2.gte_y()<<')'<<endl;
cout<<"圆柱体底面圆半径:"<<a2.gte_r()<<endl;
cout<<"圆柱体的高:"<<a2.get_h()<<endl;
cout<<"圆柱体的体积:"<<a2.volume()<<endl;
system("pause");
return 0;
}


举报

相关推荐

0 条评论