实验三继承与派生.docx
《实验三继承与派生.docx》由会员分享,可在线阅读,更多相关《实验三继承与派生.docx(17页珍藏版)》请在冰点文库上搜索。
实验三继承与派生
武汉科技大学
C++上机实验报告
实验名称:
实验三继承与派生
专业班级:
姓名:
学号:
一、实验目的
1.学习声明和使用类的继承关系,声明派生类。
2.熟悉各种继承方式下对基类成员的访问控制。
二、实验内容
1.声明一个基类Shape,在此基础上派生出Rectangle和Circle类,二者都具有GetArea()函数以计算对象的面积;使用Rectangle类再创建一个派生类Square。
实现并测试这些类。
2.声明一个object类,有数据成员weight及相应的操作函数,由此派生出box类,增加数据成员height和width及相应的操作函数,声明一个box对象,实现并测试这些类,并在相应的构造函数和析构函数中给出提示信息,观察构造函数和析构函数的调用顺序。
3.声明一个基类BaseClass,派生出类DerivedClass。
BaseClass有成员函数fn1()和fn2(),DerivedClass也有成员函数fn1()和fn2()。
两个类的成员函数中分别给出调用此函数时的提示信息。
在主函数中声明一个DerivedClass的类对象,分别用DerivedClass类的对象以及BaseClass和DerivedClass类的指针来调用fn1()和fn2(),观察并分析运行结果。
4.从people类派生出student类,添加属性:
班号charclassNO[7];从people类派生出teacher类,添加属性:
职务charprincipalship[11]、部门chardepartment[21]。
从student类中派生出graduate类,添加属性:
专业charsubject[21]、导师teacheradvisor;从graduate类和teacher类中派生出TA(助教)类,注意虚基类的使用。
用成员函数实现对人员信息的录入和显示。
附:
people类的属性:
number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。
其中“出生日期”声明为一个“日期”类内嵌子对象。
三、实验注意事项
程序中,需要编写一个可执行函数与main主调函数,自主设计输入、输出值,使得结果可以由控制台显示输出。
四、实验程序代码
(一)
#include
usingnamespacestd;
classShape
{
public:
Shape(){}//shape类只有面积是通用的属性
~Shape(){}
floatGetArea();
};
classCircle:
publicShape
{
public:
Circle(floatr):
radius(r){}//将radius作为float的一个类对象赋初值
~Circle(){};
floatGetArea()
{
return3.14f*radius*radius;
}
private:
floatradius;
};
classRectangle:
publicShape
{
public:
Rectangle(floatlen,floatwidth):
m_len(len),m_width(width){}//同上
~Rectangle(){}
floatGetArea()
{
returnm_len*m_width;
}
private:
floatm_len,m_width;
};
classSquare:
publicRectangle
{
public:
Square(floatlen):
Rectangle(len,len){}
~Square(){}
};
intmain(void)
{
Rectanglerec(1,2);
Circlecir(3);
Squaresqu(4);
cout<<"矩形面积为:
"<cout<<"圆形面积为:
"<cout<<"正方形面积为:
"<return0;
}
(二)
#include
usingnamespacestd;
classObject
{
public:
Object(floatw=0){cout<<"CallingtheconstructorofObject."<voidinitObject(floatweight=0)
{
this->weight=weight;
}
~Object(){cout<<"CallingthedestructorofObject."<private:
floatweight;
};
classBox:
publicObject
{
public:
Box(floatweg,floatlen,floatwide,floathigh)
{
cout<<"CallingtheconstructorofBox."<initObject(weg);
length=len;
width=wide;
height=high;
}
~Box(){cout<<"CallingthedestructorofBox."<private:
floatheight;
floatwidth;
floatlength;
};
intmain(void)
{
BoxB(10,20,30,5);
return0;
}
(三)
#include
usingnamespacestd;
classBaseClass
{
public:
voidfn1(void)
{
cout<<"CallingBaseclassfn1"<}
voidfn2(void)
{
cout<<"CallingBaseclassfn2"<}
};
classDerivedClass:
publicBaseClass
{
public:
voidfn1(void)
{
cout<<"CallingDerivedClassfn1"<}
voidfn2(void)
{
cout<<"CallingDerivedClassfn2"<}
};
intmain(void)
{
DerivedClassd,*dp=&d;
d.fn1();//对象调用
d.fn2();
dp->fn1();//指针调用
dp->fn2();
BaseClassb,*bp=&b;
bp->fn1();//指针调用
bp->fn2();
return0;
}
(四)
#include
#include
usingnamespacestd;
classDate//日期类
{
public:
Date(){}//默认构造
Date(inty,intm,intd)//带参构造
{
year=y;
month=m;
day=d;
}
voidset()//设置数据函数
{
cin>>year>>month>>day;
}
voiddisplay()//显示函数
{
cout<}
private:
intyear;
intmonth;
intday;
};
classPerson //人员类
{
private:
string name;
int num;
char sex[10];
Date birthday;
char ID[18];
public:
Person(){} //默认构造
Person(int n,int y,int m,int d,char id[18],char sex[10]):
birthday(y,m,d)
{
num=n;
strcpy(ID,id);
}
Person(Person& p) //拷贝构造
{
name=p.name;
num=p.num;
birthday=p.birthday;
strcpy(ID,p.ID);
}
void input() //输入函数
{
cout<<"姓名:
";
cin>>name;
cout<<"编号:
";
cin>>num;
cout<<"性别(男/女):
";cin>>sex;cout<<"生日:
";birthday.set();cout<<"身份证号:
";cin>>ID;ID[18]='\0';cout<}
voidoutput()//输出函数
{
cout<<"编号:
"<"<"<";
birthday.display();
cout<<"身份证号:
"<}
~Person()//析构函数
{
cout<<"***"<}
};
classstduent:
publicPerson
{
charclassno[7];
public:
student()
{
cout<<"*************"<}
voidinput()
{
Person:
:
input();
cout<<"输入学号"<cin>>classno;
}
voidgetno()
{
Person:
:
output();
cout<<"学号为:
"<}
};
classteacher:
publicPerson
{
charpship[11],
departt[21];
public:
teacher()
{
cout<<"***********"<}
voidinput()
{
Person:
:
input();
cout<<"输入职务"<cin>>pship;
cout<<"输入部门"<cin>>departt;
}
voidinputt()
{
cout<<"输入职务"<cin>>pship;
cout<<"输入部门"<cin>>departt;}
voidgetno()
{
Person:
:
output();
cout<<"职务为:
"<cout<<"部门为:
"<}
voidoutput()
{
cout<<"职务为:
"<cout<<"部门为:
"<}
};
classgraduate:
publicstduent
{
charsubject[21],adviser[21];
public:
graduate()
{
cout<<""<}
voidinput()
{
stduent:
:
input();
cout<<"输入专业:
"<cin>>subject;
cout<<"输入导师:
"<>adviser;
}
voidgetno()
{
stduent:
:
getno();
cout<<"专业为:
"<cout<<"导师为:
"<}
};
classTA:
publicgraduate,teacher
{
public:
TA(){}
voidinput()
{
graduate:
:
input();
teacher:
:
inputt();
}
voidgetno()
{
graduate:
:
getno();
teacher:
:
output();
}
};
intmain()
{
Personp1;
stduents;
teachert;
graduateg;
TAT;
cout<<"请依次输入人员数据信息"<p1.input();
cout<<"请输入学生数据信息";
s.input();
cout<<"请输入老师数据信息";
t.input();
cout<<"请输入研究生数据信息";
g.input();
cout<<"请输入助教博士数据信息";
T.input();
cout<<"人员数据信息为:
";
p1.output();
cout<<"学生数据信息为:
";
s.getno();
cout<<"老师信息为:
";
t.getno();
cout<<"研究生信息为:
";
g.getno();
cout<<"助教博士信息为:
";
T.getno();
return0;
}
五、实验运行结果
(一)
(二)
(三)
(四)
六、实验心得与体会
通过这次试验我掌握了类的继承与派生的基本用法,一些细节总结大多在程序的注释中。
调用构造函数的顺序是先基类,再派生类。
而析构函数的顺序与之相反。
引用可以通过对象名,也可以通过引用名。
类的继承与派生是C++相对于C语言的又一大优势,大大提高了程序编程的效率。
而最后一道编程题,更是前面所学知识的大集合,也重新温习了一些C中的字符串函数如:
strcpy,strcmp等等,收获颇多。