实验四 继承
task2.cpp
#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); }
改动前的结果:

改动后的结果:

同名覆盖原则:派生类与基类中有相同成员时,若未强行指明,则通过派生类对象使用的是派生类的同名成员;如果要通过派生类的对象访问基类被覆盖的同名成员,则需要加对象名.基类名::同名成员来限定;
二元作用域分辨符:在多重继承的情况下,如果两个基类中存在相同的变量名,可以使用作用域运算符来进行区分;
类型兼容原则:指在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。通过公有继承,派生类得到了基类中除构造函数、析构函数之外的所有成员。
task3:
battery.hpp
#include <iostream> using namespace std; class battery { public: battery(int ca = 70) : capacity(ca){}; int get_capacity() { return capacity; } private: int capacity; };
car.hpp
#include <iostream> #include <string> #include <iomanip> using namespace std; class car { public: car(string n_maker, string n_model, int n_year, int n_odometers = 0) : maker(n_maker), model(n_model), year(n_year), odometers(n_odometers){}; void info() { // cout << setiosflags(ios::left); cout << "maker:\t\t" << maker << endl; cout << "model:\t\t" << model << endl; cout << "year:\t\t" << year << endl; cout << "odometers:\t" << odometers << endl; } void update_odometers(int n) { if (n < odometers) cout << "更新数值有误!" << endl; else odometers = n; } private: string maker; string model; int year; int odometers; };
electricCar.hpp
#include <iostream> #include "car.hpp" #include "battery.hpp" using namespace std; class electricCar : public car { public: electricCar(string n_maker, string n_model, int n_year, int n_odometers = 0, battery b = 70) : car(n_maker, n_model, n_year, n_odometers), Battery(b){}; void info() { car::info(); cout << "capacity:\t" << Battery.get_capacity() << endl; } private: battery Battery; };
task3.cpp
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car car oldcar("car1", "a7", 1999); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(18000); oldcar.info(); cout << endl; // test class of ElectricCar electricCar newcar("Tesla", "model s", 2016); newcar.update_odometers(2500); cout << "\n--------newcar's info--------\n"; newcar.info(); }
运行结果:

task4
pets.hpp
#include <iostream> #include <string> using namespace std; class MachinePets { public: MachinePets(const string s) : nickname(s){}; virtual string talk() = 0; string get_nickname() const { return nickname; } private: string nickname; }; class PetCats : public MachinePets { public: PetCats(const string s) : MachinePets(s){}; string talk() { return "miao wu~"; } }; class PetDogs : public MachinePets { public: PetDogs(const string s) : MachinePets(s){}; string talk() { return "wang wang~"; } };
task4.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号