实验四 继承
任务二:
#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); }
改动后:
同名覆盖原则:派生类对象调用派生类和基类均有的函数时,派生类的会覆盖掉基类的
二元作用域分辨符:限定作用的类
类型兼容原则:派生类类型对象可以作为基类对象使用,但调用函数仅会调用基类的函数
不同:改动后运行时调用对象类型类的函数
任务三:
//battery.hpp #ifndef BATTER_HPP
#define BATTER_HPP #include<iostream> #include<string> class Battery { public: Battery(int c = 70) :capacity(c) {}; int get_capacity() { return capacity; }; protected: int capacity; }; #endif
//car.hpp #ifndef CAR_HPP
#define CAR_HPP #include<iostream> #include<string> #include<iomanip> class Car { public: Car(std::string Maker, std::string Model, int Year, int Odometers = 0) :maker(Maker), model(Model), year(Year), odometers(Odometers) {}; void info(); int update_odometers(int update); private: std::string maker, model; int year, odometers; }; void Car::info() { using namespace std; cout<<left<<setw(12)<< "maker:" << maker << endl <<setw(12) << "model:" << model << endl <<setw(12) << "year:" << year << endl <<setw(12) << "odometers:" << odometers << endl; }; int Car::update_odometers(int update) { if (odometers + update >= odometers) { odometers += update; return odometers; } else std::cout << "更新数值有误" << std::endl; } #endif
//electricCar.hpp #ifndef ELECTRICAR_HPP
#define ELECTRICAR_HPP #include"battery.hpp" #include"car.hpp" #include<iostream> #include<string> class ElectricCar :public Car { public: ElectricCar(std::string Maker, std::string Model, int Year, Battery batter=70, int Odometers = 0) :Car(Maker, Model, Year, Odometers), battery(batter){}; void info(); private: Battery battery; }; void ElectricCar::info() { Car::info(); std::cout << std::setw(12) <<"capacity:"<< battery.get_capacity() << "-kWh"; } #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("Tesla","model s", 2016); newcar.update_odometers(2500); cout << "\n--------newcar's info--------\n"; newcar.info(); }
任务四:
//pets.hpp #ifndef PETS_HPP #define PETS_HPP #include<iostream> #include<string> using std::string; class MachinePets { public: MachinePets(const string s) :nickname(s) {}; virtual string talk() { return ""; }; string get_nickname() { return nickname; } private: string nickname; }; class PetCats :public MachinePets { public: PetCats(const string s) :MachinePets(s) {}; string talk() { return "miao miao"; } }; class PetDogs :public MachinePets { public: PetDogs(const string s) :MachinePets(s) {}; string talk() { return "wang"; }; }; #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); }