文档库 最新最全的文档下载
当前位置:文档库 › C++继承与派生

C++继承与派生

C++继承与派生
C++继承与派生

第4章继承与派生

一、简答题

1. 有以下程序结构,请分析访问属性。

class A //A为基类

{public:

void func1( );

int i;

protected:

void func2( );

int j;

private:

int k;

} ;

class B: public A //B为A的公用派生类

{public :

void func3( ) ;

protected:

int m;

private :

int n;

};

class C: public B // C为B的公用派生类

{public:

void func4( );

private:

int p;

};

int main( )

{ A a; //a是基类A的对象

B b; //b是派生类B的对象

C c; //c是派生类C的对象

return 0;

}

问:

(1)在main函数中能否用b.i,b.j 和b.k 访问派生类B对象b中基类A的成员?

(2)派生类B中的成员函数能否调用基类A中的成员函数func1和func2?

(3)派生类B中的成员函数能否访问基类A中的数据成员i,j,k?

(4)能否在main函数中用c.i,c.j,c.k,c.m,c.n,c.p访问基类A的成员i,j,k,派生类B的成员m,n,以及派生类C的成员p?

(5)能否在main函数中用c. func1( ),c. func2( ),c. func3( )和c. func4( )调用func1,func2,func3,func4成员函数?

(6)派生类C的成员函数func4能否调用基类A中的成员函数func1,func2和派生类B中的成员函数func3?

【答案要点】各成员在各类的范围内的访问权限如下表:

(1)在main 函数中能用b.i访问派生类B对象b中基类A的成员i,因为它在派生类B中是公用数据成员。

不能用b.j访问派生类B对象b中基类A的成员j,因为它在派生类B中是保护数据成员,不能被类外访问。

不能用b.k访问派生类B对象b中基类A的成员k,因为它是基类A的私用数据成员,只有基类A的成员函数可以访问,不能被类外访问。

(2)派生类B中的成员函数能调用基类A中的成员函数func1和func2,因为func1、func2在派生类B中是公用成员和保护成员,可以被派生类的成员函数访问。

(3)派生类B中的成员函数能访问基类A中的数据成员i、j,因为i、j在派生类B中是公用成员和保护成员,可以被派生类的成员函数访问。

派生类B中的成员函数不能访问基类A中的数据成员k,因为它在派生类B中是不可访问的成员。

(4)能在main 函数中用c.i访问基类A的成员i,不能用c.j、c.k访问基类A的成员j、k,因为它们在派生类C中是保护成员和私有成员,不能被类外访问。也不能用c.m、c.n 访问派生类B的成员m、n,因为它们在派生类C中也是保护成员和私有成员,不能被类外访问。也不能用c.p访问派生类C中的私用成员p。

(5)能在main函数中用c.func1()、c. func3()和c. func4()调用func1、func3、func4成员函数,因为它们在派生类C中是公用成员函数,可以在类外被访问。

不能在main函数中用c.func2()调用func2成员函数,因为它在派生类C中是保护成员函数,不能在类外被访问。

(6)派生类C的成员函数func4能调用基类A中的成员函数func1、func2和派生类中的成员函数func3,因为func1、func3在派生类C中是公用成员函数,func2在派生类C中是保护成员函数,都可以被派生类C的成员函数调用。

2. 已给商品类及其多层的派生类。以商品类为基类。第一层派生出服装类、家电类、车辆类。第二层派生出衬衣类、外衣类、帽子类、鞋子类;空调类、电视类、音响类;自行车类、轿车类、摩托车类。请给出商品类及其多层派生类的基本属性和派生过程中增加的属性。

【答案要点】

按题意没有操作,所以只列出各个类的数据成员,也不再在main函数中对各类进行测试。

#include

using namespace std;

class Commodity{

double price; //价格

char name[20]; //商品名

char manufacturer[20]; //生产厂家

int items; //数量

};

class Clothing:public Commodity{ //服装类

char texture[20]; //材料质地

};

class ElectricAppliance:public Commodity{ //家电类

enum {Black,White}type; //黑白家电

};

class Vehicle:public Commodity{ //车辆类

int wheelNum; //车轮数量

};

class Shirt:public Clothing{ //衬衣类

enum {Formal,Casual}style; //式样:正式、休闲

};

class Garment:public Clothing{//外衣类

enum {Jacket,Coat}style;//式样:夹克、外套

};

class Hat:public Clothing{//帽子类

enum {Winter,Summer,Spring,Autumn}style;//季节风格

};

