文档库 最新最全的文档下载
当前位置:文档库 › c++继承与派生实验报告

c++继承与派生实验报告

c++继承与派生实验报告
c++继承与派生实验报告

实验4 继承与派生

班级学号(最后两位)姓名成绩

一、实验目的

1.熟练掌握类的继承,能够定义和使用类的继承关系

2.掌握派生类的声明与实现方法

3.掌握类构造函数的初始化列表与作用域分辨率的使用方法

4.理解虚基类在解决二义性问题中的作用.

二、实验内容

1.定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。

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

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

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

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

三、实验源程序、测试与结论

1.#include

#include

class person

{

public:

person (char *a,char *b,int s)

{

name=a;

sex=b;

score=s;

}

void display()

{

cout<<"姓名:"<

cout<<"性别:"<

cout<<"年龄:"<

}

private:

char *name ;

char *sex ;

int score;

};

class student:public person

{

public:

student(char *a,char *b,int s,char *c,int c2,float s1):person(a,b,s) {

pro=c;

cla=c2;

score=s1;

}

void display1()

{

cout<<"学生:"<

display();

cout<<"专业:"<

cout<<"班级:"<

cout<<"成绩:"<

cout<

}

private:

char *pro;

int cla ;

float score;

};

class teacher: public person

{

public:

teacher(char *a,char *b,int s,char *p,int n,int sa):person(a,b,s) {

post=p;

num=n;

salary=sa;

}

void display2()

{

cout<<"教师:"<

display();

cout<<"工号:"<

cout<<"职称:"<

cout<<"工资:"<

cout<

}

private: char *post ;

int num;

int salary;

};

void main()

{

teacher g1("小张","男",38, "老师",03,6742); g1.display2();

student s1("小李","男",19,"学生",04,85);

s1.display1 ();

}

}

2.

#include"iostream"

using namespace std;

class Mammal

{

public:

Mammal()

{

cout<<"set Mammal class"<

}

~Mammal()

{

cout<<"Delete base class"<

}

};

class Dog:public Mammal

{

public:

Dog()

{

cout<<"set Dog class\n";

}

~Dog()

{

cout<<"Delete Dog class\n";

}

};

void main()

{

Dog b;

}

3.

#include

using namespace std;

#define PI 3.14

class Point

{

public:

Point() : m_x(0), m_y(0){}

Point(double x, double y) : m_x(x), m_y(y){} ~Point(){}

protected:

double m_x;

double m_y;

};

class Rectangle : public Point

{

public:

Rectangle() : Point(){}

Rectangle(double x, double y) : Point(x, y){}

~Rectangle(){}

double Area()

{

return m_x * m_y;

}

};

class Circle : public Point

{

public:

Circle() : m_r(0){}

Circle(double r) : m_r(r){}

~Circle(){}

double Area()

{

return PI * m_r * m_r;

}

private:

double m_r;

};

void main()

{

double a,b,c;

cout<<"输入矩形长、宽:\n";

cin>>a>>b;

cout<<"输入圆的半径:\n";

cin>>c;

Rectangle rect(a, b);

Circle circle(c);

cout << "矩形面积: " << rect.Area() << endl;

cout << "圆的面积: " << circle.Area() << endl; }

4.

#include

#define PI 3.14

class table

{

};

class circle

{

};

class roundtable : public table,public circle

{

public:

void iroundtable(double h,char *c,double r) {

heigh=h;

radius=r;

color=c;

}

void display()

{

cout<<"圆桌高度:"<

cout<<"圆桌面积: "<

}

protected:

double heigh,radius;

char *color;

};

void main()

{

roundtable t;

t.iroundtable(4,"blue",6);

t.display();

}

5.

#include"string"

#include"iostream"

using namespace std;

class Student

{

char *name;

char *number;

char *college;

public:

Student();

Student(char *p1,char *p2,char *p3);

void print();

};

Student::Student(char *p1,char *p2,char *p3) {

name=new char[strlen(p1)+1];

strcpy(name,p1);

number=new char[strlen(p2)+1];

strcpy(number,p2);

college=new char[strlen(p3)+1];

strcpy(college,p3);

}

void Student::print()

{

cout<<"name:"<

cout<<"number:"<

cout<<"college:"<

}

class graduate:public Student

{

char *special;

char *directorname;

public:

graduate();

graduate(char *p1,char *p2,char *p3,char *sp,char *di);

void print();

};

graduate::graduate(char *p1,char *p2,char *p3,char *sp,char *di):Student(p1,p2,p3) {

special=new char[strlen(sp)+1];

strcpy(special,sp);

directorname=new char[strlen(di)+1];

strcpy(directorname,di);

}

void graduate::print()

{

Student::print();

cout<<"special is:"<

cout<<"directorname is:"<

}

void main()

{

Student stu1("wang","1","Dianji University");

stu1.print();

graduate stu2("liu","2","Dianji University","Computer","guo");

stu2.print();

}

继承和派生实验报告

实验目的与要求: 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="<

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 {

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

实验四继承与派生

实验四派生类与继承 【实验类型】验证性实验【实验课时】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() {

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 类图

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

福建农林大学金山学院实验报告 系(教研室):信息与机电工程系专业:计算机科学与技术年级: 实验课程:面向对象程序设计姓名:学号:  实验室号 计算机号 实验时间:指导教师签字:成绩: 实验4 派生类和继承(一) 一、实验目的和要求 (1)掌握派生类的声明与定义方法,进一步理解类的继承的概念,能够定义和使用类的继承关系。 (2)熟悉公有派生和私有派生的访问特性。 二、实验内容和原理 1、程序分析题(写出程序的输出结果,并分析结果)。

2、(1)定义一个基类animal,该类具有私有整型成员变量age,weight,构造派生类dog公有继承animal,dog类新增私有成员变量color,新增成员函数SetAge(int n)中直接给age赋值,新增成员函数SetWeight(int m)中直接给weight赋值,查看编译结果,并分析结果。(2)将类animal中的age和weight为公有成员,重做第一步,并分析结果。(3)将类animal中的age和weight为保护成员,重做第一步,并分析结果。(4)将派生类dog的继承方式改为私有继承方式和保护继承方式重做以上各小题,并分析结果。 三、实验环境 1. 硬件:PC机; 2. 软件:Windows操作系统、Visual C++ 6.0 四、算法描述及实验步骤 2.1 #include 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<<"请输入这条狗的岁数="; cin>>x;cout< 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)

实验5:继承与派生

实验项目:继承与派生 实验目的: 1.学习定义和使用类的继承关系,定义派生类 2.熟悉不同继承方式下对基类成员的访问控制 实验任务: 1.定义一个基类Animal,有私有整形成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗 2.定义一个基类BaseClass,有整形成员变量Number,构造其派生类,观察其构造函数和析构函数的执行情况。 3.定义一个车类(vehicle)基类,有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(Seatnum)等属性。,在继承和过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题变成试试看。 实验步骤: 1.编写程序定义Animal,成员变量age定义为私有的。构造其派生类dog,在其成员函数SetAge(int n)中直接对age赋值时,会出现错误提示:程序名 2.编写程序定义一个基类BaseClass,构造其派生类DerivedClass,在构造函数和析构函数中用cout输出提示信息,观察构造函数和析构函数的执行情况。程序名 3.用debug功能跟踪程序的执行过程,观察基类和派生类的构造函数和析构函数的的执行过程。 4.编写程序定义车类(vehicle),由此派生出自行车(bicycle)类、汽车(motorcar),把vehicle设置为虚基类。再从bicycle和motorcar派生出摩托车(motorcycle)类,在main()函数中测试这个类。程序名。编译成功后把vehicle 设置成非虚基类,在编译一次,此时系统报错,无法编译成功。原因是若不把 vehicle ) 实验结果: C2248Animal’ 2. 源代码: { public: int age; public:

实验六继承与派生

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

实验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, 返回操作系统 }

类的继承与派生综合题

1. 类的继承与派生综合题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)和输出实发工资函数(OutputWage)。 主函数如下: int main() { Salemanobjsale(101101, "LD", 0.88f, 1200, 800, 0.05f, 10000); Manager objmana(101102, "NXG", 0.90f, 2500, 1000, 0.10f, 20000); SaleManagerobjsalemana(101103, "HDY", 0.99f, 3500, 2000, 0.20f, 100000, 0.20f,150000); objsale.Output(); cout<< "销售员的实发工资:" << " "; cout<

实验二 类的继承与派生

实验二类的继承与派生 班级:网络工程1班 姓名:倪冬生 学号:20112346017

一、实验目的 1. 掌握类的声明和使用。 2. 掌握类的声明和对象的声明。 3. 复习具有不同访问属性的成员的访问方式。 4. 观察构造函数和析构函数的执行过程。 5. 学习声明和使用类的继承关系,声明派生类; 6. 熟悉不同继承方式下对基类成员的访问控制; 二.实验内容 1. 设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、 id(身份证号)等等。具有的属性如下:姓名char name[11]、编号char number[7]、性别char sex[3]、生日birthday、身份证号charid[16]。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、组合。在测试程序中声明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 #include using 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; }

实验四 继承与派生

实验四继承与派生 一、实验目的: 掌握利用单继承和多重继承的方式定义派生类的方法; 深刻理解在各种继承方式下构造函数和析构函数的执行顺序; 理解和掌握公有继承,私有继承和保护继承对基类成员的访问机制; 理解虚基类的概念以及引入虚基类的目的和作用。 二、实验时间: 三、实验地点: 四、实验内容: 1.运行以下程序,并对运行结果进行分析 #include"stdafx.h" #include using namespace std; class base{ int n; public: base(int a) {cout<<"constructing base class"<

c++实验3 派生类与继承1

实验三派生类与继承 一、实验目的 1、学习类的继承,能够定义和使用类的继承关系。 2、学习派生类的声明与定义方法。 3、掌握类的定义和对象的声明。 4、熟悉公有派生和私有派生的访问特性。 5、掌握派生类构造函数和析构函数的执行顺序。 6、掌握利用访问声明调整基类成员在派生类中的访问属性。 二、试验内容 1、下面的程序可以输出ASCII字符与所对应的数字的对照表。修改下列程序,使其可以输出字母a到z(或任意两个字符间)与所对应的数字的对照表。class table { public: table(int p) { i=p; } void ascii(void); protected: int i; }; void table::ascii(void) { int k=1; for (;i<127;i++) { cout<

c=m; } void print(void); protected: char *c; }; void der_table::print(void) { cout< using namespace std; #include class table { public: table(int p) { i=p; } void ascii(void); protected: int i; }; void table::ascii(void) { int k=1; for (;i<=122;i++)

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

1. 定义一个哺乳动物类Mamma,l 并从中派生出一个狗类Dog,要求: ( 1) 添加Dog 类的颜色数据成员,访问属性为私有,通过函数来对颜色进行设置和获取。 ( 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 {下面给出Mamma类的定义,SetColor 和GetColor 成员和析构函数,并进行

继承与派生(二)实验报告

学号:姓名:班级: 实验四继承与派生(二) 【实验目的】 1、理解多重继承的概念; 2、理解为了避免同同一基类出现多个重复的副本而采用的虚基类概念和虚拟继承; 3、学习利用虚基类解决二义性问题。 【实验内容】 题目: 2、设计一个用于人事管理的“people(人员)”基类。考虑到通用 性,仅只抽象出所有类型人员都有的属性:编号、姓名、性别、出生日期、身份证号等;从people(人员)类派生出student(学生)类,并添加属性:班号classNO;从people类派生出teacher(教师)类,并添加属性:职务principalship、部门Department;从student类派生出graduate (研究生)类,并添加属性:专业subject、导师teacher adviser(teacher 类);从graduate类和teacher类派生出TA(助教生)类。设计时注意虚基类的使用,注意重载相应的成员函数。测试这些类。

UML图: Date -year: int -month: int -day: int <>-Date(y: int, m: int, d: int) <>-Date(D: Date) +init(y: int, m: int, d: int): void +show(): void people #m_date: Date #m_no: long #m_ident_no: string #m_name: string #m_sex: string <>-people(no: long, name: string, sex: string, ident_no: string, year: int, month: int, day: int) <>-people(no: long, name: string, sex: string, ident_no: string, date: Date) <>-people(p: people) +init(no: long, name: string, sex: string, ident_no: string, year: int, month: int, day: int): void +init(no: long, name: string, sex: string, ident_no: string, date: Date): void +init(p: people): void +show(): void student #m_classno: string <>-student(person: people, classno: string) <>-student(stu: student) +show(): void teacher #m_principalship: string #m_department: string <>-teacher(p: people, principalship: string, department: string) <>-teacher(stu: teacher) +show(): void graduate #m_subject: string #m_adviser: teacher <>-graduate(s: student, subject: string, t: teacher) <>-graduate(g: graduate) +show(): void TA <>-TA(g: graduate, t: teacher) <>-TA(t: TA) +show(): void

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

相关文档