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

c 继承与派生实验报告
C 继承与派生实验报告
实验目的:通过实验,掌握C语言中继承与派生的概念和使用方法,加深对面向对象编程的理解。
实验内容:在C语言中,继承与派生是面向对象编程中非常重要的概念。
在本次实验中,我们将通过一个简单的例子来演示C语言中的继承与派生的用法。
首先,我们定义一个基类(父类)Person,包括姓名和年龄两个成员变量,以及一个显示信息的成员函数。
然后,我们定义一个派生类(子类)Student,继承自Person类,新增一个成员变量学号,并重写显示信息的成员函数。
在实验中,我们首先创建一个Person对象,设置姓名和年龄,然后调用显示信息函数,观察结果。
接着,我们创建一个Student对象,设置姓名、年龄和学号,再次调用显示信息函数,观察结果。
实验结果:通过实验,我们成功实现了C语言中的继承与派生。
我们发现,通过继承,子类Student可以直接使用父类Person中的成员变量和成员函数,同时可以新增自己的成员变量和函数。
这样的设计使得代码更加简洁和灵活,提高了代码的复用性和可维护性。
结论:C语言中的继承与派生是面向对象编程中非常重要的概念,通过本次实验,我们深入理解了这一概念的用法和意义。
掌握了继承与派生的方法后,我们可以更加灵活地设计和编写程序,提高代码的质量和效率。
总结:通过本次实验,我们对C语言中的继承与派生有了更深入的理解,加深了对面向对象编程的认识。
在今后的学习和工作中,我们将更加灵活地运用继承与派生的方法,提高代码的质量和效率。
c++继承与派生实验报告

第七章继承与派生实验目的学习声明和使用类的继承关系,声明派生类熟悉不同继承方式下对基类的访问控制学习使用虚基类解决二义性问题实验内容7.1#include <iostream>using namespace std;class animal{public:private:int age;};class dog:public animal{public:void setage(int n);private:};void dog::setage(int n){age=n;}int main(){dog d1;d1.setage(5);return 0;}无法访问age,因为公有继承的继承规则是基类的私有类不可访问、#include <iostream>using namespace std;class animal{public:int age;private:};class dog:public animal{public:void setage(int n);private:};void dog::setage(int n){age=n;}int main(){dog d1;d1.setage(5);return 0;}把age移到private里即可7.2#include <iostream>using namespace std;class baseclass{public:baseclass(int n){number=n;cout<<"调用了基类的构造函数"<<endl;}~baseclass(){cout<<"调用了基类的析构函数"<<endl;}private:int number;};class derivedclass:public baseclass{public:derivedclass(int n):baseclass(n){cout<<"调用了派生类的构造函数"<<endl;}~derivedclass(){cout<<"调用了派生类的析构函数"<<endl;}private:};int main(){derivedclass d(3);return 0;}7.3#include <iostream>using namespace std;class vehicle{public:void run();void stop();vehicle(int m,int w){maxspeed=m;weight=w;}private:int maxspeed;int weight;};void vehicle::run(){cout<<"it is running"<<endl;}void vehicle::stop(){cout<<"stop"<<endl;}class bicycle:public vehicle{public:bicycle(int m,int w,int h):vehicle(m,w){height=h;}private:int height;};class motorcar:public vehicle{public:motorcar(int m, int w, int s):vehicle(m,w){seatnum=s;}private:int seatnum;};class motorcycle:public bicycle,public motorcar{public:motorcycle(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s) {}};int main(){motorcycle cat(1,2,3,4);cat.run();cat.stop();return 0;}不设虚基类报错指代不明是哪一层函数#include <iostream>using namespace std;class vehicle{public:void run();void stop();vehicle(int m,int w){maxspeed=m;weight=w;}private:int maxspeed;int weight;};void vehicle::run(){cout<<"it is running"<<endl;}void vehicle::stop(){cout<<"stop"<<endl;}class bicycle:virtual public vehicle{public:bicycle(int m,int w,int h):vehicle(m,w){height=h;}private:int height;};class motorcar:virtual public vehicle{public:motorcar(int m, int w, int s):vehicle(m,w){seatnum=s;}private:int seatnum;};class motorcycle:public bicycle,public motorcar{public:motorcycle(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s) {}};int main(){motorcycle cat(1,2,3,4);cat.run();cat.stop();return 0;}。
C 继承与派生实验报告

