实验四 继承
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); }

归纳总结:
1.当派生类与基类有同名成员时,直接调用会调用派生类中的成员(同名覆盖原则),也可以使用二元作用于域分辨符来对基类的的成员进行调用。
2.兼容性原则,派生类成员可以当作基类来使用但是只能使用继承于基类的那一部分。

Battery.hpp
#include<iostream> using namespace std; class Battery{ public: Battery(int x=70):capacity(x){}; int get_cpacity() { return capacity; } private: int capacity; };
Car.hpp
#include<iostream> #include<string> #include<iomanip> using namespace std; class Car{ public: string maker; string model; int year; int odmeters; Car(string a,string b,int c):maker(a),model(b),year(c),odmeters(0){}; void info() { cout<<left<<setw(20)<<"maker:"<<maker<<endl; cout<<left<<setw(20)<<"model:"<<model<<endl; cout<<left<<setw(20)<<"year:"<<year<<endl; cout<<left<<setw(20)<<"odometers:"<<odmeters<<endl; } void update_odometers(int x) { if(x<odmeters) cout<<"wrong odometers"<<endl; else odmeters=x; } };
ElectericCar.hpp
#ifndef ElectricCar_h #define ElectricCar_h #include<iostream> #include"car.hpp" #include"Battery.hpp" using namespace std; class ElectricCar:public Car { public: Battery battery; ElectricCar(string a,string b,int c,int d=70):Car(a,b,c),battery(d){}; void info() { cout<<left<<setw(20)<<"maker:"<<maker<<endl; cout<<left<<setw(20)<<"model:"<<model<<endl; cout<<left<<setw(20)<<"year:"<<year<<endl; cout<<left<<setw(20)<<"odometers:"<<odmeters<<endl; cout<<left<<setw(20)<<"capacity:"<<battery.get_cpacity()<<"-KWh"<<endl; } }; #endif /* ElectricCar_h */
task3.cpp
#include <iostream> #include "ElectricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("Benz", "s650", 2016); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("Tesla", "model Y", 2016); newcar.update_odometers(2500); cout << "\n--------newcar's info--------\n"; newcar.info(); }

Pets.hpp
#ifndef pet_h #define pet_h #include<iostream> #include<string> using namespace std; class MachinePets{ public: string nickname; MachinePets(const string s):nickname(s){}; virtual string talk() { return " "; } string get_nickname() const { return nickname; } }; class PetCats:public MachinePets { public: PetCats(const string s):MachinePets(s){}; string talk() { return "miao wu~"; } }; class PetDogs:public MachinePets { public: PetDogs(const string s):MachinePets(s){}; string talk() { return "wang wang~"; } }; #endif /* pet_h */
task4.hpp
#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); }


浙公网安备 33010602011771号