实验4 继承
#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); }
1.同名覆盖原则:
当派生类与基类中有相同成员时: 若未强行指名,则通过派生类对象使用的是派生类中的同名成员。 如要通过派生类对象访问基类中被覆盖的同名成员,应使用基类名限定。
2.二元作用域分辨符:
可以通过类型名::的方式区分将要调用的函数究竟属于哪一类。
3.类型兼容原则:
在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。
派生类的对象可以隐含转换为基类的对象。
派生类的对象可以初始化基类的引用。
派生类的指针可以隐含转换为基类的指针。
在替代后,派生类对象可以作为基类的对象使用,但只能使用从基类继续的成员。
# ifndef BATTERY_H # define BATTERY_H # include <iostream> using namespace std; class Battery { private: int capacity; public: Battery(int a=70):capacity(70) {} int get_capacity(){ return capacity; } }; #endif
# ifndef CAR_H # define CAR_H # include <iostream> using namespace std; class Car { private: string maker; string model; int year; int odometers; public: Car(string a="XXXXX",string b="XXXXX",int c=2000,int d=0):maker(a),model(b),year(c),odometers(d) {} void info() { cout << "maker: " << maker << endl; cout << "model: " << model << endl; cout << "year: " << year << endl; cout << "odometers: " << odometers << endl; } void update_odometers(int a) { if (a >= odometers) odometers = a; else cout << "Error updating value" << endl; } }; #endif
# ifndef ELECTRICCAR_H # define ELECTRICCAR_H # include <iostream> # include "car.hpp" # include "battery.hpp" using namespace std; class ElectricCar :public Car { private: Battery battery; public: ElectricCar(string a="XXXXX",string b="XXXXX",int c=2000,int d=70,int e=0):Car(a,b,c,e),battery(d){} void info() { Car::info(); cout << "capacity: " <<battery.get_capacity() <<"-kWh"<<endl; } }; #endif
#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(85000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("Tesla", "model s", 2016); newcar.update_odometers(7800); cout << "\n--------newcar's info--------\n"; newcar.info(); }
# ifndef PETS_H # define PETS_H # include <iostream> using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string s):nickname(s) {} virtual string talk() { return " "; } string get_nickname() { return nickname; } }; class PetCats :public MachinePets { public: PetCats(const string a):MachinePets(a) {} string talk(){ return "miao wu~"; } }; class PetDogs :public MachinePets { public: PetDogs(const string s):MachinePets(s){} string talk(){ return "wang wang~"; } }; # endif
#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); }