实验4 继承与派生-程序

合集下载

实验四 继承与派生1(报告)

实验四 继承与派生1(报告)




Visual Studio 2010




编写程序计算出球、圆柱、圆锥,都含有求表面积和体积。
要求:
(1)定义一个基类圆类至少含有一个数据成员半径;
(2)定义基类成员的派生类:球、圆柱、圆锥,都含有球表面积和体积的成员函数和输出函数。
(3)在主函数中求球、圆柱、圆锥的表面积和体积。









1、基类圆的定义
ห้องสมุดไป่ตู้如上图,包含半径、周长、面积,能够输出圆的基本信息。
2、派生类的定义(球、圆柱、圆锥)
派生类球(Ball)的定义,公有继承Circle,增加变量表面积S与体积V及输出函数。
派生类圆柱(Column)公有继承Circle,增加自己的属性变量高high及表面积S、体积V,输出函数。
派生类圆锥(Cone)也是公有继承Circle,增加高high、表面积S、体积V和输出函数。
如上图主函数。




第一次运行结果:
第二次运行结果:










程序编译一次成功了,但运行结果不对。原因是在计算圆锥体积和球的体积时要用到两个分数1/3、4/3参与运算,但在计算机中这两个分数是由两个整形变量计算而来的,其结果也是一个整形变量(0和1),所以运行结果错误,将其改成1.0/3与4.0/3即可,这样就是两个float型变量计算,结果也是我们要的结果了。
课程名称
C++程序设计A2
班级
1420561

实验四 继承与派生

实验四    继承与派生

实验四继承与派生一、实验目的:掌握利用单继承和多重继承的方式定义派生类的方法;深刻理解在各种继承方式下构造函数和析构函数的执行顺序;理解和掌握公有继承,私有继承和保护继承对基类成员的访问机制;理解虚基类的概念以及引入虚基类的目的和作用。

二、实验时间:三、实验地点:四、实验内容: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个类派生的,要求输出一个圆桌的高度,面积和颜色等数据。

实验四:派生类和继承(一)

实验四:派生类和继承(一)

福建农林大学实验报告实验4 派生类和继承(一)一、实验目的和要求(1)掌握派生类的声明与定义方法,进一步理解类的继承的概念,能够定义和使用类的继承关系。

(2)熟悉公有派生和私有派生的访问特性。

二、实验内容和原理1、(1)定义一个基类Animal,该类具有私有整型成员变量age,weight,构造派生类Dog公有继承Animal。

Dog类新增私有成员变量color,新增成员函数SetAge(int n)中直接给age赋值,新增成员函数SetWeight(int m)中直接给weight赋值,查看编译结果,并分析结果。

(2)将类Anima l中的age和weight为公有成员,重做第一步,并分析结果。

(3)将类Animal中的age和weight 为保护成员,重做第一步,并分析结果。

(4)将派生类Dog的继承方式改为私有继承方式和保护继承方式重做以上各小题,并分析结果。

2、程序分析题(写出程序的输出结果,并分析结果)。

三、实验环境1. 硬件:PC机;2. 软件:Windows操作系统、Visual C++ 6.0四、算法描述及实验步骤1、(1):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<"Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";cin>>y;cout<<endl;cout<<"The dog's age is "<<a.SetAge(x)<<endl;cout<<"The dog's age weight is"<<a.SetWeight(y)<<endl; return 0;}(2):#include <iostream.h>class animal{public:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";return 0;}(3):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";cin>>y;cout<<endl;cout<<" The dog's age is "<<a.SetAge(x)<<endl;cout<<" The dog's age weight is "<<a.SetWeight(y)<<endl; return 0;}(4):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";return 0;}(5):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";cin>>y;cout<<endl;cout<<" T The dog's age is "<<a.SetAge(x)<<endl;cout<<" The dog's age weight is "<<a.SetWeight(y)<<endl; return 0;}(6):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";return 0;}(7):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";cin>>y;cout<<endl;cout<<" The dog's age is "<<a.SetAge(x)<<endl;cout<<" The dog's age weight is "<<a.SetWeight(y)<<endl; return 0;}(8):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";return 0;}(9):#include <iostream.h>class animal{private:int age,weight;};class dog:public animal{private:char color[10];public:int SetAge(int n){age=n;return n;}int SetWeight (int m){weight=m;return m; }};int main(){ int x,y;dog a;cout<<" Please input dog's age :";cin>>x;cout<<endl;cout<<" Please input dog's weight:";cin>>y;cout<<endl;cout<<" The dog's age is "<<a.SetAge(x)<<endl;cout<<" The dog's age weight is "<<a.SetWeight(y)<<endl; return 0;}2#include<iostream.h>class A{public: A(int i,int j){a=i;b=j;}void move(int x,int y){a+=x;b+=y;}void display(){cout<<"("<<a<<","<<b<<")"<<endl;} private:int a,b;};class B:public A{public: B(int i,int j,int k,int l):A(i,j),x(k),y(l){}void display(){cout<<x<<","<<y<<endl;}void fun1(){move(13,15);}void fun2(){A::display();}void fun3(){display();}private:int x,y;int main(){A aa(2,4);aa.display();B bb(5,6,7,8);bb.fun1();bb.fun2();bb.fun3();bb.A::display();bb.B::display();return 0;}五、调试过程1、调试程序,截图如下:原因分析:在public继承中void display 中的display打错成diaplay。

C++ 实验四 继承和派生

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 ();}五、总结通过本次上机实践,我学会了类的定义及运用,以及在定义的类中各种函数的用法,兵使其得到实际运用。

实验四 继承与派生(学生)

实验四  继承与派生(学生)

实验四:继承与派生(2)一、实验目的和要求(1)理解单继承与多继承,掌握多继承派生类的声明方式。

(2)掌握多继承派生类构造函数的定义的一般形式。

(3)掌握多继承构造函数与析构函数的执行顺序。

(4)掌握含有对象成员的派生类构构造函数的特点。

(5)理解类的继承可能发生的“二义性”问题(同名成员的访问)二、知识点回顾多继承构造函数定义的一般形式如下:派生类名(参数总表):基类名1(参数表1),基类名2(参数表2),…,基类名n(参数表n) {// 派生类新增成员的初始化语句}class B: public A class D:public A, public B, public C{ {B(int i,int j):A( i){ ….. D(int i,int j,int k, int l):A( i),B(j ),C( k),ob(l ) } { …….}}; };…….D obj(1,2,3,4);多重继承构造函数的执行顺序与单继承下的构造函数执行顺序相同:(1)先执行基类的构造函数。

(2)再执行对象成员的构造函数。

(3)最后执行派生类的构造函数体。

说明:处于同一层的各个基类的构造函数的执行顺序, 取决于声明派生类时所指定的各个基类的顺序, 与派生类构造函数中所定义的成员初始化列表中的顺序并没有关系.说明:派生类构造函数后如未列出基类构造函数的调用,默认调用的是无参或带默认参数的构造函数。

三、实验内容1. P191题4.15输入下列程序,分析程序运行结果........。

理解多重继承(画出类的层次结构图),注意派生类构造函数的定义格式,注意构造函数的执行顺序,注意同名成员的访问。

#include<iostream>#include<string>using namespace std;class B1{int b1;public:B1(int i){b1=i;cout<<"Construcor B1."<<i<<endl;}void print(){ cout<<b1<<endl;}};class B2{int b2;public:B2(int i){b2=i;cout<<"Construcor B2."<<i<<endl;}void print(){ cout<<b2<<endl;}};class B3{int b3;public:B3(int i){b3=i;cout<<"Construcor B1."<<i<<endl;}int getb3(){ return b3;}};class A:public B2,public B1{int a;B3 bb;public:A(int i,int j,int k,int l):B1(i),B2(j),bb(k) //此处注意派生类构造函数的定义格式{a=l;cout<<"Constructor A."<<l<<endl;}void print() //{ B1::print(); //同名成员的访问,注意消除二义性B2::print(); //同名成员的访问cout<<a<<","<<bb.getb3()<<endl;}};int main(){ A aa(1,2,3,4);aa.print(); //分析调用的是哪个print(). 同名覆盖(将基类的同名成员隐藏)return 0;}在以上程序的基础上,为每个类增加一个析构函数,析构函数体内容“Destructor X”,其中X写成具体的类名。

实验四 继承与派生

实验四  继承与派生

实验四继承与派生一. 实验目的: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;}提示:在开发环境中,通过在派生类对象后面输入成员访问运算符可以看到派生类中的成员列表,了解派生类的构成。

继承与派生实验报告

继承与派生实验报告

计算机科学与技术学院程序设计报告程序名称:继承与派生(一)专业:计算机科学与技术班级:计算机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、在派生类中可以通过基类名调用基类的成员。

实验四 C++中的继承与派生

实验四  C++中的继承与派生

实验四C++中的继承与派生一、实验目的:1.理解C++中继承与派生的概念;2.掌握C++中各种不同继承方式的访问属性差别;3.掌握单继承与多继承的实现方法;4.掌握派生类构造函数与析构函数的实现及调用顺序;5.掌握虚基类的使用方法。

二、实验任务【题目】小型公司人员管理某小型公司有四类人员:经理、技术人员、销售经理、销售员。

设计一个基类employee,派生出manager(经理)、technician(技术人员)、salesmanager(销售经理)、saleman(销售员)。

销售经理既是经理又是销售员,兼具两类人员的特点,因此同时继承manager 和salesman 两个类。

1、类定义1)employee 类:基本信息:编号、姓名、性别、出生日期、职位、薪水等;出生日期使用自定义的Date (日期)类;2)Date 类:成员变量:年、月、日3)派生类technician:新增属性:工作时间派生类saleman:新增属性:销售额、所属部门2、实现人员信息的录入与显示;3、计算并显示个人月薪:月薪计算办法:总经理拿固定月薪8000 元,技术人员按每小时25 元领取月薪;推销员的月薪按当月销售额的4%提成;销售经理固定月薪5000 元加所管辖部门当月销售总额的5‰。

【提示】1、在基类中,除了定义构造函数和析构函数,还应统一定义对各类人员信息应有的操作,规范类族中各派生类的基本行为,但是各类人员的月薪计算方法不同,不能在基类employee 中统一确定计算方法。

各类人员信息的显示内容不同,同样不能在基类employee中统一确定显示方法。

在基类中实现上述功能的函数体应为空,在派生类中根据同名覆盖原则定义各自的同名函数实现具体功能。

代码:#include<iostream>#include<string>using namespace std;class Date{private:int year;int month;int day;public:void SetYear(int x) {year=x;}void SetMonth(int x) {month=x;}void SetDay(int x) {day=x;}int GetYear() {return year;}int GetMonth() {return month;}int GetDay() {return day;}};class employee{protected:int num;string nam;string sex;Date birth;string position ;float salary;public:void display(){cout<<"编号:"<<num<<"姓名:"<<nam<<"性别:"<<sex<<"出生日期:"<<birth. GetYear()<<"-"<<birth.GetMonth()<<"-"<<birth.GetDay()<<"职位:"<<position;}};class manager:virtual public employee{public:void set_manager(int n,string na,string se,int y,int m,int d,string pos){num=n;nam=na;sex=se;birth.SetYear(y);birth.SetMonth(m);birth.SetDay(d);position=pos;}void display(){employee::display();}void total_salary(){ salary=8000;cout<<"经理月薪:";cout<< salary<<"元";}};class technician:virtual public employee{protected:float Time;public:float t;void set_technician(int n,string na,string se,int y,int m,int d,string pos,float t) { num=n;nam=na;sex=se;birth.SetYear(y);birth.SetMonth(m);birth.SetDay(d);position=pos;Time=t;}void display(){ employee::display();cout<<"工作时间:"<<Time<<endl;}void total_salary(){salary= 25*Time;cout<<"技术人员月薪:" ;cout<< salary<<"元";}};class saleman :virtual public employee{protected:float sale;string dep;public:void set_saleman(int n,string na,string se,int y,int m,int d,string pos,float sale1,string de) {num=n;nam=na;sex=se;birth.SetYear(y);birth.SetMonth(m);birth.SetDay(d);position=pos;sale=sale1;dep=de;}void display(){employee::display();cout<<position<<"销售额:"<<sale<<"所属部门:"<<dep<<endl;}void total_salary (){salary=sale*0.04;cout<<"销售员月薪:"<<salary<<"元";}};class salesmanager:public manager,public saleman{public:void total_salary (){salary=sale*0.05+5000;cout<<"销售经理月薪:" << salary<<"元";}};int main (){manager m1;technician tec;saleman s;salesmanager sg;int n,y,m,d,choose,choose1;string str,se, p;cout<<"\n\t\t================================================="<<endl;cout<<"\t\t1:输入信息与显示2:个人月薪0:退出"<<endl;cout<<"\t\t=================================================="<<endl;cout<<"\n输入您要进行的操作:";cin>>choose;while(choose!=0){switch(choose){case 1:cout<<"输入员工编号:";cin>>n;cout<<"输入员工出生日期:";cin>>y>>m>>d;cout<<"输入员工姓名:";cin>>str;cout<<"输入员工性别:";cin>>se;cout<<"输入员工职位:";cin>>p;if (p=="manager"){m1.set_manager( n, str, se,y,m,d,p);m1.display();}else if(p=="technician"){float t;cout<<"输入时间:";cin>>t;tec.set_technician( n,str, se,y,m,d, p, t) ;tec.display();}else if(p=="saleman"){float sale2;string de;cout<<"销售额:" ;cin >> sale2;cout<<"所属部门:";cin>>de ;s.set_saleman( n, str, se,y,m,d, p, sale2, de);s.display();}else{float sale1;string de;cout<<"销售额:" ;cin>>sale1;cout<<"所属部门:";cin>>de ;sg.set_saleman( n,str, se,y,m,d, p, sale1, de) ;s.display();}break;case 2:cout<<"\n\t\t===================================================="<<endl;cout<<"\t\t11: 经理12: 技术人员13:销售员14:销售经理"<<endl;cout<<"\t\t============================================================"< <endl;cout<<"\n输入您要进行的操作:";cin>>choose1;while(choose1!=0){ switch(choose1){case 11:m1.total_salary();break;case 12:tec.total_salary();break;case 13:s.total_salary();break;case 14:sg.total_salary();break;}choose1=0;}break;}cout<<"\n\t\t=================================================="<<endl;cout<<"\t\t1:输入信息与显示2:个人月薪0:退出"<<endl;cout<<"\t\t=================================================="<<endl;cout<<"\n输入您要进行的操作:";cin>>choose;}return 0;}。

