CSDN的各位友友们你们好,今天千泽为大家带来的是
燕山大学-面向对象程序设计实验-实验八 多态性—类型转换与 虚函数 -实验报告,
接下来让我们一起进入c++的神奇小世界吧,相信看完你也能写出自己的 实验报告!
本系列文章收录在专栏 燕山大学面向对象设计报告中 ,您可以在专栏中找到其他章节
如果对您有帮助的话希望能够得到您的支持和关注,我会持续更新的!
实验八 多态性—类型转换与虚函数
8.1 实验目的
1.理解掌握运算符[]、()的重载;
2.理解类型转换,掌握类型转换函数的设计和使用;
3.理解和掌握虚函数的作用;
4.掌握利用虚函数实现C++的运行时多态性;
5.理解纯虚类和抽象类。
8.2 实验内容
8.2.1程序阅读
1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include <iostream>
#include "stdlib.h"
class CComplex
{
public:
CComplex(double r = 0, double i = 0)
{
real = r;
imag = i;
}
int operator int()
{
return (int)real;
}
void Display(void)
{
cout << "(" << real << "," << imag << ")" << endl;
}
protected:
double real;
double imag;
};
class CVector
{
public:
CVector(CComplex &obj1, CComplex &obj2, CComplex &obj3, CComplex &obj4)
{
objArray[0] = obj1;
objArray[1] = obj2;
objArray[2] = obj3;
objArray[3] = obj4;
}
friend CComplex &operator[](CVector obj, int n);
private:
CComplex objArray[4];
};
CComplex &operator[](CVector obj, int n)
{
if(n<0 || n>3)
{
cout<<"Out of range!"<<endl;
exit(0);
}
return obj.objArray[n];
}
void main()
{
CComplex c1(1.1, 1.1);
CComplex c2(2.2, 2.2);
CComplex c3(3.3, 3.3);
CComplex c4(4.4, 4.4);
CVector v(c1,c2,c3,c4);
v[0].Display();
v[1].Display();
v[2].Display();
v[3].Display();
v[0] = 5.5; ----------------------------------------------------------①
v[1] = CComplex(6.6); -------------------------------------------②
v[2] = int(CComplex(7.7)); --------------------------------------③
v[3] = int(CComplex(8.8,9.9)); ----------------------------------④
v[0].Display();
v[1].Display();
v[2].Display();
v[3].Display();
}
问题一:上述程序存在两大错误,在不修改主函数和程序原意的前提下,改正该程序中存在的错误。
答:重载类型转换函数时,无需返回值;下标运算符是双目运算符,只能重载为成员函数。
修改后的类部分如下:
class CComplex{
public:
CComplex(double r = 0, double i = 0){
real = r;
imag = i;
}
operator int(){
return (int)real;
}
void Display(void){
cout << "(" << real << "," << imag << ")" << endl;
}
protected:
double real;
double imag;
};
class CVector{
public:
CVector(CComplex &obj1, CComplex &obj2, CComplex &obj3, CComplex &obj4){
objArray[0] = obj1;
objArray[1] = obj2;
objArray[2] = obj3;
objArray[3] = obj4;
}
CComplex &operator[](int n);
private:
CComplex objArray[4];
};
CComplex &CVector::operator[](int n){
if(n<0 || n>3){
cout<<"Out of range!"<<endl;
exit(0);
}
return objArray[n];
}
问题二:①处的转换属于显式转换还是隐式转换,并解释该转换过程。
答:隐式转换。①处没有类型转换函数,直接赋值,为隐式转换。
问题三:②处的转换属于显式转换还是隐式转换,并解释该转换过程。
答:显式转换。②处使用用了类的构造函数,将数值转换为类,为显式转换。
问题四:解释③处的转换过程。
问题四:解释③处的转换过程。
答:先把double型数值7.7通过CComplex构造类对象,real 为7.7,在对这个类对象进行类型转换为int型,返回(int)real的值7,再转换为CComplex对象,real为7,赋值给v[2]。整个过程中各对象的imag均为0.
问题五:解释④处的转换过程。
答:先把double型数值8.8通过CComplex构造类对象,real 为8.8,imag为9.9,在对这个类对象进行类型转换为int型,返回(int)real的值8,此时imag的值被省略,再转换为CComplex对象,real为8,imag为0,赋值给v[3]。
8.2.2 程序设计
1.编写一个程序计算三角形、正方形和圆形的面积,要求抽象出一个基类base,在其中说明一个虚函数,用来求面积,并利用单接口,多实现版本设计各图形面积的方法。
答:
#include<iostream>
using namespace std;
class Base{
virtual void GetArea()=0;
};
class Triangle:public Base{
private:
double a;
double h;
public:
Triangle(double a,double h):a(a),h(h){
}
void GetArea(){
cout<<"Triangle Area: "<<0.5*a*h<<endl;
}
};
class Square:public Base{
private:
double a;
public:
Square(double a):a(a){
}
void GetArea(){
cout<<"Square Area: "<<a*a<<endl;
}
};
class Circle:public Base{
private:
double r;
public:
Circle(double r):r(r){
}
void GetArea(){
cout<<"Circle Area: "<<3.14159*r*r<<endl;
}
};
int main(){
Triangle t(2,4);
t.GetArea();
Square s(5);
s.GetArea();
Circle c(5);
c.GetArea();
return 0;
}
8.3思考题
1.设计一个汽车类Motor,该类具有可载人数、轮胎数、马力数、生产厂家和车主五个数据成员,根据Motor类派生出Car类、Bus类和Truck类。其中Bus类除继承基类的数据成员外,还具有表示车厢节数的数据成员Number;Truck类除继承基类的数据成员之外,还具有表示载重量的数据成员Weight。每个类都具有成员函数Display,用于输出各类对象的相关信息。要求设计一个统一的显示相关信息的全局函数ShowInfo在主函数中调用,主函数中不直接调用类里面的Display函数。
答:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
class Motor{
protected:
int renshu;
int luntai;
int mali;
string changjia;
string chezhu;
public:
Motor(int a,int b,int c,string d,string e):renshu(a),luntai(b),mali(c),changjia(d),chezhu(e){
}
virtual void Display(){
cout<<"可载人数为:"<<renshu<<endl;
cout<<"轮胎数为:"<<luntai<<endl;
cout<<"马力数为:"<<mali<<endl;
cout<<"生产厂家:"<<changjia<<endl;
cout<<"车主:"<<chezhu<<endl;
}
};
class Car:public Motor{
public:
Car(int a,int b,int c,string d,string e):Motor(a,b,c,d,e){
}
void Display(){
cout<<"车型:Car"<<endl;
Motor::Display();
}
};
class Bus:public Motor{
private:
int Number;
public:
Bus(int a,int b,int c,string d,string e,int f):Motor(a,b,c,d,e),Number(f){
}
void Display(){
cout<<"车型:Bus"<<endl;
Motor::Display();
cout<<"车厢节数为:"<<Number<<endl;
}
};
class Truck:public Motor{
private:
double Weight;
public:
Truck(int a,int b,int c,string d,string e,double f):Motor(a,b,c,d,e),Weight(f){
}
void Display(){
cout<<"车型:Truck"<<endl;
Motor::Display();
cout<<"载重量为:"<<Weight<<endl;
}
};
void ShowInfo(Motor *a){
a->Display();
}
int main(){
Car c(5,4,1000,"BMW","DY");
Bus b(30,6,5000,"VW","DYY",5);
Truck t(4,12,10000,"BYD","DYYY",8.1);
Motor* p = &c;
ShowInfo(p);
p = &b;
ShowInfo(p);
p = &t;
ShowInfo(p);
}
本篇分享就到这里,希望对你有帮助!