实验六-继承与派生
继承与组合
一、实验目的
1.了解继承在面向对象程序设计中的重要作用。2.进一步理解继承与派生的概念。
3.掌握通过继承派生出一个新的类的方法。4.了解虚基类的作用和用法。
5.掌握类的组合
二、实验内容
1.请先阅读下面的程序,写出程序运行的结果,然后再上机运行程序,验证自己分析的结果是否正确。
(1)
#include
using namespace std;
class A
{public:
A(){cout<<"A::A() called.\n";}
virtual ~A(){cout<<"A::~A() called.\n";}
};
class B:public A
{public:
B(int i)
{ cout<<"B::B() called.\n";
buf=new char[i];
}
virtual ~B()
{ delete []buf;
cout<<"B::~B() called.\n";
}
private:
char *buf;
};
void fun(A *a)
{ cout<<"May you succeed!"< delete a; } int main() { A *a=new B(15); fun(a); return 0; } 1、 (1)程序运行结果: A::A() called. B::B() called. May you succeed! B::~B() called. A::~A() called. (2) #include using namespace std; class A{ public: A(int a,int b):x(a),y(b){ cout<<"A constructor..."< void Add(int a,int b){ x+=a;y+=b;} void display(){ cout<<"("< ~A(){cout<<"destructor A..."< int x,y; }; class B:private A{ private: int i,j; A Aobj; public: B(int a,int b,int c,int d):A(a,b),i(c),j(d) ,Aobj(1,1){ cout<<"B constructor..."< { A::Add(x1,y1); i+=x2; j+=y2; } void display(){ A::display(); Aobj.display(); cout<<"("< } ~B(){cout<<"destructor B..."< int main() { B b(1,2,3,4); b.display(); b.Add(1,3,5,7); b.display(); return 0; } (2)程序运行结果: A constructor... A constructor... B constructor... (1,2) (1,1) (3,4) (2,5) (1,1) (8,11) destructor B... destructor A... destructor A... (3) #include using namespace std; class A{ public: A(int a):x(a){ cout<<"A constructor..."< int f(){return ++x;} ~A(){cout<<"destructor A..."< int x; }; class B:public virtual A{ private: int y; A Aobj; public: B(int a,int b,int c):A(a),y(c),Aobj(c){ cout<<"B constructor..."< int f(){ A::f(); Aobj.f(); return ++y; } void display(){ cout< ~B(){cout<<"destructor B..."< }; class C:public B{ public: C(int a,int b,int c):B(a,b,c),A(0){ cout<<"C constructor..."< }; class D:public C,public virtual A{ public: D(int a,int b,int c):C(a,b,c),A(c){ cout<<"D constructor..."< ~D(){cout<<"destructor D..."< }; int main() { D d(7,8,9); d.f(); d.display(); return 0; } (3)程序运行结果: A constructor (9) A constructor (9) B constructor (9) C constructor... D constructor... 12 12 11 destructor D... destructor B... destructor A... destructor A... (4) #include using namespace std; class Base1 { public: Base1() { cout<<"class Base1!"< }; class Base2 { public: Base2() { cout<<"class Base2!"< } }; class Level1:public Base2,virtual public Base1 { public: Level1() { cout<<"class Level1!"< } }; class Level2: public Base2,virtual public Base1 { public: Level2() { cout<<"class Level2!"< } }; class TopLevel:public Level1,virtual public Level2 { public: TopLevel() { cout<<"class TopLevel!"< } }; int main() { TopLevel obj; return 0; } (4)程序运行结果: class Base1! class Base12 class Leve12! class Base2! class Leve11! class TopLeve1! 2.某出版系统发行图书和磁带,利用继承设计管理出版物的类。要求如下:建立一个基类Publication存储出版物的标题title、出版物名称name、单价price及出版日期date。用Book类和Tape类分别管理图书和磁带,它们都从Publication类派生。Book类具有保存图书页数的数据成员page,Tape类具有保存播放时间的数据成员playtime。每个类都有构造函数、析构函数,且都有用于从键盘获取数据的成员函数 inputData(),用于显示数据的成员函数display()。 2、 #include using namespace std; #include class Date {public: Date(){} Date(int y,int m,int d); Date(Date &d); void SetDate(int y,int m,int d); void ShowDate(); private: int year,month,day; }; Date::Date(int y,int m,int d){ year=y; month=m; day=d; } Date::Date(Date &d){ y ear=d.year; month=d.month; d ay=d.day; } void Date::SetDate(int y,int m,int d) { year=y; month=m; day=d; } void Date::ShowDate() { cout< class Time {public: Time(){} Time(int h,int m,int s); Time(Time &t); void SetTime(int h,int m,int s); void ShowTime(); private: int hour,minute,second; }; Time::Time(int h,int m,int s){ hour=h; minute=m; second=s; } Time::Time(Time &t){ hour=t.hour; minute=t.minute; second=t.second; } void Time::SetTime(int h,int m,int s){ hour=h; minute=m; second=s; } void Time::ShowTime(){ cout< class Publication {public: Publication(){} Publication(string title,string name,float price,int y,int m,int d); void inputData(); void display(); private: string title; string name; float price; Date date; }; Publication::Publication(string title,string name,float price,int y,int m,int d) :title(title),name(name),price(price),date(y,m,d){ } void Publication::inputData() { cout<<"Please input title, name, price, publication date of a book:"< cin>>title>>name>>price; int year,month,day; cin>>year>>month>>day; date.SetDate(year,month,day); } void Publication::display() { cout<<"title="< cout<<"name="< cout<<"price="< cout<<"date="; date.ShowDate(); } class Book:public Publication {public: Book(){} Book(string title,string name,float price,int y,int m,int d,int page); void inputData(); void display(); private: int page; }; Book::Book(string title,string name,float price,int y,int m,int d,int page) :Publication(title,name,price,y,m,d){this->page= page;} void Book::inputData() { Publication::inputData(); cout<<"Please the pages of the book:"< } void Book::display() { Publication::display(); cout<<"page="< } class Tape:public Publication {public: Tape(){} Tape(string title,string name,float price,int y,int m,int d,Time playtime); void inputData(); void display(); private: Time playtime; }; Tape::Tape(string title,string name,float price,int y,int m,int d,Time playtime) :Publication(title,name,price,y,m,d),playtime(pl aytime){} void Tape::inputData() { Publication::inputData(); cout<<"Please the playtime of the tape:"< int hour,minute,second; cin>>hour>>minute>>second; playtime.SetTime(hour,minute,second); } void Tape::display() { Publication::display(); cout<<"playtime="; playtime.ShowTime(); } int main() { Book book1("教材","C++程序设计",30.00,2009,6,1,300); book1.display(); cout< Time time1(30,10,20); Tape tape1("磁带","C++程序设计视频",10.00,2009,8,1,time1); tape1.display(); Book book2; book2.inputData(); book2.display(); cout< Tape tape2; tape2.inputData(); tape2.display(); return 0; } 实验目的与要求: 1.掌握类的继承与派生关系以及实验方法,理解类的层次结构。 2.掌握派生类构造函数初始化基类成员和对象成员的方法。 3.掌握内联函数和默认函数。 4.掌握赋值兼容原则,掌握派生类的复制构造函数和赋值运算符的定义。 实验过程及内容: 1.实践教程实验二十二P81范例:定义一个继承与派生关系的类体系,在 派生类中访问基类成员。 ①先定义一个点类,包含x,y坐标数据成员,显示函数和计算面积的函数成员; ②以点为基类派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数; ③定义一个线段类,以两个点类对象作数据成员,定义显示、求面积及长度函数,线段类采用聚合方式,因为有两个端点,不能用派生。 编程测试所定义的类体系。 本实验教程中有源码,请自行运行,体会和熟悉继承与派生的基本概念及实现方法,掌握派生类构造函数初始化基类成员和对象成员的方法等。2. 实践教程P83编程:多层派生练习,由上题Point类和Circle类继续派生出Cylinder类。要求计算圆柱的底面积、侧面积、全面积和体积。 请编写所有完整的成员函数,并编写主函数进行验证。 数据处理 1. (1) (2)j结果报错,原因是派生类中的成员函数不能访问基类中的私有成员。(3)在Line类中添加两个数据成员。 2. #include 注意派生类的构造函数中对基类数据成员的初始化方法(即 Circle(double xv,double yv,double vv):Point(xv,yv)), 以及构造函数中对对象成员的初始化方法(即 Line(double xv1,double yv1,double xv2,double yv2) : start(xv1,yv1),end(xv2,yv2){ } ) 【要求】 (1)建立工程,录入上述程序,改变数据实验之。 (2)修改Point 类的数据成员x ,y 的访问权限为private ,再运行,结果如何? (3)如果不将Line 类设为 Point 类的友元,应采取什么措施?为哪个类增加数据或函数成员? 2.编程:多层派生练习,由上题Point 类和Circle 类继续派生出Cylinder 类。要求计算圆柱体的底面积、侧面积、全面积和体积。 实验2 派生类与继承 实验课程名:面向对象程序设计(C++) 专业班级:学号:姓名: 实验时间:实验地点:指导教师: 2.1实验目的和要求 (1) 掌握派生类的声明方法和派生类构造函数的定义方法。 (2) 掌握不同继承方式下,基类成员在派生类中的访问属性。 (3) 掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则。 (4) 学习虚基类在解决二义性问题中的作用。 二、实验内容 一、构造一个类Geometry 及其派生类,该类主要实现关于几何图形的基本操作。对于基类“几何图形”,有求面积、求体积的函数(纯虚函数),其派生类圆和矩形主要有初始化(构造函数),求面积,求周长操作,类圆的派生类圆球和圆柱有求表面积、体积操作。 试在主函数中分别定义圆、圆球、圆柱以及矩形的对象,并调用其成员函数实现其相应操作。 实验代码如下: #include ~Geometry(){} double GetArea(){}//求面积函数double GetPerimeter(){}//求体积函数double Getcolume(){}//求周长函数 virtual show(){} }; class Circle:public Geometry { public: Circle(double i) { radiums=i; } ~Circle(){} double GetArea(); double Getcolume(); double R() { return radiums; } show(); private: 1.定义一个哺乳动物类Mammal,并从中派生出一个狗类Dog,下面给出Mammal类的定义,要求: (1)添加Dog类的颜色数据成员,访问属性为私有,通过SetColor和GetColor成员函数来对颜色进行设置和获取。 (2)分别为基类和派生类添加相应的构造函数(有参、无参)和析构函数,并进行测试。 class Mammal { protected: int itsAge; int itsWeight; public: int GetAge(){return itsAge;} void SetAge(int age) {itsAge=age;} int GetWeight() { return itsWeight;} void SetWeight(int weight) {itsWeight= weight;} }; class Dog : public Mammal { //定义Dog类的数据成员和成员函数 }; 改: #include 继承和派生实验报告
c++派生类与继承实验报告材料
c++实验8 继承与派生上机练习题
实验6-继承与派生(1)