C 继承与派生实验报告C 继承与派生实验报告引言:在面向对象的编程中,继承与派生是重要的概念。
通过继承,我们可以从已有的类中派生出新的类,并且可以在新的类中添加额外的属性和方法。
本实验旨在通过实际的编程实践,深入理解C语言中的继承与派生。
实验过程:首先,我们创建了一个基类Animal,其中包含了一个成员变量name和一个成员函数eat。
然后,我们创建了两个派生类Dog和Cat,它们分别继承了Animal类,并且在其中添加了各自特有的成员函数bark和meow。
接着,我们创建了一个对象dog和一个对象cat,并分别调用了它们的成员函数eat、bark 和meow。
实验结果:通过运行程序,我们可以看到dog对象调用了eat和bark函数,而cat对象调用了eat和meow函数。
这说明继承与派生的机制正常工作,派生类可以继承基类的属性和方法,并且可以在派生类中添加新的属性和方法。
实验分析:继承与派生是面向对象编程的重要概念,它可以使得代码的复用性更高,同时也增加了代码的灵活性。
通过继承,派生类可以继承基类的属性和方法,这样可以减少代码的冗余,并且可以在派生类中添加新的功能。
在本实验中,Dog 和Cat类分别继承了Animal类,这样它们就拥有了相同的属性name和方法eat。
然后,通过在派生类中添加新的方法bark和meow,我们可以实现不同的行为。
继承与派生的应用:继承与派生在实际的软件开发中有着广泛的应用。
例如,在一个图形界面程序中,可以定义一个基类Widget,它包含了一些基本的属性和方法,然后可以通过派生类Button和TextBox来创建具体的按钮和文本框。
这样,我们可以通过继承和派生的方式,实现不同的界面元素,并且可以在派生类中添加新的功能,如按钮的点击事件和文本框的输入验证。
继承与派生的注意事项:在使用继承与派生的过程中,我们需要注意一些问题。
首先,派生类可以访问基类的公有成员,但不能访问基类的私有成员。
c派生类与继承实验报告

实验2 派生类与继承实验课程名:面向对象程序设计(C++)专业班级:学号::实验时间:实验地点:指导教师:二、实验内容一、构造一个类Geometry 及其派生类,该类主要实现关于几何图形的基本操作。
对于基类“几何图形”,有求面积、求体积的函数(纯虚函数),其派生类圆和矩形主要有初始化(构造函数),求面积,求周长操作,类圆的派生类圆球和圆柱有求表面积、体积操作。
试在主函数中分别定义圆、圆球、圆柱以及矩形的对象,并调用其成员函数实现其相应操作。
实验代码如下:#include<iostream>using namespace std;class Geometry{public:CircleradiumsCircle()~Circle() BallBall()~Ball() GeometryGeometry()~Geometry()GetArea()GetPerimeter()Getcolume()show()Column Column()~Column()Rectangle Rectangle() ~Rectangle()Column column(1,2,3);column.show();return 0;}运行结果:代码分析:1)首先定义基类Geometry,在定义基类的派生类Circle,Rectangle再定义以Circle,Rectangle为基类的派生类Column,以及以Circle为基类的派生类Ball;2)在定义派生类时用构造函数初始化私有成员;3)最后用类的对象来调用类函数;二、设计如下类:(1)建立一个Point类,表示平面中的一个点;建立一个Line类,表示平面中的一条线端,内含两个Point类的对象;建立Triangle类,表示一个三角形,内含三个Line类的对象构成一个三角形。
(2)设计三个类的相应的构造函数、复制构造函数,完成初始化和对象复制(3)设计Triangle类的成员函数完成三条边是否能构成三角形的检验和三角形面积计算,面积显示。
C++ 实验四 继承和派生

