实验4 继承
四、实验结论
2. 实验任务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); }
运行结果截图:
归纳总结:
1.同名覆盖原则:
(1)派生类与基类中有相同成员时,若未强行指名,则通过派生类对象使用的是派生类的同名成员。
(2)如果要通过派生类的对象访问基类被覆盖的同名成员,需要加 对象名.基类名::同名成员 来限定。
2.二元作用域分辨符:
(1)如果派生类中声明了与基类成员函数同名的新函数,及时函数的参数表不同,从基类继承的同名函数的所有重载形式也都会被隐藏。
(2)如果某派生类的多个基类拥有同名的成员,同时,派生类又新增这样的同名成员,在这种情况下,派生类的成员将隐藏所有基类的同名成员。
3.类型兼容原则:
在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。
代码微调后,运行结果截图:
3. 实验任务3
battery.hpp:
#ifndef BATTERY_HPP #define BATTERY_HPP #include<iostream> class Battery{ public: Battery() {capacity=70;} int get_capacity() {return capacity;} private: int capacity; }; #endif
car.hpp:
#ifndef CAR_HPP #define CAR_HPP #include<iostream> using namespace std; class Car{ public: Car(string a,string b,int c): maker(a),model(b),year(c) {odometers=0;} void info(); void update_odometers(int t); private: string maker,model; int year,odometers; }; void Car::info(){ cout << "制造商 :" << maker << endl; cout << "型号 :" << model << endl; cout << "生产年份:" << year << endl; cout << "里程数 :" << odometers << endl; } void Car::update_odometers(int t){ if(t<odometers){ cout << "更新数值有误" << endl; return; } else{ odometers=t; } } #endif
electricCar.hpp:
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include"car.hpp" #include"battery.hpp" #include<iostream> using namespace std; class ElectricCar:public Car{ public: ElectricCar(string a,string b,int c): Car(a,b,c) {} void info(); private: Battery battery; }; void ElectricCar::info(){ Car::info(); cout << "µç³ØÈÝÁ¿:" << battery.get_capacity() << "-kWh" << endl; } #endif
task3.cpp:
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("ABC", "xx", 2019); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(15000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("XYZ", "s", 2018); newcar.update_odometers(5000); cout << "\n--------newcar's info--------\n"; newcar.info(); }
运行测试截图:
4. 实验任务4
pets.hpp:
#ifndef PETS_HPP #define PETS_HPP #include<iostream> using namespace std; 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 "喵~";} }; class PetDogs: public MachinePets{ public: PetDogs(const string s): MachinePets(s) {} string talk(){return "汪!";} }; #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); }
运行测试截图: