实验四类的继承与多态实验.docx

上传人:b****2 文档编号:24520595 上传时间:2023-05-28 格式:DOCX 页数:16 大小:17.44KB
下载 相关 举报
实验四类的继承与多态实验.docx_第1页
第1页 / 共16页
实验四类的继承与多态实验.docx_第2页
第2页 / 共16页
实验四类的继承与多态实验.docx_第3页
第3页 / 共16页
实验四类的继承与多态实验.docx_第4页
第4页 / 共16页
实验四类的继承与多态实验.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

实验四类的继承与多态实验.docx

《实验四类的继承与多态实验.docx》由会员分享,可在线阅读,更多相关《实验四类的继承与多态实验.docx(16页珍藏版)》请在冰豆网上搜索。

实验四类的继承与多态实验.docx

实验四类的继承与多态实验

实验四类的继承与多态实验

【实验目的】

1.理解软件重用性的一种形式——继承。

2.能够通过继承已有的类创建新类。

3.理解基类和派生类的概念。

4.能够在派生类中使用构造函数和析构函数

5.学习虚基类在解决二义性问题中的作用。

6.熟悉多态分类,理解静态联编和动态联编概念。

7.掌握运算符重载方法。

8.理解虚函数、纯虚函数和抽象类概念。

9.掌握用抽象类和多态性实现编程的方法。

【实验内容】

1.分析程序,写出下列程序的运行结果:

(1)#include

classBase

{

private:

intbase_priv_dat;

protected:

intbase_prot_dat;

public:

voidbase_show();

};

classDerived:

publicBase

{

private:

intderived_priv_dat;

public:

voidderived_show();

};

voidBase:

:

base_show()

{

base_priv_dat=1;//基类函数可以操纵私有和保护型基类数据

base_prot_dat=2;

cout<<"base_priv_dat="<

<<"base_prot_dat="<

}

voidDerived:

:

derived_show()

{

derived_priv_dat=3;

base_prot_dat=4;//派生函数可以处理保护型基类数据,但不能处理私有基类数据

cout<<"derived_priv_dat="<

<<"base_prot_dat="<

base_show();//派生函数可以调用公有基类函数

}

intmain()

{

Derivedd_obj;

d_obj.base_show();//可用派生对象调用基类函数

d_obj.derived_show();

return(0);

}

(2)#include

#include

classPerson

{

public:

Person(constchar*s)//带参数的构造函数

{

name=newchar[strlen(s)+1];

strcpy(name,s);

}

~Person(){delete[]name;}//析构函数做清理工作

char*GetName(){returnname;}

protected:

char*name;

};

classStudent:

publicPerson

{

char*major;

public:

Student(constchar*s,constchar*m):

Person(s)//派生类构造函数

{major=newchar[strlen(m)+1];strcpy(major,m);}

~Student(){delete[]major;}//派生类析构函数

char*GetMajor(){returnmajor;}

};

intmain()

{

Studentstu("WZQ","Electricautomatization");

cout<<"studentNameis:

"<

"

<

return(0);

}

2.编译运行下列程序,分析出现编译错误的原因,并给出解决办法。

#include

classBase

{

protected:

inta;

public:

Base(){a=30;}

};

classB1:

publicBase