.继承和派生一、实验目的1.学习定义和使用类的继承关系,定义派生类;2.熟悉不同继承方式下对基类成员的访问控制;3.了解派生类中如何使用基类的成员、基类成员在派生类中的访问控制;二、内容与设计思想上机实践内容:1.编写程序,定义一个人员类CPerson,包括数据成员:姓名、编号、性别和用于输入、输出的成员函数。
在此基础上派生出学生类Cstudent(增加成绩)和教师类Cteacher(增加教龄),并实现对学生和教师信息的输入、输出。
2.编写程序,设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weigh,带参数的构造函数、拷贝构造函数、数据成员设置函数Set()、数据成员显示函数show()。
小车类car是vehicle的派生类其中包含载人数passenger_load。
卡车类truck是vehicle的派生类其中包含载人数passenger_load和载重量payload。
每个类都有相关数据的输出方法。
三、使用环境操作系统:Windowns XPC++环境:Visual C++ 6.0四、核心代码及调试过程\#include<iostream>#include<string>using namespace std;class CPerson{public:void getCPerson(){cout<<"enter Name Number Sex:";cin>>name>>number>>sex;}void printCPerson(){cout<<"Name:"<<name<<"Number:"<<number<<"Sex:"<<sex;}private:string name,sex;int number;};class Cstudent:public CPerson{public:void getCstudent(){getCPerson();cout<<"enterScore:";cin>>chengji;} void printCstudent(){printCPerson();cout<<"Score:"<<chengji<<endl;} private:int chengji;};class Cteacher:public CPerson{public:void getCteacher(){getCPerson();cout<<"enterTeachAge:";cin>>techage;} void printCteacher(){printCPerson();cout<<"TeachAge:"<<techage<<endl;} private:int techage;};void main(){cout<<"chose the kinds:Student(1),Teacher(2)"<<endl;int i;cin>>i;if(i==1){Cstudent b;b.getCstudent();b.printCstudent();}if(i==2){Cteacher c;c.getCteacher();}}#include<iostream>using namespace std;class vehicle{protected:int wheels;float weight;public:vehicle(int wheels,float weight);int get_wheels();float get_weight();float wheel_load();void show();};class car:public vehicle{int passenger_load;public:car(int wheels,float weight,int passengers=8);void show();};class truck:public vehicle{int passenger_load;float payload;public: truck(int wheels,float weight,int passengers=4,float max_load=111111.11);int get_passengers();float efficiency();void show();};vehicle::vehicle(int wheels,float weight) {vehicle::wheels=wheels;vehicle::weight=weight;}int vehicle::get_wheels(){return wheels;}float vehicle::get_weight(){return weight/wheels;}void vehicle::show() {cout << "车轮:" << wheels << "个" << endl;cout << "重量:" << weight << "公斤" << endl;}car::car(int wheels, float weight, int passengers) :vehicle (wheels, weight) {passenger_load=passengers;}int car::get_passengers (){}void car::show(){cout <<"车型:小车" << endl;vehicle::show();cout << "载人:" << passenger_load << "人" << endl;cout << endl;}truck:: truck(int wheels, float weight,int passengers, float max_load):vehicle(wheels,weight){passenger_load=passengers;payload=max_load;}int truck::get_passengers(){return passenger_load;}void truck::show(){cout <<"车型:卡车" << endl;vehicle:: show ();cout << "载人:" << passenger_load << "人" << endl;cout << "载重量:" << payload << endl;cout << endl;}void main (){car car1(8,1111,10);truck tru1(20,2222,6,111111);cout << "输出结果" << endl;car1. show ();tru1. show ();}五、总结通过本次上机实践,我学会了类的定义及运用,以及在定义的类中各种函数的用法,兵使其得到实际运用。
实验五 继承与派生实验报告

