task 2
未修改前:
#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); }
修改后:
#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); }
归纳总结
①同名覆盖原则:当派生类和基类中有相同成员时,若未强行指明,则通过派生类对象使用的是派生类的中的同名成员。
②二元作用域分辨符:若果要通过派生类对象访问基类中被覆盖的同名成员,应使用基类名加二元作用域分辨符来限定。
③类型兼容原则:基类对像可以使用公有派生类的对象来替代。
task 3
battery.hpp
#ifndef BATTERY_HPP #define BATTERY_HPP #include<iostream> using namespace std; class Battery { private: int capacity;//车载动力电池容量 public: Battery(int x=70):capacity(x){} int get_capacity(){return capacity;} ~Battery(){} }; #endif
car.hpp
#ifndef CAR_HPP #define CAR_HPP #include<iostream> #include <iomanip> using namespace std; class Car { public: Car(string a,string b,int c,int d=0):maker(a),model(b),year(c),odometers(d){} void info(); ~Car(){} void update_odometers(int newodo); private: string maker,model;//制造商,型号 int year,odometers;//生产年份,行车里程数 }; void Car::info() { cout<<left<<setw(16)<<"maker:"<<maker<<endl; cout<<left<<setw(16)<<"model:"<<model<<endl; cout<<left<<setw(16)<<"year:"<<year<<endl; cout<<left<<setw(16)<<"odometers:"<<odometers<<endl; } void Car::update_odometers(int newodo) { if(newodo<odometers) cout<<"error!更新数值有误!"<<endl; else odometers=newodo; } #endif
electricCar.hpp
#ifndef ELECTRICAR_HPP #define ELECTRICAR_HPP #include<iostream> #include <iomanip> #include "car.hpp" #include "battery.hpp" using namespace std; class ElectricCar : public Car { private: Battery battery; public: ElectricCar(string a,string b,int c,int x=70):Car(a,b,c),battery(x){} void info(); ~ElectricCar(){} }; void ElectricCar::info() { Car::info() ; cout<<left<<setw(16)<<"capacity:"<<battery.get_capacity()<<"-KWh"<<endl; } #endif
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("Porsche", "911", 2018,150); newcar.update_odometers(3500); cout << "\n--------newcar's info--------\n"; newcar.info(); return 0; }
task 4
pets.hpp
#ifndef PETS_HPP #define PETS_HPP #include<iostream> using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string s):nickname(s){} string get_nickname()const {return nickname;} virtual string talk(){} ~MachinePets(){} }; class PetCats:public MachinePets { public: PetCats(const string s):MachinePets(s){} string talk(){return "miao wu~";} ~PetCats(){} }; class PetDogs:public MachinePets { public: PetDogs(const string s):MachinePets(s){} string talk(){return "wang wang~";} ~PetDogs(){} }; #endif
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); }