实验四 继承
task2.cpp 改动后代码
#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); }
改动前运行结果
改动后运行结果截图
同名覆盖原则:派生类与基类中有相同成员时,若未强行指名,则通过派生类对象使用的是派生类的同名成员;
如果要通过派生类的对象访问基类被覆盖的同名成员,需要加对象名.基类名::同名成员来限定
二元作用域分辨符:用法:类名::成员
在多重继承的情况下,如果两个基类中存在相同的变量名,可以使用作用域运算符来进行区分
类型兼容原则:类型兼容规则是指在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。通过公有继承,派生类得到了基类中除构造函数、析构函数之外的所有成员。
类型兼容原则中在替代之后,派生类对象就可以作为基类的对象使用,但是只能使用从基类继承的成员。
task3
battery.hpp
#include<iostream> using namespace std; class Battery{ public: int capacity; Battery(int capacity0=70):capacity(capacity0){ } int get_capacity(); }; int Battery::get_capacity() { return capacity; }
car.hpp
#include<iostream> #include<string> #include<iomanip> using namespace std; class Car{ public: string maker,model; int year,odometers; Car(string maker0,string model0,int year0,int odometers0=0):maker(maker0),model(model0),year(year0),odometers(odometers0){ } void info(); void update_odometers(int odometers); }; void Car::info() { cout<<std::left<<setw(12)<<"maker:"<<maker<<endl; cout<<std::left<<setw(12)<<"model:"<<model<<endl; cout<<std::left<<setw(12)<<"year:"<<year<<endl; cout<<std::left<<setw(12)<<"odometers:"<<odometers<<endl; } void Car::update_odometers(int odometers1) { if(odometers1<odometers) { cout<<"更新数值有误"<<endl; } else odometers=odometers1; }
electricCar.hpp
#include<iostream> #include<string> #include<iomanip> #include"car.hpp" #include"battery.hpp" using namespace std; class ElectricCar:public Car{ public: ElectricCar(string maker0,string model0,int year0,int odometers0=0,int capacity0=70):Car(maker0,model0,year0,odometers0),battery(capacity0){ }; void info(); private: Battery battery; }; void ElectricCar::info() { Car::info(); cout<<std::left<<setw(12)<<"capacity:"<<battery.capacity<<"-kWh"<<endl; }
task3.cpp
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("aaa", "a11",9999); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(100000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("zzz", "z9", 1217); newcar.update_odometers(5200); cout << "\n--------newcar's info--------\n"; newcar.info(); }
运行结果如下
task4
pets.hpp
#include<iostream> #include<string> using namespace std; class MachinePets { public: string nickname; MachinePets(const string s):nickname(s){ } string get_nickname() const; virtual string talk(){ } }; string MachinePets::get_nickname() const { return nickname; } class PetCats:public MachinePets { public: PetCats(const string s):MachinePets(s){ } string talk(); }; string PetCats::talk() { return "mao wu~"; } class PetDogs:public MachinePets { public: PetDogs(const string s):MachinePets(s){ } string talk(); }; string PetDogs::talk() { return "wang wang~"; }
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); }
运行结果如下