实验五继承与派生一、实验目的( 1 )熟练掌握类的继承,能够定义和使用类的继承关系。
( 2 )掌握派生类的声明与实现方法。
( 3 )掌握类构造函数的初始化列表与作用域分辨符的使用方法。
二、实验工具与准备工作在开始实验前,应回顾或复习相关内容。
需要一台主算机,其中安装有VisualC + + 6 .0 、VisualC + + 2005 、VisualC + + 2005 ExpreSS 、Dev 一C + +或MinGW Developer Studio 等集成开发环境软件。
三、实验内容完成设计性实验项目8.7 创建一个表示雇员信息的employee类,其中包含数据成员name、empNo和salary,分别表示雇员的姓名、编号和月薪。
再从employee类派生出3个类worker、technician和salesman,分别代表普通工人、科研人员、销售人员。
三个类中分别包含数据成员productNum、workHours和monthlysales,分别代表工人每月生产产品的数量、科研人员每月工作的时数和销售人员每月的销售额。
要求各类中都包含成员函数pay,用来计算雇员的月薪,并假定:普通工人的月薪=每月生产的产品数×每件产品的赢利×20%科研人员的月薪=每月的工作时数×每小时工作的酬金销售人员的月薪=月销售额×销售额提成。
#include<iostream>using namespace std;class employee{protected:char name[20];int empNo;float salary;public:employee(char *cp="李强",int no=1001);employee(employee&);void setname(char *cp);void setempNo(int no);void setsalary(float sa);char*getname();int getempNo();float getsalary();void display();employee compsalary(employee *emp);};employee::employee(char *cp,int no){int i=0;while(*cp){name[i]=*cp;i++;cp++;}name[i]='\0';empNo=no;}employee::employee(employee &em) {int i=0;while([i]){name[i]=[i];i++;}name[i]='\0';empNo=em.empNo;salary=em.salary;}void employee::setname(char *cp) {int i=0;while(*cp){name[i]=*cp;i++;cp++;}name[i]='\0';}void employee::setempNo(int no) {empNo=no;}void employee::setsalary(float sa) {salary=sa;}char*employee::getname(){return name;}int employee::getempNo(){return empNo;}float employee::getsalary(){return salary;}void employee::display(){cout<<"工号为"<<empNo<<"的雇员"<<name<<"的月薪为"<<salary<<endl; }employee employee::compsalary(employee *emp){if(this->salary>=emp->salary)return *this;elsereturn *emp;}class worker : public employee{public:worker(char*,int,int);void setproductNum(int n){ productNum=n;}int getproductNum(){ return productNum;}void pay();private:int productNum;static float proper; //每件产品的利润};float worker::proper=30; //假设每件产品的利润为30元worker::worker(char *name,int no,int pronum):employee(name,no){productNum=pronum;}void worker::pay(){salary=productNum*proper*0.2;}class technician : public employee{public:technician(char*,int,float);void sethours(float h){ workHours=h;}float gethours(){ return workHours;}void pay();private:float workHours;static float payperhour; //科研人员每小时工作的酬金};float technician::payperhour=50; //假设科研人员每小时工作的酬金为50元technician::technician(char *name,int no,float hours):employee(name,no){workHours=hours;}void technician::pay(){salary=workHours*payperhour;}class salesman : public employee{public:salesman(char*,int,float);void setmonthlysales(int num){ monthlysales=num;}int getmonthlysales(){ return monthlysales;}void pay();private:float monthlysales;static float percent; //销售人员的销售提成};float salesman::percent=0.6; //假设销售人员的销售提成为60% salesman::salesman(char *name,int n,float sn):employee(name,n){monthlysales=sn;}void salesman::pay(){salary=monthlysales*percent;}void main(){worker w1("王某",1001,1000);technician t1("李某",1002,200);salesman s1("许某",1003,10000);w1.pay();t1.pay();s1.pay();cout<<"工人"<<w1.getname()<<"的月薪为"<<w1.getsalary()<<"元\n";cout<<"科研人员"<<t1.getname()<<"的月薪为"<<t1.getsalary()<<"元\n";cout<<"销售人员"<<s1.getname()<<"的月薪为"<<s1.getsalary()<<"元\n";}8.8 创建一个automobile类表示汽车,其中包含数据成员brand和speed,分别表示汽车的品拍和行驶速度;成员函数run和stop表示行驶和停止。
实验三 报告 继承和派生类

实验三继承和派生类1.调试下列程序,并对程序进行修改后再调试,指出调试中的出错原因。
#include <iostream.h>class A{public:void seta(int i){ a=i; }int geta(){ return a; }public:int a;};class B:public A{public:void setb(int i){ b=i; }int getb(){ return b; }void show(){ cout<<"A::a="<<a<<endl; } //语句9 public:int b;};void main(){B bb; //语句1bb.seta(6); //语句2bb.setb(3); //语句3bb.show(); //语句4cout<<"A::a="<<bb.a<<endl; //语句5cout<<"B::b="<<bb.b<<endl; //语句6cout<<"A::a="<<bb.geta()<<endl; / /语句7cout<<"B::b="<<bb.getb()<<endl; //语句8 }按下列要求对程序进行修改,然后调试,对出现的错误分析其原因。
(1)将派生类B的继承方式改为private时,会出现哪些错误和不正常现象?为什么?(2)将派生类B的继承方式改为protected时,会出现哪些错误和不正常现象?为什么?(3)将派生类B的继承方式恢复为public后,再将类A中数据成员a的访问权限改为private时,会出现哪些错误和不正常现象?为什么?(4)派生类B的继承方式仍为public,将类A中数据成员a的访问权限改为protected时,会出现哪些错误和不正常现象?为什么?2.定义一个基类MyArray,基类中可以存放一组整数。
实验二类的继承与派生

