实验四 继承
实验任务二 有关虚基类的验证性实验
简单说明:基于虚基类的一些特性进行的验证性实验,理解并掌握虚基类的使用特点与注意事项。
源代码如下(关于虚基类已修改):
#include <iostream>
#include <typeinfo>
// definitation of Graph
class Graph
{
public:
virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; }
};
// definition of Rectangle, derived from Graph
class Rectangle : public Graph
{
public:
void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; }
};
// definition of Circle, derived from Graph
class Circle : public Graph
{
public:
void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; }
};
// definitaion of fun(): as a call interface
void fun(Graph *ptr)
{
std::cout << "pointer type: " << typeid(ptr).name() << "\n";
std::cout << "RTTI type: " << typeid(*ptr).name() << "\n";
ptr -> draw();
}
// test
int main()
{
Graph g1;
Rectangle r1;
Circle c1;
// call by object name
g1.draw();
r1.draw();
c1.draw();
std::cout << "\n";
// call by object name, and using the scope resolution operator::
r1.Graph::draw();
c1.Graph::draw();
std::cout << "\n";
// call by pointer to Base class
fun(&g1);
fun(&r1);
fun(&c1);
}
测试结果(未修改前):

测试结果(修改后):

归纳与总结:
如果一个类有直接或间接的虚基类,则先执行虚基类的构造函数。
如果该类有其他基类,则按照它们在继承声明列表中的出现的次序,分别执行它们的构造函数,但构造过程中,不再执行它们的虚基类的构造函数。
实验任务三 简单的车辆信息管理
简单说明:使用类的组合和继承模拟简单的车辆信息管理。
为了对车量基本信息进行管理,对现实世界车量基本信息抽象后,抽象出Car类、ElectricCar类、Battery类,它们之间的关系描述如下:
ElectricCar类公有继承自Car类,ElectricCar中新增数据成员为Battery类对象。
类定义与实现battery.hpp代码如下:
#ifndef BATTERY_CPP
#define BATTERY_CPP
#include <iostream>
using namespace std;
class Battery {
public:
int capacity;
Battery(int c = 70);
void get_capacity();
};
Battery::Battery(int c) : capacity(c) {}
void Battery::get_capacity() {
cout << "capacity:\t" << capacity << "-kWh" << endl;
}
#endif
类定义与实现car.hpp代码如下:
#ifndef CAR_HPP
#define CAR_HPP
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string maker;
string model;
int year;
int odometers;
Car(string ma, string mo, int year, int od);
void info();
void update_odometers(int newo);
};
Car::Car(string ma, string mo, int ye, int od = 0): maker(ma), model(mo), year(ye), odometers(od) { }
void Car::info() {
cout << "maker:\t\t" << maker << endl;
cout << "model:\t\t" << model << endl;
cout << "year:\t\t" << year << endl;
cout << "odometers:\t" << odometers << endl;
}
void Car::update_odometers(int newo) {
if (newo < odometers)
cout << "抱歉,新的里程不能小于当前里程!" << endl;
else
odometers = newo;
}
#endif // !CAR_HPP
类定义与实现electricCar.hpp代码如下:
#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP
#include <iostream>
#include <string>
#include "car.hpp"
#include "battery.hpp"
using namespace std;
class ElectricCar : public Car {
public:
Battery battery;
ElectricCar(string ma, string mo, int year, int od, int ba);
void info();
};
ElectricCar::ElectricCar(string ma, string mo, int year, int od = 0, int ba = 70): Car(ma, mo, year, od), battery(ba) { }
void ElectricCar::info() {
cout << "maker:\t\t" << maker << endl;
cout << "model:\t\t" << model << endl;
cout << "year:\t\t" << year << endl;
cout << "odometers:\t" << odometers << endl;
battery.get_capacity();
}
#endif // !ELECTRICCAR_HPP
主程序task3.cpp代码如下:
#include <iostream>
#include "electricCar.hpp"
int main()
{
using namespace std;
// test class of Car
Car oldcar("Ferrari", "laferrari", 2014);
cout << "--------oldcar's info--------" << endl;
oldcar.update_odometers(5000);
oldcar.info();
cout << endl;
// test class of ElectricCar
ElectricCar newcar("Tesla", "model y", 2020);
newcar.update_odometers(30000);
cout << "\n--------newcar's info--------\n";
newcar.info();
}
使用更改的数据的测试结果:

实验任务四 简单的机器宠物
简单说明:使用类的继承,模拟简单的机器宠物。
对机器宠物进行抽象后,抽象出三个简单的类:
机器宠物类MachinePets、宠物猫类PetCats、宠物狗类PetDogs。
类定义与实现pets.hpp代码如下:
#ifndef PETS_HPP
#define PETS_HPP
#include <iostream>
#include <string>
using namespace std;
class MachinePets {
public:
string nickname;
MachinePets(const string s);
string get_nickname() { return nickname; }
virtual string talk();
};
MachinePets::MachinePets(const string s): nickname(s) { }
string MachinePets::talk() {
return "...";
}
class PetCats : public MachinePets {
public:
PetCats(const string s);
string talk();
};
PetCats::PetCats(const string s) : MachinePets(s) { }
string PetCats::talk() {
return "miao wu~";
}
class PetDogs : public MachinePets {
public:
PetDogs(const string s);
string talk();
};
PetDogs::PetDogs(const string s) : MachinePets(s) { }
string PetDogs::talk() {
return "wang wang~";
}
#endif // !PETS_HPP
主程序task3.cpp代码如下:
#include <iostream>
#include "pets.hpp"
void play(MachinePets* ptr)
{
std::cout << ptr->get_nickname() << " says " << ptr->talk() <<
std::endl;
}
int main()
{
PetCats cat("miku");
PetDogs dog("da huang");
play(&cat);
play(&dog);
}
测试结果如下:


浙公网安备 33010602011771号