实验四
task2
程序源码
#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); }
运行结果截图
归纳总结
同名覆盖原则:若派生类和基类中有同名的成员,派生类将覆盖基类的函数
二元作用域分辨符:通过使用继承类::函数来访问基类中被派生类覆盖的同名函数
类型兼容原则:基类对象可以使用共有派生类的对象代替
微调后运行结果截图:
task 3
battery.hpp
#include <iostream> using namespace std; class Battery{ private: int capacity; public: Battery(int capacity0=70):capacity(capacity0){} int get_capacity(){return capacity;} };
car.hpp
#include <iostream> #include <string> #include <iomanip> using namespace std; class Car{ public: Car(const string maker0,const string model0,int year0,int odometers0=0):maker(maker0),model(model0),year(year0),odometers(odometers0){} void info(); void update_odometers(int new_odometers); private: string maker; string model; int year; int odometers; }; void Car::info() { cout<<"maker: "<<maker<<endl; cout<<"model: "<<model<<endl; cout<<"year: "<<year<<endl; cout<<"odometers: "<<odometers<<endl; } void Car::update_odometers(int new_odometers) { if(new_odometers<odometers) cout<<"INCORRECT UPDATA VALUE"<<endl; else odometers=new_odometers; }
electricCar.hpp
#include <iostream> #include <string> #include <iomanip> #include "car.hpp" #include "battery.hpp" using namespace std; class ElectricCar:public Car{ private: Battery battery; public: ElectricCar(const string maker0,const string model0,int year0,int odometers0=0,int capacity0=70):Car(maker0,model0,year0),battery(capacity0){} void info(); }; void ElectricCar::info() { Car::info(); battery.get_capacity(); }
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(); }
task4
pets.hpp
#include <iostream> #include <string> #include <iomanip> using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string s):nickname(s){} string get_nickname() const{ return nickname; } virtual string talk(){} }; class PetCats:public MachinePets { public: PetCats(const string s):MachinePets(s){} virtual string talk() { return "miao wu~"; } }; class PetDogs:public MachinePets { public: PetDogs(const string s):MachinePets(s){} virtual 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); }