继承与派生实验报告

继承与派生实验报告

继承与派生实验报告继承与派生实验报告引言:继承与派生是面向对象编程中的重要概念,通过继承,一个类可以派生出子类,从而实现代码的复用和扩展。

本文将通过实验来探讨继承与派生的概念、原理和应用。

实验目的:1. 理解继承与派生的概念和原理;2. 掌握如何在编程语言中实现继承和派生;3. 熟悉继承与派生的应用场景。

实验步骤:1. 创建父类:首先,我们创建一个名为"Animal"的父类,该类具有属性和方法,例如"age"和"eat()"。

2. 创建子类:接下来,我们创建一个名为"Cat"的子类,该类继承自"Animal"类。

在子类中,我们可以重写父类的方法或添加新的方法。

3. 实例化对象:通过实例化父类和子类的对象,我们可以调用它们的方法和访问它们的属性。

4. 测试继承与派生:我们可以通过调用父类和子类的方法,观察它们的行为是否符合预期。

实验结果:在创建父类"Animal"时,我们定义了一个"age"属性和一个"eat()"方法。

在创建子类"Cat"时,我们继承了父类的属性和方法,并添加了一个新的"meow()"方法。

在实例化父类对象时,我们可以通过调用"eat()"方法来模拟动物进食的行为。

而在实例化子类对象时,我们既可以调用从父类继承而来的"eat()"方法,也可以调用子类特有的"meow()"方法来模拟猫咪的叫声。

通过实验,我们发现继承与派生的优势在于代码的复用和扩展。

我们只需在父类中定义一次通用的属性和方法,然后让不同的子类继承父类,即可实现代码的复用。

同时,子类还可以通过重写父类的方法或添加新的方法,实现代码的扩展和个性化。

讨论与应用:继承与派生不仅仅局限于上述的父类和子类关系,它还可以在多层次的继承结构中发挥作用。

C实验报告-实验4继承与派生

C实验报告-实验4继承与派生

实验4 继承与派生一、实验目的和要求(1)掌握派生类的定义方法和派生类构造函数的定义方法。

(2)掌握不同继承方式的情况下,基类成员在派生类中的访问权限。

(3)掌握在多继承方式的情况下,构造函数与析构函数的调用时机与顺序。

二、实验内容和原理(1)实验指导书P86 1~2任选一题。

(2)实验指导书P89 3~4任选一题。

三、实验环境联想计算机,Windows XP操作系统,Visual C++ 6.0四、算法描述及实验步骤(1)输入源程序。

(2)检查程序有无错误(包括语法错误和逻辑错误),有则改之。

(3)编译和连接,仔细分析编译信息,如有错误应找出原因并改正之。

(4)运行程序,分析结果,在原有程序得出正确结果后,修改程序,将其改写为在类模板外定义,再按第(3)步骤运行。

(5)将调试好的程序保存在自己的用户目录中,文件名自定。

五、调试过程(1)2(2)4六、实验结果(1)2(2)4七、总结(1)掌握构造函数和析构函数的定义方法(2)构造函数调用顺序:先调用所有基类的构造函数,然后调用派生类的构造函数;(3)析构函数调用顺序:先调用派生类的析构函数,然后调用基类的析构函数,其顺序正好与构造函数调用顺序相反。

八、附录:(1)代码如下#include<iostream>using namespace std;class Base1{public:Base1(){cout<<"constructing Base1"<<endl;}~Base1(){cout<<"destructing Base1"<<endl;}};class Base2{public:Base2(){cout<<"constructing Base2"<<endl;}~Base2(){cout<<"destructing Base2"<<endl;}};class Derived1:public Base1,virtual public Base2{public:Derived1(){cout<<"constructing Derived1"<<endl;}~Derived1(){cout<<"destructing Derived1"<<endl;}};class Derived2:public Base1,virtual public Base2{public:Derived2(){cout<<"constructing Derived2"<<endl;}~Derived2(){cout<<"destructing Derived2"<<endl;}};class Derived3:public Derived1,virtual public Derived2 {public:Derived3(){cout<<"constructing Derived3"<<endl;}~Derived3(){cout<<"destructing Derived3"<<endl;}};int main(){Derived3 obj;return 0;}(2)4代码如下#include<iostream>using namespace std;const double PI=3.14;class Shape{public:double area()const{return 0.0;}void display(){};};class TwoDimShape:virtual public Shape{};class ThreeDimShape:virtual public Shape{};class Circle:public TwoDimShape{public:Circle(double myr){R=myr;}double area()const{return PI*R*R;}void display(){cout<<"Area of circle is ";}private:double R;};class Rectangle:public TwoDimShape{public:Rectangle(double myl,double myw){L=myl;W=myw;}double area()const{return L*W;}void display(){cout<<"Area of rectangle is";}。

实验四 C++中的继承与派生

实验四  C++中的继承与派生

实验四C++中的继承与派生一、实验目的:1.理解C++中继承与派生的概念;2.掌握C++中各种不同继承方式的访问属性差别;3.掌握单继承与多继承的实现方法;4.掌握派生类构造函数与析构函数的实现及调用顺序;5.掌握虚基类的使用方法。

二、实验任务【题目】小型公司人员管理某小型公司有四类人员:经理、技术人员、销售经理、销售员。

设计一个基类employee,派生出manager(经理)、technician(技术人员)、salesmanager(销售经理)、saleman(销售员)。

销售经理既是经理又是销售员,兼具两类人员的特点,因此同时继承manager 和salesman 两个类。

1、类定义1)employee 类:基本信息:编号、姓名、性别、出生日期、职位、薪水等;出生日期使用自定义的Date (日期)类;2)Date 类:成员变量:年、月、日3)派生类technician:新增属性:工作时间派生类saleman:新增属性:销售额、所属部门2、实现人员信息的录入与显示;3、计算并显示个人月薪:月薪计算办法:总经理拿固定月薪8000 元,技术人员按每小时25 元领取月薪;推销员的月薪按当月销售额的4%提成;销售经理固定月薪5000 元加所管辖部门当月销售总额的5‰。

【提示】1、在基类中,除了定义构造函数和析构函数,还应统一定义对各类人员信息应有的操作,规范类族中各派生类的基本行为,但是各类人员的月薪计算方法不同,不能在基类employee 中统一确定计算方法。

各类人员信息的显示内容不同,同样不能在基类employee中统一确定显示方法。

在基类中实现上述功能的函数体应为空,在派生类中根据同名覆盖原则定义各自的同名函数实现具体功能。

代码:#include<iostream>#include<string>using namespace std;class Date{private:int year;int month;int day;public:void SetYear(int x) {year=x;}void SetMonth(int x) {month=x;}void SetDay(int x) {day=x;}int GetYear() {return year;}int GetMonth() {return month;}int GetDay() {return day;}};class employee{protected:int num;string nam;string sex;Date birth;string position ;float salary;public:void display(){cout<<"编号:"<<num<<"姓名:"<<nam<<"性别:"<<sex<<"出生日期:"<<birth. GetYear()<<"-"<<birth.GetMonth()<<"-"<<birth.GetDay()<<"职位:"<<position;}};class manager:virtual public employee{public:void set_manager(int n,string na,string se,int y,int m,int d,string pos){num=n;nam=na;sex=se;birth.SetYear(y);birth.SetMonth(m);birth.SetDay(d);position=pos;}void display(){employee::display();}void total_salary(){ salary=8000;cout<<"经理月薪:";cout<< salary<<"元";}};class technician:virtual public employee{protected:float Time;public:float t;void set_technician(int n,string na,string se,int y,int m,int d,string pos,float t) { num=n;nam=na;sex=se;birth.SetYear(y);birth.SetMonth(m);birth.SetDay(d);position=pos;Time=t;}void display(){ employee::display();cout<<"工作时间:"<<Time<<endl;}void total_salary(){salary= 25*Time;cout<<"技术人员月薪:" ;cout<< salary<<"元";}};class saleman :virtual public employee{protected:float sale;string dep;public:void set_saleman(int n,string na,string se,int y,int m,int d,string pos,float sale1,string de) {num=n;nam=na;sex=se;birth.SetYear(y);birth.SetMonth(m);birth.SetDay(d);position=pos;sale=sale1;dep=de;}void display(){employee::display();cout<<position<<"销售额:"<<sale<<"所属部门:"<<dep<<endl;}void total_salary (){salary=sale*0.04;cout<<"销售员月薪:"<<salary<<"元";}};class salesmanager:public manager,public saleman{public:void total_salary (){salary=sale*0.05+5000;cout<<"销售经理月薪:" << salary<<"元";}};int main (){manager m1;technician tec;saleman s;salesmanager sg;int n,y,m,d,choose,choose1;string str,se, p;cout<<"\n\t\t================================================="<<endl;cout<<"\t\t1:输入信息与显示2:个人月薪0:退出"<<endl;cout<<"\t\t=================================================="<<endl;cout<<"\n输入您要进行的操作:";cin>>choose;while(choose!=0){switch(choose){case 1:cout<<"输入员工编号:";cin>>n;cout<<"输入员工出生日期:";cin>>y>>m>>d;cout<<"输入员工姓名:";cin>>str;cout<<"输入员工性别:";cin>>se;cout<<"输入员工职位:";cin>>p;if (p=="manager"){m1.set_manager( n, str, se,y,m,d,p);m1.display();}else if(p=="technician"){float t;cout<<"输入时间:";cin>>t;tec.set_technician( n,str, se,y,m,d, p, t) ;tec.display();}else if(p=="saleman"){float sale2;string de;cout<<"销售额:" ;cin >> sale2;cout<<"所属部门:";cin>>de ;s.set_saleman( n, str, se,y,m,d, p, sale2, de);s.display();}else{float sale1;string de;cout<<"销售额:" ;cin>>sale1;cout<<"所属部门:";cin>>de ;sg.set_saleman( n,str, se,y,m,d, p, sale1, de) ;s.display();}break;case 2:cout<<"\n\t\t===================================================="<<endl;cout<<"\t\t11: 经理12: 技术人员13:销售员14:销售经理"<<endl;cout<<"\t\t============================================================"< <endl;cout<<"\n输入您要进行的操作:";cin>>choose1;while(choose1!=0){ switch(choose1){case 11:m1.total_salary();break;case 12:tec.total_salary();break;case 13:s.total_salary();break;case 14:sg.total_salary();break;}choose1=0;}break;}cout<<"\n\t\t=================================================="<<endl;cout<<"\t\t1:输入信息与显示2:个人月薪0:退出"<<endl;cout<<"\t\t=================================================="<<endl;cout<<"\n输入您要进行的操作:";cin>>choose;}return 0;}。

C 继承与派生实验报告

C 继承与派生实验报告

C 继承与派生实验报告1. 引言继承与派生是面向对象编程中的基本概念之一,C语言作为一门面向过程的编程语言,也支持继承与派生的概念。

