实验任务二:
(原代码)
#include <iostream> #include <typeinfo> // definitation of Graph class Graph { public: 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); }
运行结果:
总结:
1、同名覆盖原则:派生类中新增的成员具有更小的类作用域,因此,在派生类及建立派生类对象的模块中,派生类新增成员隐藏了基类的同名成员。
2、二元作用域分辨符:可以用来限定要访问的成员所在的类的名称。
3、类型兼容规则:在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。
(改进后代码)
#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); }
运行结果:
与原来的不同在于:
运行时指针变量ptr实际指向的对象的类型信息发生了改变。
实验任务3:
(task3.cpp代码)
#include <iostream> #include <string> #include "battery.hpp" #include "car.hpp" #include "electricCar.hpp" using namespace std; int main() { using namespace std; // test class of Car Car oldcar("Audi", "a4", 2020); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("Tesla", "model s", 2020); newcar.update_odometers(5000); cout << "\n--------newcar's info--------\n"; newcar.info(); }
(battery.hpp代码)
#pragma once #include <iostream> #include <string> using namespace std; class Battery { public: Battery(int ca = 70) : capacity(ca) {} void get_capacity() { cout << "capacity:\t" << capacity << "-kwh" << endl; } ~Battery() = default; private: int capacity; };
(car.hpp代码)
#pragma once #include <iostream> #include <string> using namespace std; class Car { public: Car() = default; Car(string ma, string mo, int ye, int od = 0) : maker(ma), model(mo), year(ye), odometers(od) {} void info() { cout << "maker:\t" << maker << endl; cout << "model:\t" << model << endl; cout << "year:\t" << year << endl; cout << "odometers:\t" << odometers << endl; } void update_odometers(int newod) { if (newod < 0) cout << "更新数值有误\n"; else odometers += newod; } ~Car() = default; public: string maker; string model; int year; int odometers; };
(electricCar.hpp代码)
#include <iostream> #include <string> #include "battery.hpp" #include "car.hpp" using namespace std; class ElectricCar : public Car { public: ElectricCar(string ma, string mo, int ye, int od = 0, int ca = 70); void info(); ~ElectricCar() = default; private: Battery battery; };
ElectricCar::ElectricCar(string ma, string mo, int ye, int od = 0, int ca = 70) : battery(ca) { maker = ma; model = mo; year = ye; odometers = od; } void ElectricCar::info() { cout << "maker:\t" << maker << endl; cout << "model:\t" << model << endl; cout << "year:\t" << year << endl; cout << "odometers:\t" << odometers << endl; battery.get_capacity(); }
运行结果:
实验任务4:
(task4.cpp)
#include <iostream> #include <string> #include "pets.hpp" using namespace std; 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); }
(pets.hpp)
#include <iostream> #include <string> using namespace std; class MachinePets { public: string nickname; public: MachinePets() = default; MachinePets(const string s); virtual string talk() = 0; const string get_nickname() { return nickname; } }; MachinePets::MachinePets(const string s) : nickname(s) {} class PetCats : public MachinePets { public: PetCats(const string s) { nickname = s; } string talk() { return "miao wu~"; } }; class PetDogs : public MachinePets { public: PetDogs(const string s) { nickname = s; } string talk() { return "wang wang~"; } };
运行结果:
总结:
1、通过这次实验我对类的继承的了解更加深刻了;
2、不过,对于继承和组合还是存在一丝理解的模糊;