实验任务二
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); }
运行结果
总结
1.对于同名的函数,继承类会将基类的函数覆盖掉。
2.对于继承的类的对象,可以通过 对象.基类::(同名)函数 的方式来访问被覆盖掉的同名函数。
3.如果一个类有直接或间接的虚基类,则先执行虚基类的构造函数。
4. 如果该类有其他基类,则按照它们在继承声明列表中的出现的次序,分别执行它们的构造函数,但构造过程中,不再执行它们的虚基类的构造函数。
5.添加了virtual后,fun函数输出不同。通过使函数变为虚拟函数,可以使派生类对象指针模拟为基类对象来使调用函数发生改变。
实验任务三
Battery.hpp
#ifndef BATTERY_HPP #define BATTERY_HPP #include <bits/stdc++.h> using namespace std; class Battery { public: Battery(int _capacity = 70 ):capacity(_capacity){} void get_capacity() const; private: int capacity; }; void Battery::get_capacity() const { cout << "capacity: " << capacity << "-kWh" << endl; } #endif
Car.cpp
#ifndef CAR_HPP #define CAR_HPP #include <bits/stdc++.h> using namespace std; class Car { public: Car(string a="",string b = "", int c = -1, int d = 0):maker(a),model(b),year(c),odometers(d){} void info(); void update_odometers(int a); protected: string maker; string model; int year; int odometers; }; void Car::info() { cout << "maker: " << maker << endl; cout << "mode: " << model << endl; cout << "year: " << year << endl; cout << "odometers: " << model << endl; } void Car::update_odometers(int a) { if(a < odometers) cout << "ERROR!"<<endl; else odometers = a; } #endif
ElectricCar.hpp
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include "car.hpp" #include "Battery.hpp" #include <iostream> using namespace std; class ElectricCar:public Car { public: ElectricCar(string a="",string b="", int c=-1, int d=0,int e = 70):Car(a,b,c,d),battery(e){} void info(); private: Battery battery; }; void ElectricCar::info() { Car::info(); battery.get_capacity(); } #endif
task3.cpp
#include <iostream> #include "electricCar.hpp" using namespace std; int main() { using namespace std; // test class of Car Car oldcar("audi", "A4", 2077); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("Tesla", "model S+", 2078); newcar.update_odometers(2500); cout << "\n--------newcar's info--------\n"; newcar.info();
运行结果
实验任务四
pets.hpp
#include<iostream> #include<string> using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string s) :nickname(s) {} string get_nickname()const { return nickname; } virtual string talk() { return "0"; } }; class PetCats :public MachinePets { public: PetCats(const string s) :MachinePets(s) {} string talk() { return "meow"; } }; class PetDogs :public MachinePets { public: PetDogs(const string s) :MachinePets(s) {} string talk() { return "woof"; } };
task.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); }
运行结果