C++程序设计实验四 继承
程序源码:
#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. 类型兼容原则:派生类对象可以被当作基类对象使用,但是,当作基类对象使用时,只能使用作为基类的那一部分接口
改动后的代码:
// definitation of Graph class Graph { public: // 声明时加了关键字virtual virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; } };
运行结果截图:
battery.hpp
#include<iostream> using namespace std; class Battery { public: Battery(int cap=70): capacity(cap) { } int get_capacity() { return capacity; } private: int capacity; };
car.hpp
#include<iostream> #include<string> #include<iomanip> using namespace std; class Car { public: Car(string _maker, string _model, int _year) : maker(_maker), model(_model), year(_year) { odometers = 0; } void info() { cout << left << setw(20) << "maker:" << maker << endl; cout << setw(20) << "model:" << model << endl; cout << setw(20) << "year:" << year << endl; cout << setw(20) << "odomerters:" << odometers << endl; } void update_odometers(int new_odometers) { if (new_odometers < odometers) { cout << "\nERROR!! updata odometers small than origin odometers" << endl; return; } odometers = new_odometers; } protected: string maker; string model; int year; int odometers; };
electricCar.hpp
#include<iostream> #include<string> #include<iomanip> #include "car.hpp" #include "battery.hpp" using namespace std; class ElectricCar : public Car { public: ElectricCar(string _maker, string _model, int _year, Battery _battery=70) : Car(_maker, _model, _year), battery(_battery) { } void info() { cout << left << setw(20) << "maker:" << maker << endl; cout << setw(20) << "model:" << model << endl; cout << setw(20) << "year:" << year << endl; cout << setw(20) << "odomerters:" << odometers << endl; cout << setw(20) << "capacity:" << battery.get_capacity() << "-kwh" << endl; } private: Battery battery; };
task3.cpp
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("DingDing", "maka", 2008); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(2500); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("kapani", "soucat", 2015); newcar.update_odometers(5000); cout << "\n--------newcar's info--------\n"; newcar.info(); newcar.update_odometers(2500); }
运行测试截图:
pets.hpp
#include<string> using namespace std; class MachinePets { public: MachinePets(const string s) : nickname(s) { } string get_nickname() const { return nickname; } virtual string talk() { return "bing bo~"; } 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); }
运行测试截图: