收藏 分销(赏)

c++类的继承与派生--实验报告.doc

上传人:二*** 文档编号:4499035 上传时间:2024-09-25 格式:DOC 页数:7 大小:163KB
下载 相关 举报
c++类的继承与派生--实验报告.doc_第1页
第1页 / 共7页
本文档共7页,全文阅读请下载到手机保存,查看更方便
资源描述
. . . . 太原工业学院计算机工程系 实 验 报 告 课程名称 C++程序设计 班级 1320541 实验日期 2014年11月3日 姓 名 创 学号 37 实验成绩 实验名称 C++面向对象程序设计 实 验 目 的 与 要 求 一、实验目的 1、 理解继承的含义,掌握派生类的定义方法和实现; 2、 理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员; 3、 理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员 实验环境 VC++6.0集成环境 实 验 容 二、实验容 1. 编写一个学生和教师相关数据输入和显示程序。学生数据有编号、、班级和成绩,教师数据有编号、、职称和部门。 要求:将编号、与其输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。 2. 编写程序计算出球、圆柱和圆锥的表面积和体积。 要求: (1)定义一个基类圆,至少含有一个数据成员:半径; (2)定义基类的派生类:球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。 (3)定义主函数,求球、圆柱、圆锥的和体积。 算 法 描 述 与 实 验 步 骤 ①  首先定义一个基类person ②  派生类student和teacher ③  实现客户信息的手动输入 ④  实现客户输出信息的需求 ⑤  实现客户的循环利用 ⑥  首先定义一个基类circle ⑦  派生类cylinder和cone ⑧  构造函数与调用有关函数 ⑨  定义求体积与面积的函数 调 试 过 程 与 实 验 结 果 调试过程中出现较少的语法错误,主要是以与友元的使用不熟练等 实验一的调试结果:学生与教师的信息输入: 学生与教师信息的输出:(有清屏的实现) 实验二的调试结果: 总 结 1) 仔细学习C++语法; 2) 实验前预先编写好自己的程序; 3) 注意对C++中英文单词的记忆与书写; 4) 注意标点符号均为英文式的; 5) 注意空格的位置; 6) 注意一句话完毕时,是以分号完毕; 7) 注意主函数只有一个; 8) 注意流的符号; 9) 注意类的使用方法; 10) 注意类的继承与派生的 11) 注意构造函数的使用; 12) 注意头文件的关联; 13) 注意循环的使用 (对实验结果进行分析,实验心得体会与改进意见) 附 录 附 录 附 录 附 录 实验的源程序: 实验一: person1.h class person { private: char number[20]; char name[20]; public: void set(); void display(); }; class student:public person { private: char classname[20]; double score; public: void set1(); void display1(); }; class teacher:public person { private: char occupation[20]; char department[20]; public: void set2(); void display2(); }; 实验一: person1.cpp #include "person1.h" #include<iostream> #include<iomanip> #include<string> using namespace std; void person::set() { cout<<"请输入编号number:\n"; cin>>number; cout<<"请输入name:\n"; cin>>name; } void person::display() { cout<<"编号:"<<setw(10)<<number<<" :"<<setw(10)<<name<<endl; } void student::set1() { set(); cout<<"请输入学生的班级名classname:\n"; cin>>classname; cout<<"请输入学生的成绩score:\n"; cin>>score; } void student::display1() { display(); cout<<"学生的班级名:"<<setw(10)<<classname<<" 学生的成绩:"<<setw(10)<<score<<endl; } void teacher::set2() { set(); cout<<"请输入教师的职业名:\n"; cin>>occupation; cout<<"请输入教师的部门:\n"; cin>>department; } void teacher::display2() { display(); cout<<"教师的职业名:"<<setw(10)<<occupation<<" 教师的部门:"<<setw(10)<<department<<endl; } 实验一: person.cpp #include "person1.h" #include<iostream> #include<cstdlib> #define max 1000 #include<iomanip> using namespace std; int main() { system("color 1b"); char a,d; int b; int c; student stud[max]; teacher teac[max]; while(1) { cout<<"请问你是否需要输入学生信息?(y/n)\n"; cin>>a; if(a=='y'||a=='Y') { cout<<"请问你要输入几个学生的信息(n不大于1000人)"; cin>>b; for(int i=0;i<b;i++) { cout<<"请输入第"<<i+1<<"的信息\n"; stud[i].set1(); } } cout<<"请问你是否需要输入教师信息?(y/n)\n"; cin>>a; if(a=='y'||a=='Y') { cout<<"请问你要输入几个(n)教师的信息(n不大于1000人)"; cin>>c; for(int i=0;i<c;i++) { cout<<"请输入第"<<i+1<<"教师的信息:\n"; teac[i].set2(); cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; } } system("cls"); cout<<"请问你是否查看全部信息?(y/n)\n"; cin>>a; if(a=='y'||a=='Y') { cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; for(int i=0;i<b;i++) { cout<<"输出第"<<i+1<<"位学生的信息:\n"; stud[i].display1(); } for( i=0;i<c;i++) { cout<<"输出第"<<i+1<<"位教师的信息:\n"; teac[i].display2(); } cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; } cout<<"请问你是否继续重新返回操作?(y/n)\n"; cin>>d; if(d=='n'||d=='N')break; } return 0; } 实验二: circle1.h class circle { protected: double radius; public: circle(double r); virtual ~circle(); }; class globe:public circle { public: globe(double r):circle(r) { radius=r; } void area(); void volume(); void display(); }; class cylinder:public circle { private: double height; public: cylinder(double r,double h):circle(r) { height=h; } void area1(); void volume1(); void display1(); }; class cone:public circle { private: double height; public: cone(double r,double h):circle(r) { height=h; } void area2(); void volume2(); void display2(); }; 实验二: circle1.cpp #define pi 3.1415926 //定义π #include "circle1.h" #include<cmath> #include<iostream> using namespace std; circle::circle(double r) { radius=r; } circle::~circle() { } void globe::area() { cout<<"则球的表面积:"<<pi*radius*radius<<endl; } void globe::volume() { cout<<"则球的体积:"<<(4*pi*radius*radius*radius)/3<<endl; } void globe::display() { cout<<"球的半径:"<<radius<<endl; area(); volume(); } void cylinder::area1() { cout<<"则圆柱的面积:"<<2*pi*radius*radius+2*pi*radius*height<<endl; } void cylinder::volume1() { cout<<"则圆柱的体积:"<<pi*radius*radius*height<<endl; } void cylinder::display1() { cout<<"圆柱的半径:"<<radius<<" ;圆柱的高:"<<height<<endl; area1(); volume1(); } void cone::area2() { cout<<"则圆锥体的面积:"<<pi*radius*sqrt(radius*radius+height*height)<<endl;//利用sqat函数 } void cone::volume2() { cout<<"则圆锥体的体积:"<<(pi*radius*radius*height)/3; } void cone::display2() { cout<<"圆锥体的半径:"<<radius<<" ;圆锥体的高:"<<height<<endl; area2(); volume2(); cout<<endl; } 实验二: circle.cpp #include "circle1.h" #include<iostream> using namespace std; int main() { globe gl(2); //球 gl.display(); cylinder cy(2,3); //圆柱体 cy.display1(); cone co(2,3); //圆锥体 co.display2(); return 0; } (源程序清单等) 7 / 7
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服