class Shoes:public Clothing{//鞋子类

enum {Winter,Summer,Spring,Autumn}style;//季节风格

};

class AirCondition:public ElectricAppliance{//空调

bool warmCool; //是否冷暖

float power; //功率

};

class Television:public ElectricAppliance{//电视类

int size; //尺寸

bool isColor; //是否彩色

};

class Acoustics:public ElectricAppliance{//音响类

int speakerNum; //喇叭数目

float power; //功率

};

class Bicycle:public Vehicle{//自行车类

int speedGrades; //调速级数

int wheelSize; //轮子大小

};

class Car:public Vehicle{//轿车类

float volume; //排气量

bool isSkyLight; //是否有天窗

int boxNum; //厢数

};

class Motorcycle:public Vehicle{//摩托车类

float volume; //排气量

};

int main(){ return0; }

二、编程题

1.定义一个国家基类Country,包含国名、首都、人口等属性,派生出省类Province,增加省会城市、人口数量属性。

【程序参考代码】

#include

using namespace std;

#include

class Country

{public:

Country(char *n,char *c,double p)

{ strcpy(name,n); strcpy(capital,c); population=p; }

void print(){ cout<

private:

char name[10],capital[50];

double population;

};

class Province:private Country

{public:

Province(char *n,char *c,double p,char *cc,double pp):Country(n,c,p)

{ strcpy(provinceCapital,cc); provincePopulation=pp; }

void display()

{ print();

cout<

}

private:

char provinceCapital[50];

double provincePopulation;

};

int main()

{

Province p("China","Beijing",1.36e+010,"guang dong",1.05e+009);

p.display();

return 0;

}

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

【程序参考代码】

#include

#include

using namespace std;

class Person //声明公共基类Person {public:

void set(){ cin >>name>>sex>>age; } //姓名、性别、年龄的输入void display(){ cout<<"name="<

//姓名、性别、年龄的显示private:

string name; //姓名

char sex; //性别

int age;//年龄

};

class Student: public Person //声明Student类为Person类的公用派生类

{public:

void input()

{ Person::set();//姓名、性别、年龄的输入

cin>>stuId>>stuClass>>profession>>score;//学号、班级、专业、入学成绩的输入}

void display()

{ Person::display();//姓名、性别、年龄的显示

cout<<"\tstuId="<

<<"\tprofession ="<

//学号、班级、专业、入学成绩的显示}

private:

stringstuId;//学号

string stuClass; //班级

string profession; //专业

float score;//入学成绩

};

class Teacher: public Person //声明Teacher类为Person类的公用派生类

{public:

void set()

{ Person::set();//姓名、性别、年龄的输入

cin>>teachId>> title>> wage;//工号、职称、工资的输入

}

void display()

{ Person::display(); //姓名、性别、年龄的显示

cout<<"\tteachId="<

<<"\twage="<

//工号、职称、工资的显示}

private:

stringteachId;//工号

string title; //职称

float wage; //工资

};

int main( )

{ Student student;

Teacher teacher;

cout<<"Please enter student's name,sex,age,stuId,stuClass,profession and score:"<

student.input();

cout<<"Display student's name,sex,age,stuId,stuClass,profession and score:"<

student.display();

cout<<"Please enter teacher's name,sex,age,teachId, title and wage:"<

cout<<"Display teacher's name,sex,age,teachId, title and wage:"<

teacher.display();

return 0;

}

3.设计一个基类——Building类,有楼房的层数、房间数和总面积,再由基类派生出教学楼——TeachBuilding类和宿舍楼类——DormBuilding类,教学楼类增加教室数,宿舍楼类增加宿舍数、容纳学生总人数。

【程序参考代码】

#include

using namespace std;

class Building{

public:

Building(int f,int r,double a) { floors=f; rooms=r; area=a; }

protected:

int floors;

int rooms;

double area;

};

class TeachBuilding:public Building{

public:

TeachBuilding (int f,int r,double a,int cr):Building(f,r,a){ classrooms=cr; } void show()

{

cout<<"floors="<

<<"classrooms="<

}

private:

int classrooms;

};

class DormBuilding:public Building{

public:

DormBuilding (int f,int r,double a,int d,int sc) :Building(f,r,a)

{ dormitories =d; studcount =sc; }

void show()

{

cout<<"floors="<

<<"dormitories="<

<<"studcount="<

}

private:

int dormitories;

int studcount;

};

int main()

{

TeachBuilding TB(6, 80,200,35);

TB.show();

DormBuilding DB(6, 80,200,35,300);

DB.show();

return 0;

}

4.定义一个基类——汽车类,有型号、颜色、发动机功率、车速、重量、车牌号码,再由汽车类派生出客车类和货车类,客车类增加客车座位数、客运公司,货车类增加载货重量、货运公司。

【程序参考代码】

#include

#include

using namespace std;

class Car

{public:

void show();

protected:

string model; //型号

int color; //颜色

unsigned int motopower;//发动机功率

unsigned int speed; //车速

unsigned int weight; //重量

unsigned int id; //车牌号码

};

class Bus: public Car

{public:

void show();

Bus( string _model, int _color, unsigned int _motopower, unsigned int _speed, unsigned int _weight,unsigned int _id, unsigned int _seatnum, string _corp) {

model = _model; color = _color; motopower = _motopower; speed = _speed;

weight = _weight; id = id; _seatnum = _seatnum; corp = _corp;

}

private:

unsigned int seatnum; //客车座位数

string corp; //客运公司

};

class Truck : public Car

{public:

void show();

Truck(string _model, int _color, unsigned int _motopower,

unsigned int _speed,unsigned int _weight, unsigned int _id,

unsigned int _load, string _corp)

{ model = _model; color = _color; motopower = _motopower; speed = _speed;

weight = _weight; id = _id; load = _load; corp = _corp; }

private:

unsigned int load; //载货重量

string corp; //货运公司

};

void Car::show()

{

cout<<"型号:"<

cout<<"颜色:"<

cout<<"发动机功率:"<

cout<<"车速:"<

cout<<"重量:"<

cout<<"车牌号码:"<

}

void Bus::show()

{

cout<<"型号:"<

cout<<"颜色:"<

cout<<"发动机功率:"<

cout<<"车速:"<

cout<<"重量:"<

cout<<"车牌号码:"<

cout<<"客车座位数:"<

cout<<"客运公司:"<

}

void Truck::show(){

cout<<"型号:"<

cout<<"颜色:"<

cout<<"发动机功率:"<

cout<<"车速:"<

cout<<"重量:"<

cout<<"车牌号码:"<

cout<<"载货重量:"<

cout<<"货运公司:"<

}

int main( int argc, char** argv )

{

Car *a = new Bus("黄海",3,300,100,2000,10101010,50,"北京客运");

Car *b = new Truck("东风",1,400,200,5000,10101011,3000,"北京货运");

cout<<"==============="<

a->show();

cout<<"==============="<

b->show();

system("pause");

return 0;

}

5.定义一个Table类和Circle类,再由它们共同派生出RoundTable类。

【程序参考代码】

#include

using namespace std;

#include

class Circle

{public:

Circle(double r){radius=r;}

double getArea()

{return 3.1416*radius*radius;}

private:

double radius;

};

class Table

{public:

Table(double h){height=h;}

double getHeight(){return height;}

private:

double height;

};

class RoundTable:public Table,public Circle

{public:

RoundTable(double h,double r,char c[]):Table(h),Circle(r)

{

color=new char[strlen(c) +1];

strcpy(color,c);

}

char* getColor(){return color;}

private:

char *color;

};

int main()

{

RoundTable rt(0.8,1.0,"白色");

cout<<"圆桌的高:"<

cout<<"圆桌面积:"<

cout<<"圆桌颜色:"<

return 0;

}

6.在第4题的基础上,由客车类和货车类再派生出一个客货两用车类,一辆客货两用车既具有客车的特征(有座位,可以载客),又具有货车的特征(有装载车厢,可以载货)。要求将客货两用车类的间接共同基类——汽车类声明为虚基类。

【程序参考代码】

#include

#include

using namespace std;

class Car

{public:

Car(string _model, int _color, unsigned int _motopower, unsigned int _speed, unsigned int _weight,unsigned int _id)

{

model = _model; color = _color; motopower = _motopower;

speed = _speed; weight = _weight; id = _id;

}

void show();

private:

string model;

int color;

unsigned int motopower;

unsigned int speed;

unsigned int weight;

unsigned int id;

};

class Bus: virtual public Car

{public:

Bus( string model, int color, unsigned int motopower, unsigned int speed, unsigned int weight,unsigned int id, unsigned int _seatnum,

string _corp ):Car(model,color, motopower,speed,weight,id) {

seatnum = _seatnum; corp = _corp;

}

void display();

protected:

unsigned int seatnum;

string corp;

};

class Wagon : virtual public Car

{public:

Wagon( string model, int color, unsigned int motopower, unsigned int speed, unsigned int weight, unsigned int id, unsigned int _load,

string _corp ): Car(model,color, motopower,speed,weight,id) { load = _load; corp = _corp; }

void display();

protected:

unsigned int load;

string corp;

};

class StationWagon : public Bus,public Wagon

{public:

StationWagon( string model, int color, unsigned int motopower,

unsigned int speed,unsigned int weight, unsigned int id,

unsigned int seatnum,unsigned int load, string corp ):

Car(model,color, motopower,speed,weight,id),

Bus(model,color, motopower,speed,weight,id,seatnum,corp),

Wagon(model,color, motopower,speed,weight,id,load,corp) { } void display();

};

void Car::show()

{

cout<<"型号:"<

cout<<"颜色:"<

cout<<"发动机功率:"<

cout<<"车速:"<

cout<<"重量:"<

cout<<"车牌号码:"<

}

void Bus::display()

{

show();

cout<<"客车座位数:"<

cout<<"客运公司:"<

}

void Wagon::display(){

show();

cout<<"载货重量:"<

cout<<"货运公司:"<

}

void StationWagon::display(){

show();

cout<<"客车座位数:"<

cout<<"载货重量:"<

cout<<"客运及货运公司:"<

}

int main( )

{

StationWagon sw("黄海",3,300,100,2000,10101010,50,3000,"北京客货运");

sw.display();

system("pause");

return 0;

}

7.设计一个雇员类Employee,存储雇员的编号、姓名和生日等信息,要求该类使用日期类作为成员对象,雇员类的使用如下:

//定义一个雇员,其雇员号为1111,姓名为Tom,生日为1980年11月20日

Employee Tom("1111","Tom", 1980, 11, 20)

Date today(1980, 11, 20);

if (Tom.isBirthday(today) //判断今天是否为Tom的生日

//…

【程序参考代码】

#include

#include

using namespace std;

class Date{

public:

Date(){ year=0; month=0; day=0; } //无参构造函数

Date(int mm, int dd, int yy){ year=yy; month=mm; day=dd; }//带参数的构造函数Date(const Date &d){ year=d.year; month=d.month; day=d.day; }

//复制构造函数void setDate(const int, const int, const int); //修改日期的函数void display(); //输出日期的函数

int getYear(); //获取年份的函数int getMonth(); //获取月份的函数

int getDay(); //获取日信息的函数

private:

int year, month, day;

};

//设置成员变量,mm:月份;dd:天数;yy:年份;

void Date::setDate(const int mm, const int dd, const int yy)

{ year=yy; month=mm; day=dd;}

void Date::display()

{

cout<

int Date::getYear(){ return year; }

int Date::getMonth(){ return month; }

int Date::getDay(){ return day; }

class Employee

{public:

Employee(char *ID, char *newName,int y,int m,int d);

Employee(const Employee &);

void setData(char *ID, char *newName, int y,int m,int d);

void display();

bool isBirthday(Date day);

private:

char id[10];

char name[20];

Date birthday;

};

Employee::Employee(char *ID, char *newName, int y,int m,int d):birthday(y,m,d) { strcpy(id, ID);

strcpy(name, newName);

}

Employee::Employee(const Employee &other)

{ strcpy(id, other.id);

strcpy(name, https://www.wendangku.net/doc/4914012963.html,);

birthday=other.birthday;

}

void Employee::setData(char *ID, char *newName,int y,int m,int d)

{ strcpy(id, ID);

strcpy(name, newName);

birthday.setDate(y,m,d);

}

void Employee::display()

{cout<<"id="<

cout<<" name="<

birthday.display();

cout<

}

bool Employee::isBirthday(Date day)

{

if(birthday.getYear()==day.getYear()

&& birthday.getMonth()==day.getMonth()

&& birthday.getDay()==day.getDay())

return true;

else return false;

}

int main()

{

Employee Tom("1111","Tom",1980,11,20);

Date today(1980,11,20);

if( Tom.isBirthday(today))

cout<<"Today is Tom's birthday."<

else cout<<"Today isn't Tom's birthday."<

system("pause");

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

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

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

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

福建农林大学金山学院实验报告 系(教研室):信息与机电工程系专业:计算机科学与技术年级: 实验课程:面向对象程序设计姓名:学号:  实验室号 计算机号 实验时间:指导教师签字:成绩: 实验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)

类的继承与派生综合题

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

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

继承与派生类上机实践指导 一.实验目的 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; /* 人员级别*/

相关文档
相关文档 最新文档