实验二类的继承与派生实验二类的继承与派生一、实验目的1. 掌握类的声明和使用。
2. 掌握对象的声明和使用。
3. 掌握具有不同访问属性的成员的访问方式。
4. 观察构造函数和析构函数的执行过程。
5. 学习声明和使用类的继承关系,声明派生类;6. 熟悉不同继承方式下对基类成员的访问控制;二.实验内容1. 设计一个用于人事管理的People(人员)类。
考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex (性别)、birthday(出生日期)、id(身份证号)等等。
具有的属性如下:姓名char name[11]、编号char number[7]、性别char sex[3]、生日birthday、身份证号charid[20]。
其中“出生日期”声明为一个“日期”类内嵌子对象。
用成员函数实现对人员信息的录入和显示。
要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、组合。
在测试程序中声明people 类的对象数组,录入数据并显示。
2. 从people(人员)类派生出student(学生)类,添加属性:班号char classNO[7];从people 类派生出teacher(教师)类,添加属性:职务char pship[11]、部门char departt[21]。
从student 类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser;从graduate 类和teacher 类派生出TA(助教博士生)类,重载相应的成员函数,测试这些类。
三 . 实验步骤1.程序代码第一题#include#includeusing namespace std;class Date //日期类{private:int year;int month;int day;public:Date(){} //默认构造Date(int y,int m,int d) //带参构造{year=y;month=m;day=d;}void set() //设置数据函数{cin>>year>>month>>day;}void display() //显示函数{cout<<year<<"年"<<month<<"月"<<day<<"日";< bdsfid="103" p=""></year<<"年"<<month<<"月"<<day<<"日";<>}};class Person //人员类{private:string name;int num;char sex;Date birthday;char ID[18];public:Person(){} //默认构造Person(int n,int y,int m,int d,char id[18],char s='m'):birthday(y,m,d) {num=n;sex=s;strcpy(ID,id);} //有默认值的带参构造Person(Person& p) //拷贝构造{ name=;num=p.num;sex=p.sex;birthday=p.birthday;strcpy(ID,p.ID);}void input() //输入函数{cout<<"录入数据:"<<endl;< bdsfid="131" p=""></endl;<> cout<<"姓名:";cin>>name;cout<<"编号:";cin>>num;cout<<"性别(m/f):";cin>>sex;cout<<"生日:";birthday.set();cout<<"身份证号:";cin>>ID;ID[18]='\0';cout<<endl;< bdsfid="144" p=""></endl;<>}void output() //输出函数{cout<<"编号:"<<num<<endl;< bdsfid="149" p=""></num<<endl;<>cout<<"姓名:"<<name<<endl;< bdsfid="151" p=""></name<<endl;<>cout<<"性别:"<<sex<<endl;< bdsfid="153" p=""></sex<<endl;<>cout<<"生日:";birthday.display();cout<<endl;< bdsfid="157" p=""></endl;<>cout<<"身份证号:"<<id<<endl;< bdsfid="159" p=""></id<<endl;<>}~Person() //析构函数{cout<<" "<<num<<"号人员已经录入"<<=""></num<<"号人员已经录入"<};int main(){Person p1;p1.input();p1.output();return 0;}第二题#include#includeusing namespace std;class Date //日期类{private:int year;int month;int day;public:Date(){} //默认构造Date(int y,int m,int d) //带参构造{year=y;month=m;day=d;}void set() //设置数据函数{cin>>year>>month>>day;}void display() //显示函数{cout<<year<<"年"<<month<<"月"<<day<<"日";< bdsfid="200" p=""></year<<"年"<<month<<"月"<<day<<"日";<>}};class Person //人员类{private:string name;int num;char sex[10];Date birthday;char ID[18];public:Person(){} //默认构造Person(int n,int y,int m,int d,char id[18],char sex[10]):birthday(y,m,d) {num=n;strcpy(ID,id);} //有默认值的带参构造Person(Person& p) //拷贝构造{ name=;num=p.num;birthday=p.birthday;strcpy(ID,p.ID);}void input() //输入函数{cout<<"姓名:";cin>>name;cout<<"编号:";cin>>num;cout<<"性别(男/女):";cin>>sex;cout<<"生日:";birthday.set();cout<<"身份证号:";cin>>ID;ID[18]='\0';cout<<endl;< bdsfid="237" p=""></endl;<>}void output() //输出函数{cout<<"编号:"<<num<<endl;< bdsfid="242" p=""></num<<endl;<>cout<<"姓名:"<<name<<endl;< bdsfid="244" p=""></name<<endl;<>cout<<"性别:"<<sex<<endl;< bdsfid="246" p=""></sex<<endl;<>cout<<"生日:";birthday.display();cout<<endl;< bdsfid="250" p=""></endl;<>cout<<"身份证号:"<<id<<endl;< bdsfid="252" p=""></id<<endl;<>}~Person() //析构函数{//cout<<" "<<num<<"号人员已经录入"<<=""></num<<"号人员已经录入"<};class stduent:public Person{char classno[7];public: student(){cout<<"*************"<<="">void input(){Person::input();cout<<"输入学号"<<endl;< bdsfid="269" p=""></endl;<>cin>>classno;}void getno(){Person::output();cout<<"学号为:"<<classno<<endl;< bdsfid="275" p=""></classno<<endl;<>}};class teacher:public Person{char pship[11],departt[21];public :teacher(){cout<<"***********"<<endl;}< bdsfid="283" p=""></endl;}<> void input(){Person::input();cout<<"输入职务"<<endl;< bdsfid="288" p=""></endl;<>cin>>pship;cout<<"输入部门"<<endl;< bdsfid="291" p=""></endl;<>cin>>departt;}void inputt(){cout<<"输入职务"<<endl;< bdsfid="297" p=""></endl;<>cin>>pship;cout<<"输入部门"<<endl;< bdsfid="300" p=""></endl;<>cin>>departt;}void getno(){Person::output();cout<<"职务为:"<<pship<<endl;< bdsfid="306" p=""></pship<<endl;<>cout<<"部门为:"<<departt<<endl;< bdsfid="308" p=""></departt<<endl;<>}void output (){cout<<"职务为:"<<pship<<endl;< bdsfid="313" p=""></pship<<endl;<>cout<<"部门为:"<<departt<<endl;< bdsfid="315" p=""></departt<<endl;<>}};class graduate:public stduent{char subject[21], adviser[21];public :graduate(){cout<<""<<endl;< bdsfid="323" p=""></endl;<>}void input(){stduent::input();cout<<"输入专业:"<<endl;< bdsfid="329" p=""></endl;<> cin>>subject;cout<<"输入导师:"<<endl;< bdsfid="332" p=""></endl;<>cin>>adviser;}void getno(){ stduent::getno();cout<<"专业为:"<<subject<<endl;< bdsfid="338" p=""></subject<<endl;<>cout<<"导师为:"<<adviser<<endl;< bdsfid="340" p=""></adviser<<endl;<>}};class TA :public graduate,teacher{public :TA(){}void input(){graduate::input();teacher::inputt();}void getno(){graduate::getno();teacher::output();}};int main(){Person p1;stduent s;teacher t;graduate g;TA T;cout<<"请依次输入人员数据信息"<<endl;< bdsfid="366" p=""></endl;<>p1.input();cout<<"请输入学生数据信息";s.input();cout<<"请输入老师数据信息";t.input();cout<<"请输入研究生数据信息";g.input();cout<<"请输入助教博士数据信息";T.input();cout<<"人员数据信息为:";p1.output();cout<<"学生数据信息为:";s.getno();cout<<"老师信息为:";t.getno();cout<<"研究生信息为:";g.getno();cout<<"助教博士信息为:"T.getno();}2.调试程序第一次调试,发现没有名字的显示。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(1)定义一个基类圆,至少含有一个数据成员:半径;
(2)定义基类的派生类:球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。
(3)定义主函数,求球、圆柱、圆锥的和体积。
算
法
描
述
及
实
验
步
骤
1首先定义一个基类person
2派生类student和teacher
3实现客户信息的手动输入
4实现客户输出信息的需求
}
实验一: person.cpp
#include "person1.h"
#include<iostream>
#include<cstdlib>
#define max 1000
#include<iomanip>
using namespace std;
int main()
{
system("color 1b");
void person::set()
{
cout<<"请输入编号number:\n";
cin>>number;
cout<<"请输入name:\n";
cin>>name;
}
void person::display()
{
cout<<"编号:"<<setw(10)<<number<<" :"<<setw(10)<<name<<endl;
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人)";
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];
}
void teacher::set2()
{
set();
cout<<"请输入教师的职业名:\n";
cin>>occupation;
cout<<"请输入教师的部门:\n";
cin>>department;
}
void teacher::display2()
{
display();
cout<<"教师的职业名:"<<setw(10)<<occupation<<" 教师的部门:"<<setw(10)<<department<<endl;
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人)";
5实现客户的循环利用
6首先定义一个基类circle
7派生类cylinder和cone
8构造函数及调用有关函数
9定义求体积及面积的函数
调
试
过
程
及
实
验
结
果
调试过程中出现较少的语法错误,主要是以及友元的使用不熟练等
实验一的调试结果:学生及教师的信息输入:
学生及教师信息的输出:(有清屏的实现)
实验二的调试结果:
char department[20];
public:
void set2();
void display2();
};
实验一: person1.cpp
#include "person1.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
总
结
1)仔细学习C++语法;
2)实验前预先编写好自己ห้องสมุดไป่ตู้程序;
3)注意对C++中英文单词的记忆与书写;
4)注意标点符号均为英文式的;
5)注意空格的位置;
6)注意一句话结束时,是以分号结束;
7)注意主函数只有一个;
8)注意流的符号;
9)注意类的使用方法;
10)注意类的继承与派生的
11)注意构造函数的使用;
实验环境
VC++6.0集成环境
实
验
容
二、实验容
1.编写一个学生和教师相关数据输入和显示程序。学生数据有编号、、班级和成绩,教师数据有编号、、职称和部门。 要求:将编号、及其输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。
2.编写程序计算出球、圆柱和圆锥的表面积和体积。
}
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;
课程名称
C++程序设计
班级
1320541
实验日期
2014年11月3日
姓 名
创
学号
37
实验成绩
实验名称
C++面向对象程序设计
实
验
目
的
及
要
求
一、实验目的
1、 理解继承的含义,掌握派生类的定义方法和实现;
2、 理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;
3、 理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员
cin>>c;
for(int i=0;i<c;i++)
{
cout<<"请输入第"<<i+1<<"教师的信息:\n";
teac[i].set2();
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}