实验四 继承
实验任务2
实验源码
#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
加virtual
同名覆盖原则:在派生类中定义了和基类相同的函数,那么派生类会覆盖基类的函数。
二元作用域分辨符:为了访问基类同名成员。
类型兼容原则:在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。
实验任务3
battery.h
#ifndef BATTERY_H #define BATTERY_H #include<iostream> using namespace std; class Battery{ public: Battery(int a=70):capacity{a}{}; void get_capacity(); private: int capacity; }; void Battery::get_capacity() { cout<<capacity<<"-kwh"<<endl; } #endif
car.h
#ifndef CAR_H #define CAR_H #include<iostream> using namespace std; class Car{ public: Car(string a,string b,int c):maker{a},model{b},year{c}{odometers=0;}; void info(); void update_odometers(int m); protected: string maker; string model; int year; int odometers; }; void Car::info() { cout<<"maker:"<<maker<<endl; cout<<"model:"<<model<<endl; cout<<"year:"<<year<<endl; cout<<"odometers"<<odometers; } void Car::update_odometers(int m) { if(m<odometers) cout<<"wrong odometer"<<endl; else { odometers=m; cout<<"update success"<<endl; } } #endif
electricCar.h
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include<iostream> #include"car.h" #include"battery.h" using namespace std; class ElectricCar:public Car{ public: ElectricCar(string a,string b,int c):Car{a,b,c}{}; void info(); protected: Battery battery; }; void ElectricCar::info() { cout<<"maker:"<<maker<<endl; cout<<"model:"<<model<<endl; cout<<"year:"<<year<<endl; cout<<"odometers:"<<odometers<<endl; cout<<"capacity:"; battery.get_capacity(); } #endif
task3.cpp
#include <iostream> #include "electricCar.h" 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 "electricCar.h" int main() { using namespace std; // test class of Car Car oldcar("BYD", "newbrand", 2020); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(15); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("Tesla", "model x", 2021); newcar.update_odometers(15); cout << "\n--------newcar's info--------\n"; newcar.info(); }
实验任务四
pets.h
#ifndef PETS_H #define PETS_H #include<iostream> using namespace std; class MachinePets { public: MachinePets(const string s) :nickname{ s } {} virtual string talk() { return "123"; } const string get_nickname() { return nickname; } protected: string nickname; }; class PetCats :virtual public MachinePets { public: PetCats(const string s) :MachinePets{ s } {} string talk() { return "miao wu~"; } }; class PetDogs :virtual public MachinePets { public: PetDogs(const string s) :MachinePets{ s } {} string talk() { return "wang wang~"; } }; #endif
task4.cpp
#include <iostream> #include "pets.h" 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); }
实验截图