本实验旨在通过编写代码演示C语言中的继承与派生的使用方法,加深对这一概念的理解。

2. 继承与派生的概念继承是一种面向对象编程中的重要概念,通过继承,派生类可以继承基类的属性和方法。

在C语言中,继承是通过结构体嵌套的方式实现的。

派生是继承的一种特殊形式,通过派生,派生类可以在基类的基础上增加新的属性和方法。

3. 实验步骤步骤一:定义基类首先,我们需要定义一个基类,基类包含一些公共的属性和方法。

在C语言中,我们可以使用结构体来定义类。

typedef struct {int x;int y;} Point;上述代码定义了一个名为Point的结构体,它包含了两个整型属性x和y。

这个结构体可以看作是基类。

步骤二:定义派生类接下来,我们可以定义派生类,派生类通过嵌套包含基类的结构体来实现继承。

typedef struct {Point base; // 基类结构体int z; // 派生类自己的属性} Point3D;上述代码定义了一个名为Point3D的结构体,它嵌套包含了基类Point的结构体,并新增了一个整型属性z。

这个结构体可以看作是派生类。

步骤三:使用派生类在定义好派生类后,我们可以使用派生类来创建对象,并调用基类的属性和方法。

int main() {// 创建对象Point3D point3d;point3d.base.x = 1;point3d.base.y = 2;point3d.z = 3;// 调用基类属性printf("x: %d\n", point3d.base.x);printf("y: %d\n", point3d.base.y);// 调用派生类自己的属性printf("z: %d\n", point3d.z);return0;}上述代码示例了如何使用派生类创建对象,并访问基类的属性和派生类自己的属性。

实验4 派生与继承

实验4  派生与继承

实验4 派生与继承一、实验目的理解继承思想,掌握继承的语法,体会不同继承方式下派生类对基类成员的访问权限。

二、实验内容:1.编写一个程序,实现字符串的相关操作。

要求:在已有简单串类myString的基础上派生字符串类String1,类中包含输入字符串、返回字符串长度及内容等功能,并能实现字符串连接与比较(不用库函数);设计一个具有编辑功能的串类String2,它从String1类派生而来。

成员函数能够在在字符串指定位置处实现如下操作:插入字符串、替换和删除某个字符,并能查找子串。

类的设计可参考如下UML图:2. 综合性的实验,设计数组类族。

要求编写的程序涉及到C++的许多知识点,如类的定义,动态分配内存,构造函数,派生,虚基类等。

实验内容给出的是一个完整的程序,4个题目实际是类等级。

同学也可以在此基础上发挥增加新的内容。

(1)定义一个基类MyArray,基类中可以存放一组数组。

Class Myarray{ int *alist;int length;public:Myarray( int leng);Myarray(const Myarray&);~myarray();void input();void display();};编写构造函数,析构函数及其它函数,实现动态分配内存,释放内存和数据输入输出等功能。

并进行调试。

(2)定义一个类averarray继承自myarray,在类中求数组的平均值,并输出。

进行调试。

(3)定义一个类revarray继承自myarray, 使数组按反序存放,并输出。

进行调试。

(4)定义一个类Safearray继承自averarray和revarray,并保证访问数组不会越界。

在继承过程中声明为虚基类,体会虚基类在解决二义性中的问题中的作用。

调试中可以试一试不用虚基类出现的问题。

(提交书面作业)3. 设计一个学生和教师类,学生数据有编号、姓名、班级和成绩;教师数据有编号、姓名、职称和部门;要求:运用继承的思想实现相关功能:对学生和教师的数据进行输入和显示,类中包括各种构造函数,思考如何在派生类的构造函数中初始化基类的成员。

继承与派生

继承与派生

实验四继承与派生一.实验目的及要求1.掌握单继承和多重继承下派生类的定义方法,理解基类成员在不同的继承方式下不同的访问属性。

2.正确定义派生类的构造函数与析构函数,理解定义一个派生类对象时各个构造函数、析构函数被调用的顺序。

3.正确定义虚基类,消除在多层次继承方式下顶层基类中成员访问的二义性问题,关注此时各构造函数、析构函数的调用顺序。

4.通过基类与派生类的定义,及基类对象、指针、引用与派生类的对象、地址间相互赋值的方法,正确理解赋值兼容的4种情况,通过程序理解其不可逆性。

二.实验内容在自己的文件夹下建立一个名为exp4的工程,在该工程中做如下操作:定义一个车基类,派生出自行车类和汽车类,又以自行车类和汽车类为基类共同派生出摩托车类,每个类都要定义带有参数的构造函数。

对自行车类继承基类的方式分别用private、protected、public,观察基类成员在派生类中的访问属性;观察自行车类、汽车类和摩托车类对象定义时构造、析构函数的调用顺序。

最后将车基类定义为虚基类再观察程序运行结果,具体完成以下要求。

(1)定义基类Vehicle,具有两个保护成员变量:MaxSpeed、Weight,有3个公有的成员函数:Run()、Stop()、Show(),以及带参数构造函数、析构函数;再定义一个从Vehicle公有继承的Bicycle类,增加Height保护属性的成员变量,定义Bicycle类的构造函数、析构函数,改造Show函数,用于输出本类中的完整信息。

main()函数中定义Bicycle类对象,观察构造函数和析构函数的执行顺序,以及各成员函数的调用。

请用跟踪的方法观察程序运行的每一步究竟调用的是哪一个函数。

(2)在上一步基础上,将继承方式分别修改为protected和private,再重新编译,观察这时的报错信息并能解释。

修改程序使程序正确运行。

(3)将Bicycle类的继承方式恢复为public,代码回到(1)的状态,再在Bicycle类下面增加一个第二层汽车类Car的定义,Car也是公有继承基类Vehicle,其中增加了一个保护成员变量SeatNum,表示汽车有几个座位,其定义方式与类Bicycle类似。

[C++]继承和派生实验报告

[C++]继承和派生实验报告

运行结果:修改过后的程序代码如下:#include <iostream>#include <cstring>using namespace std;class Person{private: char m_strName[20];int m_nAge;int m_nSex;public: Person();//构造函数Person( char *name, int age, char sex ); //构造函数Person( const Person &p ); //拷贝构造函数~Person() //析构函数{cout<<"Now destroying the instance of Person"<<endl;}void SetName( char *name );void SetAge( int age );void setSex( char sex );char* GetName();运行结果:2. 程序的类结构图为:A-x:int+A()+A( int m ) : x( m )+~A()B-A a-y:int+B()+B( int m, int n, int l ) : A( m ), a( n ),y( l )+~B()运行结果:3.程序的类结构图为:Person#m_name[20]:char#m_age:int#m_sex:char+Person()+information(char* name,int age,char sex):void+~Person()Teacher#major[20]: char#position[20]: char#course[20]: char+m_major(char* m): void+m_position(char* p):void+m_course(char* c): voidcout<<'['<<x_size<<","<<y_size<<']'<<", "<<'['<<i_size<<","<<j_size<<']'; }int main(){Circle1 circle(0.0,0.0,3.0);circle.area();circle.perimeter();circle.print();cout<<"\n";Square1 square(0.0,0.0,3.0,3.0);square.area();square.perimeter();square.print();cout<<"\n";cout<<"圆的面积为:"<<circle.area()<<endl;cout<<"圆的周长为:"<<circle.perimeter()<<endl;cout<<"圆的圆心坐标和半径为:";circle.print();cout<<"\n\n";cout<<"正方形的面积为:"<<square.area()<<endl;cout<<"正方形的周长为:"<<square.perimeter()<<endl;cout<<"正方形的中心坐标和一个顶点坐标分别为:";square.print();cout<<"\n";return 0;}运行结果:。

实验4类的继承和派生

实验4类的继承和派生
{
height=h;
}
void display3()
{
cout<<"圆锥的表面积是: "<<pi*r*sqrt(height*height+r*r)<<"圆锥的体积是: "<<(1.0/3)*pi*r*r*height<<endl;
}
private:
float height;
};
void main()
太原工业学院计算机工程系报告实验课程名称姓名实验名称实验目的及要求实验环境c班级学号实验日期实验成绩20121115类的继承和派生1理解继承的含义掌握派生类的定义方法和实现
课程名称
C++
班级
实验日期
2012.11.15
姓 名
学号
实验成绩
实验名称
类的继承和派生







1、理解继承的含义,掌握派生类的定义方法和实现;
#define pi 3.14
class Circle
{
public:
Circle(float r1)
{
r=r1;
}
protected:
float r;
};
class Ball:public Circle
{
public:
Ball(float r1):Circle(r1){}
void display1()
using namespace std;
class person
{
public:
person(string na,int num1)

实验4 继承与派生

实验4  继承与派生

实验4 继承与派生班级网络1311 学号39 姓名付豪成绩一、实验目的1.熟练掌握类的继承,能够定义和使用类的继承关系2.掌握派生类的声明与实现方法3.掌握类构造函数的初始化列表与作用域分辨率的使用方法4.理解虚基类在解决二义性问题中的作用.二、实验内容1.定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩,在main()函数中定义基类和派生类对象,对类进行测试。

2.声明一个哺乳动物Mammal类,再由此派生出狗Dog类,声明一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。

3.定义一个Point类,派生出矩形类Rectangle和圆类Circle,计算各派生类对象的面积Area()。

4.设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。

5.定义一个大学生类student,函数私有数据成员:姓名、学号、校名,并为它定义带参数的构造函数,参数带缺省值的构造函数和输出数据成员值的print()公有成员函数,另定义研究生类,它以公有继承方式派生于类student,新增加“研究方向、导师名”两个私有数据成员,并定义带参数的构造函数和输出研究生数据的print()公有成员函数。

在main()函数中定义基类和派生类对象,对类进行测试。

三、实验源程序、测试与结论1.#include<iostream>#include<string>using namespace std;class person{string name; char sex;int age;public:person(string n,char s,int a):name(n),sex(s),age(a) {}void show(){cout<<name<<endl;cout<<sex<<endl;cout<<age<<endl;}};class teacher:public person{int num; string job; int money;public:teacher(string n,char s,int a,int nu,string j,int m):person(n,s,a),num(nu),job(j),money(m) {} void show(){person::show();cout<<num<<endl;cout<<job<<endl;cout<<money<<endl;}};class student:public person{int num; string Class; string zhuanye; float sorce;public:student(string n,char s,int a,int nu,string C,string z,float ss):person(n,s,a),num(nu),Class(C),zhuanye(z),sorce(ss) {}void show(){person::show();cout<<num<<endl;cout<<Class<<endl;cout<<zhuanye<<endl;cout<<sorce<<endl;}};void main(){ teacher t1("Fsda",'M',1234,74,"jiaoshi",8000);student s1("Hfgh",'F',1145,1001,"BX1311","wangluo",511);t1.show();cout<<endl<<endl;s1.show();}2.#include<iostream>using namespace std;class Mammal{public:Mammal(){cout<<"Mammal构造"<<endl;}~Mammal(){cout<<"Mammal析构"<<endl;} };class Dog:public Mammal{public:Dog(){cout<<"Dog构造"<<endl;}~Dog(){cout<<"Dog析构"<<endl;} };void main(){ Dog a;}3.#include<iostream>using namespace std;class Point{double x;double y;public:Point(double x,double y):x(x),y(y){}void show(){cout<<"("<<x<<","<<y<<")"<<endl;}};class Rectangle:public Point{double x, y;public:Rectangle(double x,double y,double x1,double y1):Point(x,y),x(x1),y(y1){}void Area(){Point::show();cout<<"Ãæ»ý:"<<x*y<<endl;}};class Circle:public Point{double r;public:Circle(double x,double y,double c):Point(x,y),r(c){}void Area(){ Point::show();cout<<"Ãæ»ý:"<<r*r*3.14<<endl;}};void main(){Rectangle f2(5,6,7,8);Circle f3(1,2,3);f2.Area();f3.Area();}4.#include<iostream>#include<string>using namespace std;class Circle{int r;public:Circle(int r1){r=r1;}};class Table{int h;string color;public:Table(int h1,string c1){h=h1;color=c1;}};class Roundtable:public Circle,public Table{public:Roundtable(int x,int y,string z):Circle(x),Table(y,z){cout<<"高度:"<<x<<"面积:"<<y<<"颜色:"<<z<<endl;}};void main(){ string c;cout<<"请输入颜色:"<<endl;cin>>c;Roundtable a(7,11,c);}5.#include<iostream>#include<string>using namespace std;class student{protected:string name;int num;string school;public:student(string n="hi",int a=0,string b="bey"):name(n),num(a),school(b){} void print(){ cout<<"name:"<<name<<endl;cout<<"num:"<< num<<endl;cout<<"school:"<<school<<endl;}};class graduate:public student{string research_area;string tutor_name;public:graduate(string a,int b,string c,string d,string e):student(a,b,c),research_area(d),tutor_name(e){}void print(){ cout<<"name:"<<name<<endl;cout<<"num:"<< num<<endl;cout<<"school:"<<school<<endl;/**///student::print();cout<<"research_area:"<<research_area<<endl;cout<<"tutor_name:"<<tutor_name<<endl;}};void main(){graduate g("Fsda",17,"DJ","wangluo","Tao");cout<<"graduate:"<<endl;g.print();cout<<endl<<endl;student s("Fuh",11,"DJ");cout<<"student:"<<endl;s.print();}四、实验小结能够较好地应用类的继承,能够定义和使用类的继承关系掌握了派生类的声明与实现方法对类构造函数的初始化列表与作用域的使用更加熟悉理解虚基类在解决二义性问题中的作用.。

C++实验报告--继承和派生

C++实验报告--继承和派生
cout<<"Teacher constructing...."<<endl;
}
void show(){
Person::show();
cout<<"Teacher lesson:"<<lesson<<endl;
}
};
class Student:virtual public Person{
private:
{
cout<<"constructing...."<<endl;
}
void show(){
cout<<"YJSZJ:"<<endl;
Teacher::show();
Student::show();
}
};
void main(){
YJSZJ x(21,50,98.0,99.0,97.0,5000,02);
}
void show_biaomianji()
{
cout<<"表面积:"<< "2*(A*B+A*H+B*H)= "<<2*(A*B+A*H+B*H)<<" (cm2)"<<endl;;
}
};
void main(){
Point C(6,8),D(3,4);
C.showXY();
D.showXY();
int H;
public:
Cuboid(int H,int A,int B,int X,int Y):Rectangle(A,B,X,Y)

C++程序设计实验四-类的继承与派生

C++程序设计实验四-类的继承与派生

电子与信息工程学院实验报告班级学号姓名吴前斌同组实验课程:C++程序设计实验项目:类的继承与派生实验日期:2019 年 5 月 5 日运行的效果如下:9、#include<iostream>#include<string>using namespace std;class building{protected:string name;int floors;float areas;public:building(string,int,float);void print();};building::building(string q,int m,float s){name=q;floors=m;areas=s;}void building::print(){cout<<"name:"<<name<<"floors:"<<floors<<"area:"<<areas<<endl; }class house:public building{public:house(string q,int m,float s,int i,int j):building(q,m,s){rooms=i;balcony=j;}void print();private:int rooms;int balcony;void house::print(){cout<<"name:"<<name<<"floors:"<<floors<<"areas:"<<areas<<"rooms:"<<roo ms<<"balcony:"<<balcony<<endl;}class office:public building{public:office(string q,int m,float s,int a,int b):building(q,m,s){offices=a;meetingrooms=b;}void print();private:int offices;int meetingrooms;};void office::print(){cout<<"name:"<<name<<"floors:"<<floors<<"areas:"<<areas<<"offices:"<<offic es<<"meetingrooms:"<<meetingrooms<<endl;}int main(){house q1("住宅",8,520,20,17);office q2("办公室",26,530,5,6);q1.print();q2.print();return 0;}运行的效果如下:int main(){account a1("cz",90,80,90,80,90);chemistry c1("zc",80,90,80,90,80);a1.show();cout<<"---------------------------"<<endl;c1.setanl(100);c1.show();}运行的效果如下图所示:实验心得:本次实验用了C++函数定义函数和函数重载,以及相关类的知识点,同时得掌握使用debug进行对函数的调试。

C++实验四继承与派生、多态性编程

C++实验四继承与派生、多态性编程

C++实验四继承与派生、多态性编程实验四继承与派生、多态性编程一、实验目的1.掌握面向对象程序设计继承与派生机制的概念2.理解并掌握public、protected和private三种不同继承方式的特点与属性3.深入理解面向对象程序设计关于多态性的概念与特征4.掌握纯虚函数的定义方法、运用纯虚函数实现多态性二、实验内容1.试编写程序定义几何图形类Shape作为基类,并在Shape的基础上派生出圆形Circle类和矩形Rectangle类,两个派生类都有函数CalculateArea计算几何图形面积。

在主函数中,要求创建圆形Circle 类和矩形Rectangle类的对象,并输出半径为30的圆的面积和长、宽分别为250、180的矩形的面积。

2.试定义一个Shape抽象类,在此基础上派生出正方形Square 类、矩形Rectangle类、圆形Cirlce类和梯形LadderShape类,四个派生类都有成员函数CalculateArea计算几何图形的面积,CalculatePerim计算几何图形的周长。

在主函数中,要求创建Square类、Rectangle类、Cirlce类和LadderShape类的对象,并输出边长为15的正方形的周长及面积,长、宽分别为18、16的矩形的周长及面积,半径为30的圆的周长及面积,上、下底分别为12、24腰均为10高为8的梯形的周长及面积。

三、实验结果1.实验代码#include#include#define pi 3.1415926class Shape{public:Shape(float a,float b,float c)x=a;y=b;r=c;}float Getx(){return x;}float Gety(){return y;}float Getr(){return r;}protected:float x,y,r;};class Circle :public Shape{public:Circle(float a,float b,float c):Shape(a,b,c) {}float CalculateArea()return pi*r*r;}};class Rectangle :public Shape{public:Rectangle(float a,float b,float c):Shape(a,b,c) {}float CalculateArea(){return x*y;}};void main(){Circle C1(0,0,30);Rectangle C2(250,180,0);cout<<"s="<<c1.calculatearea()<<endl;< p=""> cout<<"s="<<c2.calculatearea()<<="">1,实验结果s=2827.43s=45000Press any key to continue2.实验代码#include#include#define pi 3.1415926class Shapepublic:Shape(float a,float b,float c,float d) {x=a;y=b;r=c;h=d;}float Getx(){return x;}float Gety(){return y;}float Getr(){return r;}float Geth()return h;}protected:float x,y,r,h;};class Square :public Shape{public:Square(float a,float b,float c,float d):Shape(a,b,c,d) {}float CalculateArea(){return x*x;}float CalculatePerim(){return 4*x;}};class Rectangle :public Shape{public:Rectangle(float a,float b,float c,float d):Shape(a,b,c,d) { }float CalculateArea(){return x*y;}float CalculatePerim(){return (2*(x+y));}};class Circle :public Shape{public:Circle(float a,float b,float c,float d):Shape(a,b,c,d){}float CalculateArea(){return pi*r*r;}float CalculatePerim(){return 2*pi*r;}};class LadderShape :public Shape{public:LadderShape(float a,float b,float c,float d):Shape(a,b,c,d) { }float CalculateArea(){return ((x+y)*h)/2;}float CalculatePerim(){return (x+y+2*r);}};void main(){Square C1(15,0,0,0);Rectangle C2(18,16,0,0);Circle C3(0,0,30,0);LadderShape C4(12,14,10,8);cout<<"s1="<<c1.calculatearea()<<endl;< p=""> cout<<"C1="<<c1.calculateperim()<<endl;< p=""> cout<<"s2="<<c2.calculatearea()<<endl;< p=""> cout<<"C2="<<c2.calculateperim()<<endl;< p=""> cout<<"s3="<<c3.calculatearea()<<endl;< p=""> cout<<"C3="<<c3.calculateperim()<<endl;< p=""> cout<<"s4="<<c4.calculatearea()<<endl;< p=""> cout<<"C4="<<c4.calculateperim()<<endl;< p=""> }2实验结果s1=225C1=60s2=288C2=68s3=2827.43C3=188.496s4=104C4=46Press any key to continue</c4.calculateperim()<<endl;<></c4.calculatearea()<<endl;<></c3.calculateperim()<<endl;<></c3.calculatearea()<<endl;<></c2.calculateperim()<<endl;<></c2.calculatearea()<<endl;<></c1.calculateperim()<<endl;<></c1.calculatearea()<<endl;<></c2.calculatearea()<</c1.calculatearea()<<endl;<>。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验4 继承与派生---参考程序二、实验内容1.定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。

#include<iostream>#include<string>using namespace std;class Person{public:Person(string s1,int s2,string s3): name(s1),age(s2),gender(s3){}void show(){ cout<<name<<"\t"<<age<<"\t"<<gender<<"\t"; }private:string name;int age;string gender;};class Teacher: public Person{public:Teacher(string s1,int s2,string s3,string s4,string s5,double s6):Person(s1,s2,s3),num(s4),title(s5),salary(s6){}void show(){// Person::show();cout<<num<<"\t"<<title<<"\t"<<salary<<endl; }private:string num;string title;double salary;};class Student:public Person{public:Student(string s1,int s2,string s3,string s4,string s5,string s6,double s7):Person(s1,s2,s3),num(s4),Class(s5),major(s6),score(s7){}void show(){ Person::show();cout<<num<<"\t"<<Class<<"\t"<<major<<"\t"<<score<<endl; } private:string num;string Class;string major;double score;};int main(void){Teacher T("JSR",34,"female","23435","professor",10000);Student S("ZJ",19,"male","36","BX1108","Communication",88);cout<<"姓名"<<"\t"<<"年龄"<<"\t"<<"性别"<<"\t"<<"工号"<<"\t"<<"职称"<<"\t\t"<<"薪水"<<endl;T.Person::show();T.show();cout<<"姓名"<<"\t"<<"年龄"<<"\t"<<"性别"<<"\t"<<"学号"<<"\t"<<"班级"<<"\t\t"<<"专业"<<"\t"<<"成绩"<<endl;S.show();return 0;}2.设计一个圆类Circle和一个桌子类Table,再由它们共同派生出圆桌类RoundTable,要求输出一个圆桌的高度、面积和颜色等数据。

#include<iostream>#include<string>using namespace std;class Circle{public:Circle(double r):radius(r){}double area(){return 3.14159*radius*radius;}private:double radius;};class Table{public:Table(string c,double h):color(c),height(h){}void show(){ cout<<"The Table's color is "<<color<<" ,height is "<<height;}private:string color;double height;};class Roundtable: public Circle,public Table{public:Roundtable(double r,string c,double h):Circle(r),Table(c,h){}};int main(void){Roundtable R(4,"blue",3);R.show();cout<<" ,area is "<<R.area()<<endl;return 0;}3. 分别定义教师类Teacher和干部类Cadre,采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)。

要求:#include<string>#include <iostream>using namespace std;class Teacher{public:Teacher(string nam, int a, char s, string tit, string ad, string t);void display();protected:string name; //姓名int age; //年龄char sex; //性别string title; //职称string addr; //地址string tel; //电话};Teacher::Teacher(string nam, int a, char s, string tit, string ad, string t): name(nam), age(a), sex(s), title(tit), addr(ad), tel(t){ }void Teacher::display(){ cout << "name:" << name << endl;cout << "age:" << age << endl;cout << "sex:" << sex << endl;cout << "title:" << title << endl;cout << "address:" << addr << endl;cout << "tel:" << tel << endl;}class Cadre{public:Cadre(string nam, int a, char s, string p, string ad, string t);void display();protected:string name; //姓名int age; //年龄char sex; //性别string post; //职务string addr; //地址string tel; //电话};Cadre::Cadre(string nam, int a, char s, string p, string ad, string t): name(nam), age(a), sex(s), post(p), addr(ad), tel(t){}void Cadre::display(){ cout << "name:" << name << endl;cout << "age:" << age << endl;cout << "sex:" << sex << endl;cout << "post:" << post << endl;cout << "address:" << addr << endl;cout << "tel:" << tel << endl;}class Teacher_Cadre: public Teacher, public Cadre{public:Teacher_Cadre(string nam, int a, char s, string tit, string p, string ad, string t, float w);void show( );private:float wage; //工资};Teacher_Cadre::Teacher_Cadre(string nam, int a, char s, string t, string p, string ad, string tel,float w): Teacher(nam, a, s, t, ad, tel), Cadre(nam, a, s, p, ad, tel), wage(w) {}void Teacher_Cadre::show( ){ Teacher::display();cout << "post:" << Cadre::post << endl;cout << "wages:" << wage << endl;}int main( ){Teacher_Cadre te_ca("Wang-li", 50, 'f', "prof.", "president"," 135 Beijing Road, Shanghai", "(021)61234567", 1534.5);te_ca.show( );return 0;}4.定义一个基类汽车类Car,有型号、颜色、发动机功率、车速、重量、车牌号,再由汽车类派生出客车类Bus和货车类Wagon,客车类增加客车座位数、客运公司,货车类增加载货重量、货运公司。

相关文档
最新文档