当前位置:文档之家› 实验六-继承与派生

实验六-继承与派生

实验六-继承与派生
实验六-继承与派生

实验六-继承与派生

继承与组合

一、实验目的

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:"<>page;

}

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 #include using namespace std; #define PI 3.14159 class 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="<

注意派生类的构造函数中对基类数据成员的初始化方法(即 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 类。要求计算圆柱体的底面积、侧面积、全面积和体积。

c++派生类与继承实验报告材料

实验2 派生类与继承 实验课程名:面向对象程序设计(C++) 专业班级:学号:姓名: 实验时间:实验地点:指导教师: 2.1实验目的和要求 (1) 掌握派生类的声明方法和派生类构造函数的定义方法。 (2) 掌握不同继承方式下,基类成员在派生类中的访问属性。 (3) 掌握在继承方式下,构造函数与析构函数的执行顺序与构造规则。 (4) 学习虚基类在解决二义性问题中的作用。

二、实验内容 一、构造一个类Geometry 及其派生类,该类主要实现关于几何图形的基本操作。对于基类“几何图形”,有求面积、求体积的函数(纯虚函数),其派生类圆和矩形主要有初始化(构造函数),求面积,求周长操作,类圆的派生类圆球和圆柱有求表面积、体积操作。 试在主函数中分别定义圆、圆球、圆柱以及矩形的对象,并调用其成员函数实现其相应操作。 实验代码如下: #include using namespace std; class Geometry { public: Geometry(){} Circle radiums Circle() ~Circle() Ball Ball() ~Ball() Geometry Geometry() ~Geometry() GetArea() GetPerimeter() Getcolume() show() Column Column() ~Column() Rectangle Rectangle() ~Rectangle()

~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:

c++实验8 继承与派生上机练习题

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 #include using namespace std; class Mammal { protected: int itsAge; int itsWeight; public: Mammal(); ~Mammal(); int GetAge(){return itsAge;} void SetAge(int age) {itsAge=age;} int GetWeight() { return itsWeight;} void SetWeight(int weight) {itsWeight= weight;} }; class Dog : public Mammal {

实验6-继承与派生(1)

实验六继承与派生(1) 1.1 实验目的 (1)理解继承的含义以及声明; (2)理解共有派生、私有派生和保护派生的含义以及使用; (3)理解单派生类中构造函数和析构函数的调用顺序。 1.2 实验内容 1、定义点CPoint类作为基类,在此基础上派生出直线CLine类和圆CCircle类,并要求基类和各派生类具有以下特点: (1)CLine类含有计算直线长度和斜率的成员函数; (2)CCircle类含有计算圆面积的成员函数。 2、程序阅读。 (1)以下程序有两大错误,请修改程序,写出程序运行结果并分析。 #include using namespace std; class CBase { public: CBase(int a):a(a) {} protected: void print() { cout<<"a="<

public: void print() { CBase::print(); cout<<"b="< using namespace std; class CBase {public: CBase(int a):a(a) {cout<<"base structure"< 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)

实验2继承与派生讲解

实验2 继承与派生 2.1 实验目的 1.熟练掌握类的继承,能够定义和使用类的继承关系。 2.掌握派生类的声明与实现方法。 3.掌握类构造函数的初始化列表与作用域分辨符的使用方法。 4.理解虚基类在解决二义性问题中的作用。 2.2 实验工具与准备工作 在开始实验前,应回顾或复习相关内容。 需要一台主算机,其中安装有Visual C++ 6.0等集成开发环境软件。 2.3 实验内容 1.先阅读下列程序,写出执行结果。然后输入程序,调试程序,比较结果的正确性。 // 文件名: main.cpp #include // 预处理命令 using namespace std; // 使用标准命名空间std class A { public: // 公有函数: A(){ cout << "构造A" << endl; } // 构造函数 ~A(){ cout << "析构A" << endl; } // 析构函数 }; class B: public A { public: // 公有函数: B(){ cout << "构造B" << endl; } // 构造函数 ~B(){ cout << "析构B" << endl; } // 析构函数 }; class C: public B { public: // 公有函数: C(){ cout << "构造C" << endl; } // 构造函数 ~C(){ cout << "析构C" << endl; } // 析构函数 };

int main(void) // 主函数main(void) { C obj; // 定义对象 system("PAUSE"); // 调用库函数system( ),输出系统提示信息return 0; // 返回值0, 返回操作系统 } 2.先阅读下列程序,写出执行结果。然后输入程序,调试程序,比较结果的正确性。// 文件名: main.cpp #include // 预处理命令 using namespace std; // 使用标准命名空间std class A { protected: // 数据成员: int a; // 数据成员 public: // 公有函数: A(int x): a(x){ } // 构造函数 void Show() const{ cout << a << endl; } // 显示a之值 }; class B { protected: // 数据成员: int b; // 数据成员 public: // 公有函数: B(int x): b(x){ } // 构造函数 void Show() const{ cout << b << endl; } // 显示a与b之值 }; class C: public A, public B { public: // 公有函数: C(int x, int y): A(x), B(y){ } // 构造函数 void Show() const // 显示b之值 { cout << a << "," << b << endl; } }; int main(void) // 主函数main(void) { C obj(5, 18); // 定义对象 obj.Show(); // 显示相关信息 obj.A::Show(); // 显示相关信息 obj.B::Show(); // 显示相关信息 system("PAUSE"); // 调用库函数system( ),输出系统提示信息return 0; // 返回值0, 返回操作系统 }

c++实验报告

面向对象程序设计 (C++) 实 验 报 告 指导老师:柯栋梁 学生:吴健 班级:软131 学号:139074164

目录 实验一、VC6.0环境入门与简单程序设计实验报告 (3) 实验二、函数的应用实验报告 (7) 实验三、类与对象实验报告 (14) 实验四、C++程序结构实验报告 (22) 实验五、数组、指针与字符串实验报告 (26) 实验六、继承和派生实验报告 (33) 实验七、多态性实验报告 (42) 实验八、面向对象综合实验实验报告 (48)

实验一、VC6.0环境入门与简单程序设计实验报告 系软件工程班级 131 学号 139074164 姓名吴健 同组者指导教师柯栋梁 一、实验目的: 1、熟悉VC++6.0开了环境并编写简单的C++程序。 3、使用C++语言编写简单的输入输出程序。 4、使用VC++6.0的DEBUG调试功能:单步执行、设置断点、观察变量值。 二、实验内容: 1、使用VC++建立一个标准C++程序,编译、运行如下程序: #include Int main() { Cout<<”Hello World!\n”; Cout<<”Welcome to C++!\n”; } 2、编程计算图形的面积。程序可计算圆形、长方形、正方形等的面积,运行时首先提示用户选择图形类型,然后根据不同图形类型,输入相关参数计算其面积,并将其显示出来。 3、使用DEBUG功能观察任务2程序运行中变量值的变化情况。 三、实验要求: 1、任务1要求熟悉VC++开发环境,并能够使用其建立第一个C++程序。 2、实验前要做好充分准备,包括程序清单、调试步骤、调试方法,以及对程序结果的分析等。 四、实验报告: 1.程序运行截图

C++语言程序设计实验答案_继承与派生教学提纲

C++语言程序设计实验答案_继承与派生

实验07 继承与派生(4学时) (第7章继承与派生) 一、实验目的 二、实验任务 7_1 声明一个基类Animal。 有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。 7_2 声明一个基类BaseClass。 有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。 7_3 声明一个车(vehicle)基类。 具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类,在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题?编程试试看。

7_4 以实验6中的People(人员)类为基类。 派生出student(学生)类,添加属性:班号char classNo[7]; 派生出teacher(教师)类,添加属性:职务char principalship[11]、部门char department[21]。 从student类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser; 从graduate类和teacher类派生出TA(助教生)类,注意虚基类的使用。重载相应的成员函数,测试这些类。 类之间的关系如图7-1所示。 图7-1 类图

实验四 继承与派生讲解学习

实验四继承与派生

实验四派生类与继承 【实验类型】验证性实验【实验课时】2学时 【实验目的】 (1)理解类的继承的概念,能够定义和使用类的继承关系。 (2)掌握派生类的声明与定义方法。 (3)熟悉公有派生和私有派生的访问特性。 (4)学习虚基类在解决二义性问题中的作用。 【实验环境】 硬件:计算机 软件:Microsoft Visual C++ 6.0 【实验内容】 1、按要求阅读、编写、调试和运行以下程序。 (1)实验内容 ①定义一个基类MyArray,基类中可以存放一组整数。 class MyArray {public: MyArray(int leng); ~MyArray(); void Input(); void Display(); protected: long int *alist; // 指向动态申请的一组空间 int length;}; // 整数的个数 基类中有构造函数、析构函数、输入数据和输出数据的函数。 ②定义一个类SortArray继承自MyArray ,在该类中定义函数实现排序功能。

③定义一个类ReArray继承自MyArray ,在该类中定义函数实现逆转功能。 ④定义一个类AverArray继承自MyArray ,在该类中定义函数Aver求解整数的平均值。 ⑤定义NewArray类,同时继承了SortArray, ReArray和AverArray,使得NewArray类的对象同时具有排序、逆转、和求平均值的功能。在继承的过程中声明为虚基类,体会虚基类在解决二义性问题中的作用。 (2)实验程序 (参考) 程序如下: #include "iostream.h" #include "process.h" class MyArray {public: MyArray(int leng); ~MyArray(); void Input(); void Display(); protected: long int *alist; // 指向动态申请的一组空间 int length; // 整数的个数 }; MyArray::MyArray(int leng) { length=leng; alist=new long int[length]; if(alist==NULL) { cout<<"对不起,创建失败。请重试。 ";exit(1); } } MyArray::~MyArray() {

派生与继承实验1

实验二继承与派生——教师工资计算(一) 一、实验目的 1.理解继承的含义; 2.学习从现有类派生出新类的方式; 3.了解在派生类中如何使用基类的成员。 二、实验内容与要求 1.新建一个雇员类,它的数据成员有雇员代号,年龄,工资,性别,姓名,输入雇员资料方法,打印雇员资料方法。 2.以此雇员类为基类,从中派生出教师类,其中要求在教师类中加入一个计算教师工资的方法,教师工资=基本工资(1000)+课时(月工作量)×30。 3.以此雇员类为基类,从中派生出实验员类,其中要求在实验员类中加入一个计算实验员工资的方法,实验员工资=基本工资(800)+实验室补助(150)+值班时间(月工作量)×5。 4. 以此雇员类为基类,从中派生出行政人员类,其中要求在行政人员类中加入一个计算行政人员工资的方法,行政人员工资=基本工资(900)+行政补贴(200)。 三、实验任务分解 四、实验步骤 1. 建立工程 (1) 新建一个win32 Console Application的工程: 打开VC++开发软件,从[文件]菜单中点击[新建]菜单项,出现如图所示: 在右上角的工程下输入该工程的名称,如 c10,并设置该工程所保存的路径。 最后点击确定。

(2) 当确定后会出现要你选择工程类型的对话框,如图所示:请选择一个空的工程,即 第一个选项。 (3) 当单击确定后,工程建立完毕,接下来建立程序源文件,请再单击[文件]菜单下的[新 建]出现原先出现的对话框,请选择“文件”选项卡中的c++ source file选项,并取名,如c10 (4)当确定后,就进入了源代码的编辑窗口,如图所示:

实验六继承与派生

继承与组合 一、实验目的 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!"<

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..."<

816306411_5_实验七 继承与派生

实验七继承与派生 【实验目的】 1、掌握继承的概念。 2、理解派生类与基类的关系。 3、理解不同的继承类型。 4、掌握继承下的构造函数和析构函数。 5、掌握单继承和多继承使用方法。 6、理解静态成员。 【实验内容】 1、上机分析下面程序,理解继承下构造函数和析构函数的执行顺序。 #include using namespace std; class A { public: A() { cout<<"Constructor1_A"<< x << endl; } A( int m ) : x( m ) { cout<<"Constructor2_A"<< x << endl; } ~A() { cout<<"Destructor_A"<< x << endl; } private: int x; }; class B : public A { public: B() { cout<<"Constructor1_B"<< y << endl; } B( int m, int n, int l ) : A( m ), a( n ), y( l ) { cout<<"Constructor2_B"<< y <

B b1, b2(5,6,7); return 0; } 2、在下面一段类定义中,Derived类是有直接基类Base1和Base2所公有派生的,Derived 类包含有两个间接基类Base,在初始化函数Init中,需要把x1和x2的值分别赋给属于基类Base1的x成员和属于基类Base2的x成员。 #include using namespace std; class Base{ protected: int x; public: Base(){x=0;} }; class Base1:public Base{ public: Base1(){} }; class Base2:public Base{ public: Base2(){} }; class Derived: (1) { public: Derived(){} void Init(int x1,int x2){ (2) ; (3) ; } void output(){cout< using namespace std; class Base{

实验3 继承和派生类的应用

3.1实验目的 1.掌握多重继承和派生类的方法 2.掌握初始化基类成员的方法 3.掌握定义虚基类的方法 3.2实验内容与步骤 1.上机实验题一 定义一个日期(年、月、日)的类和一个时间(时、分、秒)的类,并由这两个类派生出日期和时间类。主函数完成基类和派生类的测试工作。 ⑴分析 定义一个描述日期的类,构造函数完成年、月、日的初始化,包含一个重新设置日期的成员函数,一个获取日期的成员函数。该类可定义为: class Date{ int Year,Month,Day; //分别存放年、月、日 public: Date(int y=0, int m=0,int d=0) { Year= y; Month = m; Day = d; } void SetDate(int ,int ,int ); void GetDate(char *); }; 函数SetDate完成数据成员的赋初值。函数GetDate要将整数年、月、日变换成字符串后,存放到参数所指向的字符串中。把一个整数变换成字符串可通过库函数: char * _itoa(int a , char *s, int b); 来实现,参数a为要变换的整数,b为数制的基数(如10,表示将a转换为对应的十进制的字符串),转换的结果存放到s所指向的字符串中。函数返回变换后字符串的首指针。该成员函数可以是: void Date::GetDate(char *s) { char t[20];

_itoa(Year,s,10); //将年变换为字符串表示 strcat(s,"/"); //年、月、日之间用“/”隔开 _itoa(Month,t,10); //将月变换为字符串表示 strcat(s,t); //将年、月字符串拼接 strcat(s,"/"); _itoa(Day,t,10); strcat(s,t); //将年、月、日拼接成一个字符串} 定义描述时间的类与描述日期的类类同,然后用这二个类作为基类,公有派生出描述日期和时间的类。 简化的参考程序如下: #include #include #include class Date{ int Year,Month,Day; //分别存放年、月、日 public: Date(int y=0, int m=0,int d=0) { Year= y; Month = m; Day = d; } void SetDate(int ,int ,int ); void GetDate(char *); }; void Date::SetDate(int y,int m,int d ) { Year= y; Month = m; Day = d; } void Date::GetDate(char *s) { char t[20]; _itoa(Year,s,10); strcat(s,"/"); _itoa(Month,t,10); strcat(s,t); strcat(s,"/"); _itoa(Day,t,10); strcat(s,t); } class Time { int Hours,Minutes,Seconds; //时、分、秒 public: Array Time(int h=0,int m=0, int s=0)

实验6 继承与派生

实验6 继承与派生 专业:计算机科学与技术班级:10计本1班学号:姓名: 实验地点:B102实验时间:2011/11/23 指导教师:李佐勇 一、实验目的 1.理解继承与派生、单继承与多继承的概念; 2.理解基类与派生类的定义及使用方法,派生类对象的定义与初始化方法; 3.理解继承与派生过程中,把派生类作为基类构成类族的概念及虚基类的概念。 二、实验环境 一台PC机,Windows XP操作系统,V isual C++ 6.0开发环境。 三、实验内容 1、由在校人员类(Person)作为基类派生出学生类(Student): 在校人员类有数据成员:编号(ID)、姓名(Name)、性别(Sex)、年龄(Age),要求有如下函数成员:构造函数、获取编号的函数和输出所有成员的函数。把在校人员类作为基类,通过公有继承,派生出学生类,派生类新增成员数据有数学(Math)、物理(Physical)、英语(English)和C++程序设计(CPP)四门课程以及总成绩(Total);新增成员函数有构造函数和输出所有成员的函数。main()完成派生类对象的定义和有关成员函数的测试。 2、由学生类、课程类作为基类,共同派生出选课类。 声明一个学生类(Student),有数据成员:学号(No)、姓名(Name)、性别(Sex)、年龄(Age),要求有如下函数成员:构造函数、输出所有成员的函数。 声明一个课程类(Lesson),有数据成员:课程编号(Cno)、课程名称(Cname)、学时数(Chour),要求有如下函数成员:构造函数、输出所有成员的函数。 将学生类和课程类作为基类,通过公有继承,共同派生出选课类(SL),派生类新增数据成员:成绩(Score);新增函数成员:构造函数、输出所有成员的函数。 main()完成派生类对象的定义和有关成员函数的测试。 3、由二维坐标点类Point作为基类派生出圆类Circle;再由圆类Circle作为基类派生出圆柱体类Cylinder。(提示:点类Point的数据成员为点坐标x、y,函数成员有构造函数和显示点坐标的函数show;Circle类新增数据成员为圆的半径radius,其成员函数show除了显示圆心的坐标外还能显示半径大小;Cylinder类新增数据成员为圆柱体高度height,其成员函数除了显示基类的所有数据成员外,还得显示圆柱体的高度) 四、实验记录 1、#include #include using namespace std; class Person{ public: Person(int i,char *n, char s, int a){ ID=i; name=n; sex=s; age=a; }; int getID(){ return ID; } void show(){ cout<<"ID: "<

第三次上机实验:继承与派生类上机实践指导

继承与派生类上机实践指导 一.实验目的 1.理解继承的含义,掌握派生类的定义方法和实现; 2.理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员; 3.理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员; 二.实验内容 1. (1) 将例5.3的程序修改、补充,写成一个完整、正确的程序,用保护继承方式。在程序中应包括输入数据的函数。(2) 修改例5.3的程序,改为用公用继承方式。上机调试程序,使之能正确运行并得到正确的结果。对这两种继承方式作比较分析,考虑在什么情况下二者不能互相代替。 2. 调试运行Ex1 2.1需求分析: 某小型公司的人员管理信息系统。 三类人员:经理(manager)、技术人员(technician)、销售人员(salesman);后期又增加一类人员:销售经理(sales_manager)。 要求存储这些人员的姓名、编号、级别、当月工资、计算月薪总额并显示全部信息。 (1)人员编号:基数为1000,每增加一名人员时,人员编号加1; (2)人员级别:所有人员初始级别为1,然后进行升级。升级规则:经理为4级、技术人员为3级、销售人员为1级、销售经理为3级; (3)月薪计算:经理=固定月薪8000元;技术人员=100元/小时; 销售人员=当月个人销售额*4%;销售经理=固定月薪5000+所辖部门当月销售额*5%。 2.2数据结构: struct employee { char *name; /* 人员姓名*/ int indiveidualEmpNo; /* 人员编号*/ int grade; /* 人员级别*/

c++实验三继承和派生类(附答案)

实验三继承和派生类 实验目的和要求 1.理解类的继承的概念,能够定义和使用类的继承关系。 2.掌握派生类的声明与定义方法。 3.熟悉公有派生和私有派生的访问特性。 4.学习虚基类在解决二义性问题中的作用。 实验内容 1.先阅读下面的程序,分析程序运行的结果,然后再上机运行程序,验证自己分析的结果是否正确。 (1) #include<> class A { public: A() { cout<<"A::A() called.\n"; } ~A() { cout<<"A::~A() called.\n"; } }; class B:public A { public: B(int i) { cout<<"B::B() called.\n"; buf=new char[i]; } ~B() { delete []buf; cout<<"B:~B() called.\n"; } private: c har *buf; }; void main() {

B b(10); } (2) #include<> class A { public: A(int a,int b):x(a),y(b) { cout<<"A constructor..."<

实验7 继承与派生

电子信息学院实验报告书 课程名:面向对象程序设计题目:实验7 继承与派生实验类别设计 班级:BX1004 学号:35 姓名:赵鑫

一.实验目的 1.理解继承的含义,掌握派生类的定义方法和实现; 2.理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员; 3.理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员; 4.理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使 用虚函数的简单程序写出程序结果。 二.实验内容 1.将例5.2的程序片段补充和改写成一个完整、正确的程序,用私有继承方式。在程序 中应包括输入数据的函数,在程序运行时输入num,name,sex,age,addr的值,程序应输出以上5个数据的值。 2.编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教 师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。 3. 编写一个程序计算出球、圆柱和圆锥的表面积和体积。 要求: (1)定义一个基类圆,至少含有一个数据成员半径; (2)定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。(3)定义主函数,求球、圆柱、圆锥的和体积(初始化用构造函数)。 3.实验结果 ⑴ #include using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display() {cout<<"学号:"<>age>>addr;} void display_1() {

相关主题
文本预览
相关文档 最新文档