继承和派生实验报告
- 格式:doc
- 大小:109.00 KB
- 文档页数:7
实验三继承与派生1.实验目的(1)熟练掌握类的继承,能够定义和使用类的继承关系。
(2)掌握派生类的声明和实现方法。
(3)掌握类构造函数的初始化列表与作用域分辨符的使用方法。
(4)理解虚基类在解决二义性问题中的作用。
2.实验内容(1)定义Staff(员工)类,有Staff分别派生出Saleman(销售员)类和Manager (经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生出新类SaleManager(销售经理)类。
要求:a.在Staff类中包含的数据成员有编号(num)、姓名(name)、出勤率(rateOfAttend)、基本工资(basicSal)和奖金(prize)。
在Saleman类中还包含数据成员:销售员提成比例(deductRate)和个人销售额(personAmount),在Manager类中还包含数据成员:经理提成比例(totalDeductRate)和总销售额(totalAmount)。
在SaleManager类中不包含其他数据成员。
b.各类人员的实发工资公式如下:员工实发工资=基本工资+奖金+出勤率销售员实发工资=基本工资+奖金+出勤率+个人销售额*销售员提成比例经理实发工资=基本工资+奖金+出勤率+总销售额*经理提成比例销售经理实发工资=基本工资+奖金+出勤率+个人销售额*销售员提成比例+总销售额*经理提成比例c.每个类都有构造函数、输出基本信息函数(Output)和输出实发工资函数(OutputW age)。
3.实验步骤(1)实验源程序//程序功能:熟悉类的继承与派生的基本应用#include<iostream>using namespace std;class Staff //定义员工类{protected:char name[20];float rateOfAttend;int num;int basicSal;int prize;int salary;public:Staff(int nu, char na[], float rate, int bac, int pe){num = nu;strcpy(name, na);rateOfAttend = rate;basicSal = bac;prize = pe;}void Output() const{cout << "员工的基本信息:\n";cout << "编号姓名出勤率基本工资奖金\n";cout << num << " "<< name << " "<< rateOfAttend << " "<< basicSal << " "<< prize << endl;}int OutputW age(float rate, int bac, int pe){salary = bac + pe * rate;return (salary);}};class Saleman:virtual public Staff //继承得到销售员类Saleman{protected:float deductRate;int personAmount;int lsalary;public:Saleman(int nu, char na[], float rate, int bac, int pe, int ded, int person):Staff(nu, na, rate, bac, pe){deductRate = ded;personAmount = person;}void Output() const{Staff::Output();cout << "提成比例:" << deductRate << endl;cout << "个人销售额:" << personAmount << endl;}int OutputW age(float rate, int bac, int pe, float ded, int person){lsalary = Staff:: OutputW age( rate, bac, pe) + ded * person;return (lsalary);}};class Manager:virtual public Staff //继承得到经理类Manager {protected:float totalDeductRate;int totalAmount;int rsalary;public:Manager(int nu, char na[], float rate, int bac, int pe, float totalr, int totala):Staff(nu, na, rate, bac, pe){totalDeductRate = totalr;totalAmount = totala;}void Output() const{Staff::Output();cout << "提成比例:" << totalDeductRate << endl;cout << "总销售额:" << totalAmount << endl;}int OutputW age(float rate, int bac, int pe, float totalr, int totala){rsalary = Staff:: OutputW age( rate, bac, pe) + totalr * totala;return(rsalary );}};class SaleManager:virtual public Saleman,virtual public Manager //有多重继承得到销售经理类{protected:int lrsalary;public:SaleManager(int nu, char na[], float rate, int bac, int pe, float ded, int person, float totalr, int totala):Staff(nu, na, rate, bac, pe),Saleman(nu, na, rate, bac, pe, ded, person),Manager(nu, na, rate, bac, pe, totalr, totala){}void Output() const{Staff::Output();cout << "提成比例:" << deductRate << endl;cout << "个人销售额:" << personAmount << endl;cout << "提成比例:" << totalDeductRate << endl;cout << "总销售额:" << totalAmount << endl;}int OutputW age(float rate, int bac, int pe, float ded, int person, float totalr, int totala){lrsalary = Staff:: OutputWage( rate, bac, pe) + ded * person + totalr * totala;return( lrsalary);}};void main(){Saleman objsale(101101, "LD", 0.88f, 1200, 800, 0.05f, 10000);Manager objmana(101102, "NXG", 0.90f, 2500, 1000, 0.10f, 20000);SaleManager objsalemana(101103, "HDY", 0.99f, 3500, 2000, 0.20f, 100000, 0.20f,150000);objsale.Output();cout << "销售员的实发工资:" << " ";cout << objsale.OutputWage(0.88f, 1200, 800, 0.05f, 10000) << endl;cout << endl;objmana.Output();cout << "经理的实发工资:" << " ";cout << objmana.OutputW age(0.90f, 2500, 1000, 0.10f, 20000) << endl;cout << endl;objsalemana.Output();cout << "销售经理的实发工资:" << " ";cout<<objsalemana.OutputW age(0.99f, 3500, 2000, 0.20f, 10000, 0.20f, 150000) << endl;cout << endl;}(2)编译并运行该程序。
实验目的与要求:1.掌握类的继承与派生关系以及实验方法,理解类的层次结构。
2.掌握派生类构造函数初始化基类成员和对象成员的方法。
3.掌握内联函数和默认函数。
4.掌握赋值兼容原则,掌握派生类的复制构造函数和赋值运算符的定义。
实验过程及内容:1.实践教程实验二十二P81范例:定义一个继承与派生关系的类体系,在派生类中访问基类成员。
①先定义一个点类,包含x,y坐标数据成员,显示函数和计算面积的函数成员;②以点为基类派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数;③定义一个线段类,以两个点类对象作数据成员,定义显示、求面积及长度函数,线段类采用聚合方式,因为有两个端点,不能用派生。
编程测试所定义的类体系。
本实验教程中有源码,请自行运行,体会和熟悉继承与派生的基本概念及实现方法,掌握派生类构造函数初始化基类成员和对象成员的方法等。
2. 实践教程P83编程:多层派生练习,由上题Point类和Circle类继续派生出Cylinder类。
要求计算圆柱的底面积、侧面积、全面积和体积。
请编写所有完整的成员函数,并编写主函数进行验证。
数据处理1.(1)(2)j结果报错,原因是派生类中的成员函数不能访问基类中的私有成员。
(3)在Line类中添加两个数据成员。
2. #include <iostream>#include <cmath>using namespace std;#define PI 3.14159class Point{friend class Line;protected:double x, y ;public:Point(){x = 0 ; y = 0 ; }Point(double xv,double yv){ x = xv; y = yv; }double Area(){return 0;}void Show() {cout<<"x="<<x<<' '<<"y="<<y<<endl;}};class Circle :public Point{protected:double radius;public:Circle(){ x = 0; y = 0; radius = 0; }Circle(double xv,double yv,double vv):Point(xv,yv){ //调用基类构造函数radius = vv;}Circle(Circle & cir):Point(cir){ //按赋值兼容规则cir可为Point实参radius=cir.radius;}Circle & operator=(Circle & cir){this->Point::operator=(cir); //在派生类中定义重载的拷贝赋值操作符有固定的标准格式radius=cir.radius;return *this;}double Area(){return PI*radius*radius;}void Show()cout<<"x="<<x<<' '<<"y="<<y<<" radius="<<radius<<endl; //访问基类的数据成员}};class Cylinder:public Circle {double high;public:Cylinder(){ x = 0; y = 0; radius = 0;high=0; }Cylinder(double xv,double yv,double vv,double kv):Circle(xv,yv,vv){ //调用基类构造函数high=kv;}Cylinder(Cylinder & cyl):Circle(cyl){ //按赋值兼容规则cyl可为Cylinder实参high=cyl.high;}Cylinder & operator=(Cylinder & cyl){this->Circle :: operator=(cyl); //在派生类中定义重载的拷贝赋值操作符有固定的标准格式high=cyl.high;return *this;}double ceArea(){return 2*PI*radius*high;}double quArea(){return ceArea()+2* Area();}double volume(){return Area()*high;}void Show(){cout<<"x="<<x<<' '<<"y="<<y<<' '<<"radius="<<radius<<' '<<"high="<<high<<endl; //访问基类的数据成员};class Line{Point start,end; //对象成员public:Line(){} //对象成员初始化Line(double xv1,double yv1,double xv2,double yv2) :start(xv1,yv1),end(xv2,yv2){ }double GetLength() {return sqrt((start.x-end.x)*(start.x-end.x)+(start.y-end.y)*(start.y-end.y));}double Area(){return 0;}void Show(){cout<<"start point:\n";start.Show();cout<<"end point:\n";end.Show();}};int main(){Point pt(0,0);Circle cl1(100,100,10),cl2(cl1),cl3;Cylinder h1(50,50,20,30),h2(h1),h3;Line ln1(0,0,100,100),ln2;cout<<"点面积:"<<pt.Area()<<endl;pt.Show();cout<<"cl1圆面积:"<<cl1.Area()<<endl;cl1.Show();cout<<"cl2圆面积:"<<cl2.Area()<<endl;cl2.Show();cl3=cl1;cout<<"cl3圆面积:"<<cl3.Area()<<endl;cl3.Show();cout<<"h1底面积:"<<h1.Area()<<endl;cout<<"h1侧面积:"<<h1.ceArea()<<endl;cout<<"h1全面积:"<<h1.quArea()<<endl;cout<<"h1体积:"<<h1.volume()<<endl;h1.Show();cout<<"h2底面积:"<<h2.Area()<<endl;cout<<"h2侧面积:"<<h2.ceArea()<<endl;cout<<"h2全面积:"<<h2.quArea()<<endl;cout<<"h2体积:"<<h2.volume()<<endl;h2.Show();h3=h1;cout<<"h3底面积:"<<h3.Area()<<endl;cout<<"h3侧面积:"<<h3.ceArea()<<endl;cout<<"h3全面积:"<<h3.quArea()<<endl;cout<<"h3体积:"<<h3.volume()<<endl;h3.Show();cout<<"线面积:"<<ln1. Area()<<'\t'<<"线长度:"<<ln1. GetLength()<<endl;ln1.Show();ln2.Show();return 0;}实验结论:通过这次实验,我对类的继承和派生,派生类构造函数初始化基类成员和对象成员的方法,以及赋值兼容原则有了更深的理解。
实验四继承与派生一、实验目的:掌握利用单继承和多重继承的方式定义派生类的方法;深刻理解在各种继承方式下构造函数和析构函数的执行顺序;理解和掌握公有继承,私有继承和保护继承对基类成员的访问机制;理解虚基类的概念以及引入虚基类的目的和作用。
二、实验时间:三、实验地点:四、实验内容:1.运行以下程序,并对运行结果进行分析#include"stdafx.h"#include<iostream>using namespace std;class base{int n;public:base(int a){cout<<"constructing base class"<<endl; n=a;cout<<"n="<<n<<endl;}~base(){cout<<"desstructing base class"<<endl;}};class subs:public base{ base bobj;int m;public:subs(int a,int b,int c):base(a),bobj(c) {cout<<"constructing sub class"<<endl; m=b;cout<<"m="<<m<<endl;}~subs(){cout<<"destructing sub class"<<endl;}};void main(){subs s(1,2,3);}●subs s(1,2,3);subs(int a,int b,int c):base(a),bobj(c)所以a=1,b=2,c=3,●class subs:public base,又因为base(a),所以先调用base 传一个参数的构造函数base(int a),(若此处删去base(a),则会出现如下错误)●调用base(int a),所以有●然后,因为base bobj;所以这里还会调用base(int a)构造函数,此时传入的a值为bobj(c)中的c,即3,所以有●再者,才会调用subs的构造函数subs(int a,int b,intc):base(a),bobj(c)●相反,在调用析构函数的时候,顺序刚刚好相反,则~base(){cout<<"desstructing base class==>:"<<n<<endl;}~subs(){cout<<"destructing sub class==>:"<<m<<endl;}2.设计一个圆类circle和一个桌子类table,另外设计一个圆桌类roundtable,从前2个类派生的,要求输出一个圆桌的高度,面积和颜色等数据。
.继承和派生一、实验目的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. 掌握派生类构造函数的定义方法。
4. 掌握多重继承的方法。
5. 掌握虚基类的使用。
6. 掌握基类与派生类的转换关系。
二. 实验类型:验证型实验和设计型实验三. 验证型实验内容:1.编写运行下面程序,体会继承与派生的概念,学习派生类声明的方式并了解派生类的构成。
#include <iostream>#include <string>using namespace std;class Person //声明基类{public:void set_person(char[],int,char);void display_person( );private :char name[20];int age;char sex;};void Person::set_person(char na[],int a,char s){strcpy(name,na);age=a;sex=s;}void Person::display_person( ){ cout<<"name:"<<name<<endl;cout<<"age:"<<age<<endl;cout<<"sex:"<<sex<<endl;}class Student: public Person //声明派生类{ public:void set_student(char[],int,char,int,char[],int); //派生类新增加成员函数void display_student( ); //派生类新增加成员函数private:int num; //派生类新增加数据成员char speciality[20]; //派生类新增加数据成员int grade; //派生类新增加数据成员};void Student::set_student(char na[],int a,char s,int n,char sp[],int g)//设置派生类中全部数据成员{set_person(na,a,s); //调用派生类继承的基类成员函数num=n;strcpy(speciality,sp);grade=g;}void Student::display_student( ) //显示派生类中全部数据成员{ display_person( ); //调用派生类继承的基类成员函数cout<<"num:"<<num<<endl;cout<<"speciality:"<<speciality<<endl;cout<<"grade:"<<grade<<endl;}int main( ){Student s; //定义派生类对象s.set_student("wang",21,'m',20060701,"JAVA",2);s.display_student();return 0;}提示:在开发环境中,通过在派生类对象后面输入成员访问运算符可以看到派生类中的成员列表,了解派生类的构成。
实验三继承和派生类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,基类中可以存放一组整数。
班级:计科(2)班学号:201013137183 姓名:王于铭完成时间:2011-4-21实验四继承与派生[实验目的]1、学习定义和使用类的继承关系,定义派生类;2、熟悉不同继承方式下对集类成员的访问控制;3、学习和利用虚基类解决二义性问题;[实验内容与步骤]4、设计一个人事管理的“people(人员)”基类。
考虑到通用性,仅只抽象出所有类型人员都具有的属性:编号、姓名、性别、出生日期、身份证号等;从people(人员)类派生出student(学生)类,并添加属性:班号class no;从people(人员)类派生teacher(教师)类,并添加属性:职务principalship、部门department;从student类中派生出graduate(研究生)类,添加属性:专业subject、导师teacher adviser (teacher类);从graduate类和teacher类派生出TA(助教生)类。
设计时注意虚基类的使用,注意重载相应的成员函数。
测试这些类。
[源代码]#include<iostream>#include<string>using namespace std;class Date{private:int year,month,day;public:Date(int y=2010,int m=1,int d=1){year=y;month=m;day=d;}~Date(){}Date(Date &p);int Get_Date(){cout<<"出生年月:"<<year<<"-"<<month<<"-"<<day;return 1;}void Set_Date(int y,int m,int d){year=y;month=m;day=d;}};Date::Date(Date &p){year=p.year;month=p.month;day=p.day;}class people{private:string name,idcard,num;char sex;Date a;public:people(string n,string i,string m,char s,Date b):a(b){name=n; num=m;idcard=i;sex=s;}~people(){}void Set_name(string n){name=n;}void Set_idcard(string i){idcard=i;}void Set_num(string m){num=m;}void Set_sex(char s){sex=s;}string Get_name(){return name;}string Get_idcard(){return idcard;}string Get_num(){return num;}char Get_sex(){return sex;}void Set_people(string n,string i,string m,char s,int y,int mon,int d){Set_name(n);Set_idcard(i);Set_num(m);Set_sex(s);a.Set_Date(y,mon,d);}void Get_people(){cout<<"编号: "<<Get_num()<<" 姓名: "<<Get_name()<<" 身份证号: "<<Get_idcard()<<" 性别: "<<Get_sex()<<" ";a.Get_Date();cout<<endl;}};class student:virtual public people{private:string classNO;public:student(string n,string i,string m,char s,Date b,string c):people(n,i,m,s,b),classNO(c){}~student(){}void Set_classNO(string c){classNO=c;}void Get_student(){Get_people();cout<<"classNO: "<<classNO<<endl;}void Set_student(string n,string i,string m,char s,int y,int mon,int d,string c){Set_people(n,i,m,s,y,mon,d);Set_classNO(c);}};class teacher:virtual public people{private:string principalship;string department;public:teacher(string n,string i,string m,char s,Date b,string p,string depart):people(n,i,m,s,b){principalship=p;department=depart;}~teacher(){}void Set_principalship(string p){principalship=p;}void Set_department(string d){department=d;}void Set_teacher(string n,string i,string m,char s,int y,int mon,int d,string p,string depart){Set_people(n,i,m,s,y,mon,d);Set_principalship(p);Set_department(depart);}void Get_teaher(){people::Get_people();cout<<"职务: "<<principalship<<" 部门: "<<department<<endl;}};class graduate:virtual public student{private:string subject;teacher teacher_adviser;//这是一个类!public:graduate(string n,string i,string m,char s,Date b,string c,string sub,teacher t):student(n,i,m,s,b,c),people(n,i,m,s,b),teacher_adviser(t){subject=sub;}~graduate(){}string Get_subject(){return subject;}void Get_graduate(){student::Get_student();cout<<"subject: "<<Get_subject()<<endl;teacher_adviser.Get_teaher();}void Set_subject(string sub){subject=sub;}void Set_grduate(string n,string i,string m,char s,int y,int mon,int d,string c,string sub,teacher t){Set_student(n,i,m,s,y,mon,d,c);Set_subject(sub);//teacher_adviser.Set_teacher(n,i,m,s,y,mon,d,p,sdepart);}};class TA:virtual public graduate,public teacher{public:TA(string n,string i,string m,char s,Date b,string c,string sub,teacher t,string p,string depart):student(n,i,m,s,b,c),graduate(n,i,m,s,b,c,sub,t),people(n,i,m,s,b),teacher(n,i,m,s,b,p,depart){} ~TA(){}void Get_TA(){student::Get_student();cout<<"subject: "<<Get_subject()<<endl;teacher::Get_teaher();}// void Set_TA(){Set_grduate();Set_teacher();}};void main(){Date a(2011,4,21);student b("韩笑","201012","201013138012",'M',a,"1001");b.Get_student();cout<<endl;teacher c("张毅","201012","201013138012",'M',a,"教授","2222");c.Get_teaher();cout<<endl;graduate d("赵军","201012","201013138012",'M',a,"2001","c++",c);d.Get_graduate();cout<<endl;TA e("王于铭","201012","201013138012",'M',a,"1001","c++",c,"教授","2022");e.Get_TA();cout<<endl;}[实验截图][实验小结]本程序大体能完成实验目的,框架已经构架好,但是程序的内容需要不断完善。
计算机科学与技术学院程序设计报告程序名称:继承与派生(一)专业:计算机科学与技术班级:计算机1103班学号:姓名:指导老师:设计日期:2012年4月13日实验四继承与派生(一)[实验目的]1、掌握类继承与派生关系以及实现方法,理解类的层次结构;2、掌握派生类构造函数初始化基类成员和对象成员的方法;3、掌握赋值兼容原则,掌握派生类的复制构造函数的定义。
[实验内容与步骤]题目:由例题1中的point类和circle类继续派生出cylinder,求其表面积Area.源程序代码:#include<iostream>using namespace std;const double PI=3.14159;class point //定义point类{protected:double x,y;public:point(){x=0;y=0;}point(double xv,double yv){x=xv;y=yv;}double area(){return 0;}void show(){cout<<"("<<x<<","<<y<<") ";}};class circle:public point //公有继承point类,派生出circle类{protected:double radius;public:circle(){x=0;y=0;radius=0;}circle(double xv,double yv,double vv):point(xv,yv){radius=vv;}circle(point p,double vv):point(p){radius=vv;}circle(circle &cir):point(cir){radius=cir.radius;}double area(){return PI*radius*radius;}void show(){point::show();cout<<"radius="<<radius<<endl;}};class cylinder:public circle //公有继承circle类,派生出cylinder类{protected:double high;public:cylinder(){x=0;y=0;radius=0;high=0;}cylinder(double xv,double yv,double vv,double hv):circle(xv,yv,vv) {high=hv;}cylinder(cylinder &cyl):circle(cyl){high=cyl.high;}double area(){return(2*PI*radius*high+2*circle::area());}void show(){cout<<"圆柱体信息:"<<endl;circle::show();cout<<"high="<<high<<endl;}};int main() //主函数的定义{point point1(2,5);circle circle1;circle circle2(1,2,3);circle circle3(point1,4);circle1.show();circle2.show();circle3.show();cylinder cylinder1(5,6,11,20);cylinder1.show();cout<<"圆柱体面积:"<<cylinder1.area()<<endl; }实验截图:[实验体会]1、掌握类的继承与派生的实现方法;2、定义合适的派生类构造函数,用于初始化基类成员和对象成员;3、要理解赋值兼容性原则,掌握派生类的复制构造函数的定义方法;4、在派生类中可以通过基类名调用基类的成员。
实验二类的继承与派生实验二类的继承与派生一、实验目的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.掌握内联函数和默认函数。
4.掌握赋值兼容原则,掌握派生类的复制构造函数和赋值运算符的定义。
实验过程及内容:1.实践教程实验二十二P81范例:定义一个继承与派生关系的类体系,在派生类中访问基类成员。
①先定义一个点类,包含x,y坐标数据成员,显示函数和计算面积的函数成员;②以点为基类派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数;③定义一个线段类,以两个点类对象作数据成员,定义显示、求面积及长度函数,线段类采用聚合方式,因为有两个端点,不能用派生。
编程测试所定义的类体系。
本实验教程中有源码,请自行运行,体会和熟悉继承与派生的基本概念及实现方法,掌握派生类构造函数初始化基类成员和对象成员的方法等。
2. 实践教程P83编程:多层派生练习,由上题Point类和Circle类继续派生出Cylinder类。
要求计算圆柱的底面积、侧面积、全面积和体积。
请编写所有完整的成员函数,并编写主函数进行验证。
数据处理1.(1)(2)j结果报错,原因是派生类中的成员函数不能访问基类中的私有成员。
(3)在Line类中添加两个数据成员。
2. #include <iostream>#include <cmath>using namespace std;#define PI 3.14159class Point{friend class Line;protected:double x, y ;public:Point(){x = 0 ; y = 0 ; }Point(double xv,double yv){ x = xv; y = yv; }double Area(){return 0;}void Show() {cout<<"x="<<x<<' '<<"y="<<y<<endl;}};class Circle :public Point{protected:double radius;public:Circle(){ x = 0; y = 0; radius = 0; }Circle(double xv,double yv,double vv):Point(xv,yv){ //调用基类构造函数radius = vv;}Circle(Circle & cir):Point(cir){ //按赋值兼容规则cir可为Point实参radius=cir.radius;}Circle & operator=(Circle & cir){this->Point::operator=(cir); //在派生类中定义重载的拷贝赋值操作符有固定的标准格式radius=cir.radius;return *this;}double Area(){return PI*radius*radius;}void Show()cout<<"x="<<x<<' '<<"y="<<y<<" radius="<<radius<<endl; //访问基类的数据成员}};class Cylinder:public Circle {double high;public:Cylinder(){ x = 0; y = 0; radius = 0;high=0; }Cylinder(double xv,double yv,double vv,double kv):Circle(xv,yv,vv){ //调用基类构造函数high=kv;}Cylinder(Cylinder & cyl):Circle(cyl){ //按赋值兼容规则cyl可为Cylinder实参high=cyl.high;}Cylinder & operator=(Cylinder & cyl){this->Circle :: operator=(cyl); //在派生类中定义重载的拷贝赋值操作符有固定的标准格式high=cyl.high;return *this;}double ceArea(){return 2*PI*radius*high;}double quArea(){return ceArea()+2* Area();}double volume(){return Area()*high;}void Show(){cout<<"x="<<x<<' '<<"y="<<y<<' '<<"radius="<<radius<<' '<<"high="<<high<<endl; //访问基类的数据成员};class Line{Point start,end; //对象成员public:Line(){} //对象成员初始化Line(double xv1,double yv1,double xv2,double yv2) :start(xv1,yv1),end(xv2,yv2){ }double GetLength() {return sqrt((start.x-end.x)*(start.x-end.x)+(start.y-end.y)*(start.y-end.y));}double Area(){return 0;}void Show(){cout<<"start point:\n";start.Show();cout<<"end point:\n";end.Show();}};int main(){Point pt(0,0);Circle cl1(100,100,10),cl2(cl1),cl3;Cylinder h1(50,50,20,30),h2(h1),h3;Line ln1(0,0,100,100),ln2;cout<<"点面积:"<<pt.Area()<<endl;pt.Show();cout<<"cl1圆面积:"<<cl1.Area()<<endl;cl1.Show();cout<<"cl2圆面积:"<<cl2.Area()<<endl;cl2.Show();cl3=cl1;cout<<"cl3圆面积:"<<cl3.Area()<<endl;cl3.Show();cout<<"h1底面积:"<<h1.Area()<<endl;cout<<"h1侧面积:"<<h1.ceArea()<<endl;cout<<"h1全面积:"<<h1.quArea()<<endl;cout<<"h1体积:"<<h1.volume()<<endl;h1.Show();cout<<"h2底面积:"<<h2.Area()<<endl;cout<<"h2侧面积:"<<h2.ceArea()<<endl;cout<<"h2全面积:"<<h2.quArea()<<endl;cout<<"h2体积:"<<h2.volume()<<endl;h2.Show();h3=h1;cout<<"h3底面积:"<<h3.Area()<<endl;cout<<"h3侧面积:"<<h3.ceArea()<<endl;cout<<"h3全面积:"<<h3.quArea()<<endl;cout<<"h3体积:"<<h3.volume()<<endl;h3.Show();cout<<"线面积:"<<ln1. Area()<<'\t'<<"线长度:"<<ln1. GetLength()<<endl;ln1.Show();ln2.Show();return 0;}实验结论:通过这次实验,我对类的继承和派生,派生类构造函数初始化基类成员和对象成员的方法,以及赋值兼容原则有了更深的理解。
指导教师批阅意见:成绩评定:指导教师签字:年月日备注:注:1、报告内的项目或内容设置,可根据实际情况加以调整和补充。