实验四 继承
实验二源码:
#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); }
运行结果截图:

加上virtual后:

归纳总结:
同名覆盖原则:派生类与基类中有相同成员时,通过派生类对象使用的是派生类的同名成员;
二元作用域分辨符:类名称::成员名,可以精确指定某个成员;
类型兼容原则:在需要基类对象的任何地方都可以使用公有派生类的对象来替代:
(1)派生类对象可以隐含转化为基类对象,b1=d1;
(2)派生类的对象可以初始化基类的引用,B &rb=d1;
(3)派生类的指针可以隐含转化为基类的指针,pb1=&d1;
实验三:
Battery.hpp源码:
#ifndef BATTERY_HPP #define BATTERY_HPP #include<iostream> using namespace std; class Battery { private: int capacity; public: Battery(int capacity0=70):capacity(capacity0){} ~Battery(); int get_capacity(); }; Battery::~Battery() { } int Battery::get_capacity() { return capacity; } #endif
Car.hpp源码:
#ifndef CAR_HPP #define CAR_HPP #include<iostream> #include<string> using namespace std; class Car { protected: string maker,model; int year,odometers; public: Car(string maker0,string model0,int year0,int odometers0=0):maker(maker0),model(model0),year(year0),odometers(odometers0){} ~Car()=default; void info(); void update_odometers(int update); }; void Car::info() { cout<<"maker: "<<maker<<endl; cout<<"model: "<<model<<endl; cout<<"year: "<<year<<endl; cout<<"odometers: "<<odometers<<endl; } void Car::update_odometers(int update) { if(update<odometers) cout<<"wrong updating information..."; else odometers=update; } #endif
ElectricCar.hpp源码:
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include "car.hpp" #include "battery.hpp" #include<iostream> using namespace std; class ElectricCar : public Car { private: Battery battery; public: ElectricCar(string maker0,string model0,int year0,int odometers0=0,Battery battery0=70):Car(maker0,model0,year0,odometers0),battery(battery0){} ~ElectricCar()=default; void info(); }; void ElectricCar::info() { Car::info(); cout<<"capacity: "<<battery.get_capacity()<<"-kWh"<<endl; } #endif
task3.cpp源码:
#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(); }
运行结果截图:


实验四:

pets.hpp源码:
#ifndef PESTS_HPP #define PETS_HPP #include<iostream> #include<string> using std::cin; using std::cout; using std::string; class MachinePets { protected: string nickname; public: MachinePets(const string s):nickname(s){} string get_nickname()const {return nickname;} virtual string talk(){return "haha";} ~MachinePets()=default; }; 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
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); }

浙公网安备 33010602011771号