实验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.派生类的指针可以隐含转换为基类的指针。
在替代之后,派生类的对象就可以作为基类的对象使用,但只能使用从基类继承的成员。
battery.hpp
#ifndef BATTERY_HPP #define BATTERY_HPP #include<iostream> class battery { public: battery(int m = 70):capacity(m) { } int get_capacity(); private: int capacity; }; int battery::get_capacity() { return capacity; } #endif
car.hpp
#ifndef CAR_HPP #define CAR_HPP #include<iostream> #include<string> #include<iomanip> using namespace std; class car { public: car(string m,string n,int y,int o = 0) :maker(m), model(n), year(y), odometers(o) { } void info(); void update_odometers(int new_odometers=0); private: string maker; string model; int year; int odometers; }; void car::info() { cout << left << setw(30) << "maker:" << maker << endl; cout << left << setw(30) << "model:" << model << endl; cout << left << setw(30) << "year:" << year << endl; cout << left << setw(30) << "odometers:" << odometers << endl; } void car::update_odometers(int new_odometers) { if (new_odometers < odometers) { cout << "invalid input."; } odometers = new_odometers; } #endif
electriccar.hpp
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include<iostream> #include<string> #include<iomanip> using namespace std; #include"car.hpp" #include"battery.hpp" class electriccar :public car{ public: electriccar(string m, string n, int y, int o=0,int b=70) :car(m,n,y,o),Battery(b) { } void info(); private: battery Battery; }; void electriccar::info() { car::info(); cout << left << setw(30) << "capacity:" << Battery.get_capacity() << "-KWh" << endl; } #endif
main.cpp
#include <iostream> #include "electriccar.hpp" int main() { using namespace std; // test class of Car car oldcar("Benz", "E3", 2012); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25000); oldcar.info(); cout << endl; // test class of ElectricCar electriccar newcar("Tesla", "model x", 2020); newcar.update_odometers(2500); cout << "\n--------newcar's info--------\n"; newcar.info(); }
MachinePets.hpp
#ifndef MACHINEPETS_HPP #define MACHINEPETS_HPP #include<iostream> using namespace std; class MachinePets { public: MachinePets(const string s) :nickname(s) { } string get_nickname() { return nickname; } virtual string talk() { return "jiaosheng"; } private: string 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
main.hpp
#include <iostream> #include "MachinePets.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); }