面向对象程序设计C++Word文档格式.docx
《面向对象程序设计C++Word文档格式.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计C++Word文档格式.docx(31页珍藏版)》请在冰豆网上搜索。
在主函数中进行测试。
三、实验源程序和运行结果
实验
(一)源程序:
#include<
iostream>
string>
usingnamespacestd;
classDocument
{
public:
Document(){};
~Document();
Document(char*name);
char*Name;
voidPrintNameOf();
};
Document:
:
Document(char*name)
Name=newchar[strlen(name+1)];
strcpy(Name,name);
}
~Document(){
delete[]Name;
voidDocument:
PrintNameOf()
cout<
<
Name<
endl;
classBook:
publicDocument
intPageCount;
Book(char*a,intb):
Document(a)
{
PageCount=b;
}
voidmain()
charBookName[20];
intn;
"
请输入书名:
cin>
>
BookName;
请输入书的页数:
n;
Bookb(BookName,n);
书名为:
b.Name<
页数为:
b.PageCount<
运行结果:
实验
(二)源程序:
classperson{
public:
person(){
name="
张三"
;
age=0;
}
person(stringc,inta){
name=c;
age=a;
~person(){}
voidsetname(stringc){
stringgetname(){
returnname;
voidsetage(inta){
intgetage(){
returnage;
private:
stringname;
intage;
classstudent:
publicperson
student(){
xh=0;
student(intd){
xh=d;
student(stringc,inta,intd):
person(c,a){
~student(){}
voidsetxh(intd){
intgetxh(){
returnxh;
intxh;
voidmain(){
stringc;
请输入学生的姓名:
\n"
c;
请输入学生的年龄:
inta;
a;
请输入学生的学号:
intd;
d;
studentn(c,a,d);
请输入学生的姓名为:
"
n.getname()<
请输入学生的年龄为:
n.getage()<
请输入学生的学号为:
n.getxh()<
实验(三)源程序:
classdate{
date(){
year=2011;
month=12;
day=17;
~date(){}
date(inty,intm,intd){
year=y;
month=m;
day=d;
intgetyear(){
returnyear;
intgetmonth(){
returnmonth;
intgetday(){
returnday;
intyear;
intmonth;
intday;
publicperson{
//birthday.date();
student(inty,intm,intd):
birthday(y,m,d)
{
voidgetbirthday(){
cout<
学生的生日为:
birthday.getyear()<
年"
birthday.getmonth()<
月"
birthday.getday()<
日"
datebirthday;
classteacher:
teacher(){
//birthday.date();
teacher(inty,intm,intd):
//birthday.date(y,m,d);
~teacher(){}
老师的生日为:
请输入学生的生日:
inty,m,d;
y>
m>
students(y,m,d);
请输入老师的生日:
teachert(y,m,d);
s.getbirthday();
t.getbirthday();
实验6多继承
1.掌握多基继承的使用,访问方法;
2.理解类层次中访问规则;
3.掌握虚基类的定义及使用。
1.定义一个学生类Student和教师类Teacher,学生类有姓名、学号、私有数据成员,教师类有姓名、工作证号、职称、课程、周学时数。
再定义一个助教类TA,继承学生类和教师类,该类可以使用学生类的全部数据成员,以及教师类的课程和周学时数的数据成员。
要求:
每个类提供自定义的构造函数和析构函数,并通过同名函数ShowInfo来显示全部数据成员的值。
2.设计一个虚基类Person,包含姓名和年龄私有数据成员以及相关的成员函数;
由它派生出领导类Leader,包含职务和部门私有数据成员以及相关的成员函数;
再由Person派生出工程师类Engineer,包含职务和专业私有数据成员以及相关的成员函数;
再由Leader和Engineer类派生出主任工程师类Chairman。
并采用相关数据进行测试。
iostream.h>
string.h>
classStudent{
protected:
chars_name[20];
intid_s;
Student(char*name,intid);
voidShowInfo();
classTeacher{
chart_name[20];
intid_t;
charposition[30];
charlesson[30];
inthour;
Teacher(char*pos,inth);
Teacher(char*name,intid,char*less,char*pos,inth);
classTA:
publicStudent,publicTeacher{
TA(char*name,charid,char*less,inth);
Student:
Student(char*name,intid){
strcpy(s_name,name);
id_s=id;
voidStudent:
ShowInfo(){
姓名:
s_name<
'
\t'
学号:
id_s<
Teacher:
Teacher(char*less,inth){
strcpy(lesson,less);
hour=h;
Teacher(char*name,intid,char*les