实验四 继承
#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); }
同名覆盖原则 :当调用派生类内与基类同名的函数时,默认调用派生类内的函数。二元作用域分辨符派生类名.基类::函数名,此时调用的是作用域标定的类内函数。类型兼容原则:当派生类与基类为public派生时,可以互相调用彼此类内的函数及数据。
Graph::draw()变为虚基类,指针指向不再由指针类型决定,而是指向定义时的定义类型。于是r1指向的是Rectangle类,调用draw()时默认调用Rectangle::draw();同理c1。
#include<iostream> #include<string> using namespace std; class Battery { public: Battery(int a= 70) :capacity(a) {}//具有默认形参数的构造函数 int get_capacity() { return capacity; } private: int capacity;//车在动力电池容量 };
#include<iostream> #include<string> using namespace std; class Car { private: string maker;//制造商 string model;//型号 int year;//生产年份 int odometers;//行车里程数 public: //带参数的默认构造函数 Car(string maker1, string model1, int year1, int odometers1 = 0) :maker(maker1), model(model1), year(year1), odometers(odometers1) {} //显示车辆信息 void info() { cout << "maker:" << maker << endl << "model:" << model << endl << "year:" << year << endl << "odometers:" << odometers << endl; } //更新行车里程数 void update_odometers(int new_odometers) { if (new_odometers >= odometers)//合理 odometers = new_odometers; else//不合理 警告 cout << "New_odometers Error!" << endl; } };
#include<iostream> #include<string> using namespace std; class Car { private: string maker;//制造商 string model;//型号 int year;//生产年份 int odometers;//行车里程数 public: //带参数的默认构造函数 Car(string maker1, string model1, int year1, int odometers1 = 0) :maker(maker1), model(model1), year(year1), odometers(odometers1) {} //显示车辆信息 void info() { cout << "maker:" << maker << endl << "model:" << model << endl << "year:" << year << endl << "odometers:" << odometers << endl; } //更新行车里程数 void update_odometers(int new_odometers) { if (new_odometers >= odometers)//合理 odometers = new_odometers; else//不合理 警告 cout << "New_odometers Error!" << endl; } };
#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(); }
#include<iostream> #include<string> using namespace std; class MachinePets { public: MachinePets(const string s) :nickname(s) {}//带参数的构造函数 string get_nickname()const { return nickname; } virtual string talk() { return "0"; } 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~"; } };
#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); }