實驗 6 多型性和虛擬函式
實驗目的和要求
瞭解靜態聯編和動態聯編的概念。掌握動態聯編的條件。
實驗內容
1、分析並除錯下列程式。
//sy6_1.cpp
#include<iostream>
using namespace std;
class Base
{
public:
virtual void f(float x){cout<<"Base::f(float)"<<x<<endl;}
void g(float x){cout<<"Base::g(float)"<<x<<endl;}
void h(float x){cout<<"Base::h(float)"<<x<<endl;}
};
class Derived:public Base
{
public:
virtual void f(float x){cout<<"Derived::f(float)"<<x<<endl;}
void g(float x){cout<<"Derived::g(float)"<<x<<endl;}
void h(float x){cout<<"Derived::h(float)"<<x<<endl;}
};
int main()
{
Derived d;
Base *pb=&d;
Derived *pd=&d;
pb->f(3.14f);
pd->f(3.14f);
pb->g(3.14f);
pb->h(3.14f);
pd->h(3.14f);
return 0;
}
執行結果:
(1)找出以上程式中使用了過載和覆蓋的函式。
(2)寫出以上程式的輸出結果,並解釋輸出結果。
2、分析並除錯下列程式。
//sy6_2.cpp
#include<iostream>
using namespace std;
class Base
{
public:
void f(int x){cout<<"Base::f(int)"<<x<<endl;}
void f(float x){cout<<"Base::f(float)"<<x<<endl;}
virtual void g(void){cout<<"Base::g(void)"<<endl;}
};
class Derived:public Base
{
public:
virtual void g(void){cout<<"Derived::g(void)"<<endl;}
};
int main()
{
Derived d;
Base *pb=&d;
Derived *pd=&d;
pb->f(42);
pb->f(3.14f);
pb->g();
return 0;
}
(1)找出以上程式中使用了過載和覆蓋函式。(2)寫出程式的輸出結果,並解釋輸出結果。執行結果:
3. 分析並除錯下列程式
//sy6_3.cpp
#include<iostream>
using namespace std;
class Point
{
public:
Point(double i,double j){x=i;y=j;}
double Area(){return 0.0;}
private:
double x,y;
};
class Rectangle:public Point
{
public:
Rectangle(double i,double j,double k,double l):Point(i,j){w=k;h=l;}
double Area(){return w*h;}
private:
double w,h;
};
int main()
{
Point p(3.5,7);
double A=p.Area();
cout<<"Area= "<<A<<endl;
Rectangle r(1.2,3,5,7.8);
A=r.Area();
cout<<"Area= "<<A<<endl;
return 0;
}
執行結果:
4. 分析並除錯下列程式
//sy6_4.cpp#include<iostream>
using namespace std;
const double PI=3.1415;
class Shap
{
public:
virtual double Area()=0;
};
class Triangle:public Shap
{
public:
Triangle(double h,double w){H=h;W=w;}
double Area(){return 0.5*H*W;}
private:
double H,W;
};
class Rectangle:public Shap
{
public:;
Rectangle(double h,double w){H=h;W=w;}
double Area(){return H*W;}
private:
double H,W;
};
class Circle:public Shap
{
public:
Circle(double r){R=r;}
double Area(){return PI*R*R;}
private:
double R;
};
class Square:public Shap
{
public:
Square(double s){S=s;}
double Area(){return S*S;}
private:
double S;
};
double Total(Shap *s[],int n)
{
double sum=0;
for(int i=0;i<n;i++)
sum+=s[i]->Area();
return sum;
}
int main()
{
Shap *s[5];
s[0]=new Square(8.0);
s[1]=new Rectangle(3.0,8.0);
s[2]=new Square(12.0);
s[3]=new Circle(8.0);
s[4]=new Triangle(5.0,4.0);
double sum=Total(s,5);
cout<<"SUM = "<<sum<<endl;
return 0;
}
(1)指出抽象類。
(2)指出純虛擬函式,並說明它的作用。
(3)每個類的作用是什麼?整個程式的作用是什麼?
5. 某學校對教師每個月工資的計算規定如下:固定工資+課時補貼;教授的固定工資為5000元,每個課時補貼50;副教授的固定工資為3000,每個課時補貼30元;講師的固定工資為2000元,每個課時補貼20元。定義教師抽象類,派生不同職稱的教師類,編寫程式求若干個教師的月工資。(sy6_5.cpp)
//sy6_5.cpp#include <iostream>
using namespace std;
class Teacher
{
public:
virtual int Salary()=0;
virtual void Print(int)=0;
};
class Professor:public Teacher
{
private:
char name[128];
int lessons;
public:
Professor()
{
cout<<"請輸入姓名:";
cin>>name;
cout<<"請輸入課時:";
cin>>lessons;
};
int Salary()
{
return (5000+lessons*50);
};
void Print(int money)
{
cout<<"職稱:教授 姓名:"<<name<<" 薪水:"<<money<<endl<<endl;
};
};
class AssociateProfessor:public Teacher
{
private:
char name[128];
int lessons;
public:
AssociateProfessor()
{
cout<<"請輸入姓名:";
cin>>name;
cout<<"請輸入課時:";
cin>>lessons;
};
int Salary()
{
return (3000+lessons*30);
};
void Print(int money)
{
cout<<"職稱:副教授 姓名:"<<name<<" 薪水:"<<money<<endl<<endl;
};
};
class Lecturer:public Teacher
{
private:
char name[128];
int lessons;
public:
Lecturer()
{
cout<<"請輸入姓名:";
cin>>name;
cout<<"請輸入課時:";
cin>>lessons;
};
int Salary()
{
return (2000+lessons*20);
};
void Print(int money)
{
cout<<"職稱:講師 姓名:"<<name<<"薪水:"<<money<<endl<<endl;
};
};
int main()
{
Teacher *t = NULL;
int money=0;
t = new Professor();
money = t->Salary();
t->Print(money);
delete t;
t = new AssociateProfessor();
money = t->Salary();
t->Print(money);
delete t;
t = new Lecturer();
money = t->Salary();
t->Print(money);
delete t;
t = NULL;
return 0;
}
執行結果: