};
voidmain()
{Cc1;
}
2.定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。
编写一个完整程序进行测试。
3.定义并描述一个人员类Person,它派生出学生类Student和教师类Teacher,学生类和教师类共同派生出在职读书的教师类Stu_Tech。
人员类有姓名、性别、身份证号、出生年月等信息;学生类有学号、成绩等信息;教师类有职务、职称等信息。
编写一个完整程序进行测试。
三、编程分析及运行结果
1、运行结果:
2、classPoint
{public:
voidinpoint(doublex,doubley)
{Px=x;Py=y;}
doublegetPx()
{returnPx;}
doublegetPy()
{returnPy;}
protected:
doublePx,Py;
};
classRectangle:
publicPoint
{public:
Rectangle(doublex,doubley,doubleh,doublew)
{inpoint(x,y);High=h;Wide=w;}
doublegetHigh(){returnHigh;}
doublegetWide(){returnWide;}
doubleArea(){returnHigh*Wide;}
protected:
doubleHigh,Wide;
};
classCircle:
publicPoint
{public:
Circle(doublex,doubley,doubler)
{inpoint(x,y);R=r;}
doublegetR()
{returnR;}
doubleArea()
{doublepi=3.1415926;returnpi*R*R;}
protected:
doubleR;
};
#include
voidmain()
{doublex,y,h,w,r;
cout<<"请输入矩形的位置坐标(x,y)、长、宽:
";
cin>>x>>y>>h>>w;
RectangleRect1(x,y,h,w);
cout<<"矩形的数据:
"<<<cout<<"请输入圆的位置坐标(x,y)及半径:
";
cin>>x>>y>>r;
CircleC1(x,y,r);
cout<<"圆的数据:
"<<<"),\tR="<}
3、#include
#include
classPerson
{public:
Person();
Person(char*name1,charsex1,char*id1,char*birth);
voiddisplay();
~Person();
protected:
char*name;
char*id;
charsex;
char*birthday;
};
Person:
:
Person()
{name='\0';
sex=0;
id='\0';
birthday='\0';
}
Person:
:
Person(char*name1,charsex1,char*id1,char*birth)
{name=newchar[strlen(name1)+1];
name=strcpy(name,name1);
sex=sex1;
id=newchar[strlen(id1)+1];
id=strcpy(id,id1);
birthday=newchar[strlen(birth)+1];
birthday=strcpy(birthday,birth);
}
voidPerson:
:
display()
{cout<<"name:
"<"<"<"<}
Person:
:
~Person()
{delete[]name;
delete[]id;
delete[]birthday;
}
classStudent:
virtualpublicPerson
{public:
Student(char*name,charsex,char*id,char*birthday,longints_num1,floatscore1);
voiddisplay()
{Person:
:
display();
cout<<"s_number:
"<"<}
~Student(){}
protected:
longints_num;
floatscore;
};
Student:
:
Student(char*name1,charsex1,char*id1,char*birth,longints_num1,floatscore1):
Person(name1,sex1,id1,birth)
{s_num=s_num1;
score=score1;
}
classTeacher:
virtualpublicPerson
{public:
Teacher(char*name1,charsex1,char*id1,char*birth,char*posit,char*title1):
Person(name1,sex1,id1,birth)
{position=newchar[strlen(posit)+1];
position=strcpy(position,posit);
title=newchar[strlen(title1)+1];
title=strcpy(title,title1);
}
voiddisplay()
{Person:
:
display();
cout<<"Position:
"<"<
}
~Teacher()
{delete[]position;
delete[]title;
}
protected:
char*position;
char*title;
};
classStu_teach:
publicStudent,publicTeacher
{public:
Stu_teach(char*name1,charsex1,char*id1,char*birth,longints_num1,floatscore1,char*posit,char*title1):
Person(name1,sex1,id1,birth),
Student(name1,sex1,id1,birth,s_num1,score1),Teacher(name1,sex1,id,birth,posit,title1){}
voiddisplay();
~Stu_teach(){}
};
voidStu_teach:
:
display()
{Student:
:
display();
cout<<"Position:
"<"<
}
voidmain()
{Student
Teacherteacher","Professor");
Stu_teachteacher","Professor");
s1.display();
t1.display();
s_t1.display();
}
四、解决方法及难点
难点:
用虚基类解决继承和派生时的二义性问题..通过虚基类来实现.
实验八利用虚函数编程
一、实验目的
1.了解多态性的概念和虚函数的作用及使用方法。
2.了解静态关联和动态关联的概念和用法。
3.了解纯虚函数和抽象类的概念和用法。
4.学习使用虚函数的继承实现动态关联。
5.学习理解静态数据成员和静态成员函数的使用。
二、实验内容
1.事先阅读程序,给出其运行结果,上机验证虚函数的作用。
有如下程序:
#include
#include
classStudent//定义基类
{public:
Student(int,string,float);
voiddisplay();//声明输出函数
protected:
//受保护成员,派生类可访问
intnum;
stringname;
floatscore;
};
Student:
:
Student(intn,stringnam,floats)
{num=n;name=nam;score=s;}
voidStudent:
:
display()
{cout<
num:
?
<\nname:
?
<\nscore:
?
<classGraduate:
publicStudent//声明公有派生类
{public:
Graduate(int,string,float,flaot);
voiddisplay();//声明输出函数
private:
floatpay;
};
Graduate:
:
Graduate(intn,stringnam,floats,flaotp):
Student(n,nam,s),pay(p){}
voidGraduate:
:
display()
{cout<<
num:
?
<\nname:
?
<\nscore:
?
<\npay:
?
<voidmain()
{Studentstud(1001,?
Li?
87.5);
Graduategrad(2001,?
Wang?
98.5,563.5);
Student*pt=&stud;
pt->display();
pt=&grad;
pt->display();
}