C++程式設計實驗四 繼承
阿新 • • 發佈:2021-12-01
老師的任務
程式原始碼:
#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); }
執行測試截圖: