当前位置:文档之家› 实验8 多态性与虚函数

实验8 多态性与虚函数


b)
c)Leabharlann 根据是 Bicycle 还是 Motocar 输入 height 或者 seatNum。
(4)main 中的输出: 建立循环(for 0 ~ N-1) 。在循环中: 检测指针数组中 pvehicle[i](第 i 个指针)是否为空? 调用虚函数 print()输出属性。 (因为有动态联编, 所以不用区分是 Bicycle 还是 Motocar) 。 (5)main 中释放空间: 将指针数组 pvehicle[]中非空指针释放空间(用 delete) 。
parkingSpace[i]->record(licensetag); // 车辆登记 CampusAutos::ShowCurAutos(parkingSpace); break; } } if(i==N) cout << "Parking Space No." << i << "is available.\n"; continue; } else if ( choice == 2 ) // 车辆退出 { if( CampusAutos::count <= 0 ) // 车位是否已空 { cout<<"Parking Space is empty!\n"<<endl; continue; } for(;;) // 车位不为空时的退出操作 { char licensetag[80]; CampusAutos::inputLicensetag(licensetag); for(int i=0;i<N;i++) // 检查是否有此车 if( parkingSpace[i] != null && parkingSpace[i]->isSameAs(licensetag) ) break; if ( i==N ) // 没发现此车,输入有误,重新输入 cout << "No such licensetag! Retry.\n"; else // 该车是编号为 i 车位的车 { delete parkingSpace[i]; // 删除该对象 parkingSpace[i] = null; // 该车位指针置空 break; // 退出循环 } CampusAutos::ShowCurAutos(parkingSpace); } } else if ( choice == 0 ) // 程序结束 { for(int i=0;i<N;i++) // 释放申请空间 if( parkingSpace[i] != null ) delete parkingSpace[i]; break; // 退出循环 } else { cout << "Error choice. Retry.\n";
} } }
4. 按要求为 Complex 类重载以下几个运算符函数,并在主函数中进行测试。 1) 赋值=:重载为成员函数(必须) 2) 加号-: 重载为友元函数 3) 前置++:重载为友元函数 4) 后置++:重载为友员函数 5) 判断相等= = : 重载为友元函数 class Complex { double real; double imag; public: Complex(double real=0,double imag=0); Complex(Complex &r); ~Complex(); void SetComplex(double real=0,double imag=0); void Print(); }; Complex::~Complex() { } Complex::Complex(double real/* =0 */,double imag/* =0 */) { this->real=real; this->imag=imag; } Complex::Complex(Complex &r) { real=r.real; imag=r.imag; } void Complex::SetComplex(double real/* =0 */,double imag/* =0 */) { this->real=real; this->imag=imag; }
void Complex::Print() { cout<<real<<"+"<<imag<<"j"<<endl; } 4. 临时对象:某些时候会创建生存期在一条语句范围的无名对象,称之为临时对象。在以 下场合下会创建临时对象: a) 非初始化语句中显式调用类的构造函数时 Student(“zhang”); Student s; s=Student(“ zhang”); Student t[3]; t[0]=Student(“zhang”); Student Test(Student s){return Student(“ zhang”);} 非初始化语句中对象作返回值函数返回时 Student Test(){Student s; return s;} Student t; t=Test(); 新版 VC 编译器中,临时对象如果用于在初始化语句中初始化新对象,则不再创建 临时对象: Student s=Student(“ zhang”); Student s1(Student(“zhang”)); //前面两句等同于 Student s(“ zhang”);只调用 1 次有参构造 Student s2[3]={Student(“ zhang”)};//本句调用 1 次有参构造,2 次无参构造 Student Test(Student s){ return s;} Test(Student(“zhang”)); Student t= Test(Student(“ zhang”));
实验八 多态性与虚函数
实验目的
了解多态性的两种表现形式:函数重载和虚函数 了解赋值兼容原则的含义 掌握虚函数的定义方法及作用
一、实验任务: 1. 虚函数练习 (1)设计一个抽象类 Shape,其中包括 double area 和 double Area(); (2)以 public 方式从 Shape 派生出 Circle 和 Rectangle 两个派生类,添加相关的数据成 员(Circle: radius,PI; Rectangle:width,height) 、有参构造函数、重写 Area 函数; (3)设计主函数:要求定义一个 4 元素 Shape 类指针数组,数组元素初始化为 NULL, 动态创建 2 个 Circle 对象和 2 个 Rectangle 对象,用上面的指针数组操作 4 个对象,设 计循环求算它们的面积总和。 调试程序,分析理解虚函数动态联编的原理。 2. 自行车(Bicycle)和汽车(Motorcar)都是车辆(Vehicle) ,它们有共同的属性最大速度 (maxSpeed)和重量(weight) ,也有各自不同的特性,比如自行车的高度(height)和 汽车的座位数(seatNum) 。现有车辆若干(实验时设 N=3) ,将其输入并放入一个指针 数组,每个车辆需要设置其属性。输入后分类显示各自属性(即自行车和汽车分别显示 各自属性) 。 (1)基本思路: 创建类 Vehicle 及其派生类;设基类为 Vehicle,具有属性 maxSpeed 和 weight,其中有 一个虚函数 print 显示属性, 由派生类负责解释具体的属性。 由 Vehicle 派生两个类 Bicycle 和 Motocar,Bicycle 的属性为 height;而 Motocar 的属性为 seatNum。 (2)main 中创建指针数组:main 中创建一个指针数组 Vehicle* pvehicle[N];并将其中指 针置空。 (3)main 中的输入: 用一个循环次数少于 N 的循环(while 或 for)下面是循环中进行的工作: 询问用户是 Bicycle 还是 Motocar,或者退出循环? 如果是 Bicycle 则创建 Bicycle 的堆对象;否则创建 Motocar 的堆对象 vcl。 (使用 new) 将该对象的地址记录到指针数组 pvehicle[]的空白处。 (也可先找到这个空白处,再 创建堆对象) 询问用户新输入的车辆的 maxSpeed 和 weight,将这两个参数记录到这个对象 vcl 中。
3. 某学校实现进出校园的车辆管理, 试用类的形式记录车辆的进出 (类名为 CampusAutos) 。 有车进来时用 new 的方法构建一个新的对象,记录它的牌照( 8 个字符的字符串, licensetag) ,离开校园时即析构。在 main()中设一循环,每次可以输入车辆进入及牌照 号或某牌照号的车辆出去(校园内限 N 辆,暂设为 5) ,同时报告校园内现有多少辆汽 车及它们的牌照号。 (用对象指针数组) 注意: 程序中要实现数组溢出保护 (车位已满) 、 牌照输入错误(超 8 个字符)和查询失败报告(无此牌照) 。停车时要注意重复车牌问 题。 void main() { CampusAutos *parkingSpace[N]; for(int i=0;i<N;i++) // 停车场初始化为空 parkingSpace[i] = null; for(;;) { cout << "1 --- Auto Enter\n2 -- Auto Exit\n0 -- End\nYour Choice:"; int choice; cin >> choice; if ( choice == 1 ) // 车辆进入 { if( CampusAutos::count >= N ) // 车位是否已满 { cout<<"Parking space is full!\n"<<endl; continue; } for (int i=0; i < N; i++ ) { if( parkingSpace[i] == null ) // 寻找空位 { parkingSpace[i] = new CampusAutos(); // 创建新的停车位 char licensetag[80]; CampusAutos::inputLicensetag(licensetag);
相关主题