任务2 源代码
task2.cpp
#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); }
归纳总结
①同名覆盖原则:当派生类和基类中有相同成员时,若未强行指明,则通过派生类对象使用的是派生类的中的同名成员
②二元作用域分辨符:若果要通过派生类对象访问基类中被覆盖的同名成员,应使用基类名加二元作用域分辨符来限定
③类型兼容原则:基类对像可以使用公有派生类的对象来替代
任务3 源代码
battery.hpp
#pragma once #include "electricCar.hpp" using namespace std; class Battery { public: Battery(int capacity0 = 70); ~Battery() = default; int get_capacity() const; private: int capacity; }; Battery::Battery(int capacity0) :capacity{ capacity0 } { ; } int Battery::get_capacity() const { return this->capacity; }
car.hpp
#pragma once #include <iostream> #include <iomanip> using namespace std; class Car { public: Car(string maker0, string model0, int year0, int odometers0 = 0); ~Car() = default; void info() const; void update_odometers(int new_odometers); private: string maker; string model; int year; int odometers; }; Car::Car(string maker0, string model0, int year0, int odometers0) :maker{ maker0 }, model{ model0 }, year{ year0 }, odometers{ odometers0 } { ; } void Car::info() const { cout << setw(20) << setiosflags(ios::left) << "maker:"; cout << this->maker << endl; cout << setw(20) << setiosflags(ios::left) << "model:"; cout << this->model << endl; cout << setw(20) << setiosflags(ios::left) << "year:"; cout << this->year << endl; cout << setw(20) << setiosflags(ios::left) << "odometers:"; cout << this->odometers << endl; } void Car::update_odometers(int new_odometers) { if (this->odometers >= new_odometers) { cout << "Error updating value"; return; } this->odometers = new_odometers; }
electricCar.hpp
#pragma once #include "car.hpp" #include "battery.hpp" using namespace std; class ElectricCar:public Car { public: ElectricCar(string maker0, string model0, int year0, int odometers0 = 0, int capacity0 = 70); ~ElectricCar() = default; void info() const; private: Battery battery; }; ElectricCar::ElectricCar(string maker0, string model0, int year0, int odometers0, int capacity0) :Car(maker0, model0, year0, odometers0), battery(capacity0) { ; } void ElectricCar::info() const { Car::info(); cout << setw(20) << setiosflags(ios::left) << "capacity:"; cout << battery.get_capacity() << "-kwh" << endl; }
task3.cpp
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("Audi", "a4", 2016); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25000); 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(); cout << endl; ElectricCar newnewcar("Mercedes-Benz", "SLS", 2020, 3000, 100); cout << "\n--------new newcar's info--------\n"; newnewcar.info(); }
任务4 源代码
pets.hpp
#pragma once #include <iostream> #include <string> using namespace std; class MachinePets { public: MachinePets(const string s) : nickname{s} { ; } ~MachinePets() = default; string get_nickname() const { return this->nickname; } virtual string talk() = 0; private: string nickname; }; class PetCats : public MachinePets { public: PetCats(const string s) : MachinePets(s) { ; } ~PetCats() = default; virtual string talk() { string s = "miao wu~"; return s; } }; class PetDogs : public MachinePets { public: PetDogs(const string s) : MachinePets(s) { ; } ~PetDogs() = default; virtual string talk() { string s = "wang wang~"; return s; } };
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); }