{

public:

B1(){cout<<"B1:

:

a:

"<

};

classB2:

publicBase

{

public:

B2(){cout<<"B2:

:

a:

"<

};

classDerived:

publicB2,publicB1

{

public:

Derived(){cout<<"Derived:

:

a:

"<

};

intmain()

{

Derivedd;

return(0);

}

3.开发一个简单的大学人员管理程序,该程序可以管理大学的一些基本人员:

学生(student)、教师(teacher)、教授(professor)。

首先设计一个虚基类person。

通过该类保存人员的最基本信息:

姓名(name)、年龄(age)和性别(sex)。

然后使用该类派生出学生类student和教师类teacher,在其中添加各自的特性,如在student类中添加如下信息:

专业(speciality),在teacher类中添加院系(department)等。

还有部分教师在工作的同时在职修读学位,因此同时具有教师和学生双重身份,所以由student类和teacher类再次派生出stuTeacher类。

为每个类定义一个输出函数print(),输出该类相关信息。

4.用运算符重载设计有理分数类,实现+、-、*、/等操作。

提示:

创建一个有理分数类Rational,有两个数据成员,一个构造函数,四个运算符重载函数,一个显示函数和一个约简函数。

其中数据成员有分子numerator和分母denominator,构造函数可以避免分母为0,对不是约化型的分数进行约化。

显示函数显示计算结果。

约简函数对分数进行化简。

5.用运算符重载设计复数类,实现复数的+、-、*、/运算。

6.某学校对教师每月工资的计算规定如下:

固定工资+课时补贴。

教授的固定工资为5000元,每个课时补贴50元。

副教授的固定工资为3000元,每个课时补贴30元。

讲师的固定工资为2000元,每个课时补贴20元。

定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。

注:

4、5选做一题。

【思考题】

1.组合与继承的区别是什么?

2.公有继承、保护继承和私有继承分别在什么情况下使用?

3.什么是虚基类?

有何作用?

4.静态联编和动态联编有什么区别?

5.简述空的虚函数与纯虚函数的区别?

6.简述抽象类和具体类的区别?

#include

classBase

{

private:

intbase_priv_dat;

protected:

intbase_prot_dat;

public:

voidbase_show();

};

classDerived:

publicBase

{

private:

intderived_priv_dat;

public:

voidderived_show();

};

voidBase:

:

base_show()

{

base_priv_dat=1;//基类函数可以操纵私有和保护型基类数据

base_prot_dat=2;

cout<<"base_priv_dat="<

<<"base_prot_dat="<

}

voidDerived:

:

derived_show()

{

derived_priv_dat=3;

base_prot_dat=4;//派生函数可以处理保护型基类数据,但不能处理私有基类数据

cout<<"derived_priv_dat="<

<<"base_prot_dat="<

base_show();//派生函数可以调用公有基类函数

}

intmain()

{

Derivedd_obj;

d_obj.base_show();//可用派生对象调用基类函数

d_obj.derived_show();

return(0);

}

#include

#include

classPerson

{

public:

Person(constchar*s)//带参数的构造函数

{

name=newchar[strlen(s)+1];

strcpy(name,s);

}

~Person(){delete[]name;}//析构函数做清理工作

char*GetName(){returnname;}

protected:

char*name;

};

classStudent:

publicPerson

{

char*major;

public:

Student(constchar*s,constchar*m):

Person(s)//派生类构造函数

{major=newchar[strlen(m)+1];strcpy(major,m);}

~Student(){delete[]major;}//派生类析构函数

char*GetMajor(){returnmajor;}

};

intmain()

{

Studentstu("WZQ","Electricautomatization");

cout<<"studentNameis:

"<

"

<

return(0);

}

#include

classBase

{

protected:

inta;

public:

Base(){a=30;}

};

classB1:

virtualpublicBase

{

public:

B1(){cout<<"B1:

:

a:

"<

};

classB2:

virtualpublicBase

{

public:

B2(){cout<<"B2:

:

a:

"<

};

classDerived:

publicB2,publicB1

{

public:

Derived(){cout<<"Derived:

:

a:

"<

};

intmain()

{

Derivedd;

return(0);

}

#include

#include

#include

classperson

{

public:

voidprint();

person(chara[],intb,charc[]);

protected:

charname[20];

intage;

charsex[5];

};

person:

:

person(chara[],intb,charc[])

{

strcpy(name,a);

age=b;

strcpy(sex,c);

}

voidperson:

:

print()

{

cout<<"姓名"<<""<

}

classteacher:

virtualpublicperson

{

public:

teacher(chara[],intb,charc[],chard[]);

voidprint();

protected:

chardepartment[50];

};

teacher:

:

teacher(chara[],intb,charc[],chard[]):

person(a,b,c)

{

strcpy(department,d);

}

voidteacher:

:

print()

{

person:

:

print();

cout<

}

classstudent:

virtualpublicperson

{

public:

student(chara[],intb,charc[],chard[]);

voidprint();

protected:

charspeciality[50];

};

student:

:

student(chara[],intb,charc[],chard[]):

person(a,b,c)

{

strcpy(speciality,d);

}

voidstudent:

:

print()

{

person:

:

print();

cout<

}

classprofessor:

publicperson

{

public:

voidprint();

professor(chara[],intb,charc[],chard[],chare[]);

protected:

charposition[50];

chardepartment[50];

};

professor:

:

professor(chara[],intb,charc[],chard[],chare[]):

person(a,b,c)

{

strcpy(position,d);

strcpy(department,e);

}

voidprofessor:

:

print()

{

person:

:

print();

cout<

}

classstuTeacher:

publicteacher,publicstudent

{

public:

stuTeacher(chara[],intb,charc[],chard[],chare[]);

voidprint();

};

stuTeacher:

:

stuTeacher(chara[],intb,charc[],chard[],chare[]):

person(a,b,c),student(a,b,c,d),teacher(a,b,c,e)

{}

voidstuTeacher:

:

print()

{

person:

:

print();

cout<

}

intmain()

{

teachera("zhang",30,"男","电院");

a.print();

studentb("刘道广",20,"男","计算机");

b.print();

professorc("wang",40,"女","教授","材工");

c.print();

stuTeacherd("zhou",25,"女","国际贸易","管院");

d.print();

return(0);

}

#include

#include

#include

floatm,n;

classfraction

{

public:

fraction(floatx=1,floaty=1);

fractionoperator+(fraction&c);

fractionoperator-(fraction&c);

fractionoperator*(fraction&c);

fractionoperator/(fraction&c);

voiddisplay(void);

private:

floata;

floatb;

};

fraction:

:

fraction(floatx,floaty)

{

a=x;

b=y;

}

fractionfraction:

:

operator+(fraction&c)

{

m=a+c.a;

n=b+c.b;

return(fraction(m,n));

}

fractionfraction:

:

operator-(fraction&c)

{

m=a-c.a;

n=b-c.b;

return(fraction(m,n));

}

fractionfraction:

:

operator*(fraction&c)

{

m=a*c.a-b*c.b;

n=a*c.b+b*c.a;

return(fraction(m,n));

}

fractionfraction:

:

operator/(fraction&c)

{

if(c.a==0&&c.b==0)

{

cout<<"输入错误"<

exit(0);

}

m=(a*c.a+b*c.b)/(c.a*c.a+c.b*c.b);

n=(b*c.a-a*c.b)/(c.a*c.a+c.b*c.b);

return(fraction(m,n));

}

voidfraction:

:

display()

{

cout<

}

intmain()

{

fractionx(3,4),y(1,2),z;

z=x+y;

z.display();

z=x-y;

z.display();

z=x*y;

z.display();

z=x/y;

z.display();

return(0);

}

运行结果:

4+6i

2+2i

-5+10i

2.2+-0.4i

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 英语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1