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

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

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;
}
目录
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8084 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10382 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1568 5
|
9天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2719 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2503 0