task2.cpp源码
#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); }
源代码对应实验结果:
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); }
task2.cpp修改后的实验结果截图:
task2的归纳总结
同名覆盖原则:覆盖是指派生类重新实现(或者改写)了基类的成员函数,其特征如下:1.不同的作用域(分别位于派生类和基类中);2.函数名称相同;3.参数列表完全相同;4.基类函数必须是虚函数。满足这四个条件之后,派生类的对象使用函数时会进行派生类中的代码规则运行。
二元作用域分辨符:即不论是否派用类还是基类,同名函数前对应的类名是什么,对应的代码运行规则便是该类名内的规则。
类型兼容原则:1.派生类的对象可以隐含转换为基类对象;2.派生类的对象可以初始化基类的引用;3.派生类的指针可以隐含转换为基类的指针
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(); }
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("Porsche", "911", 2020); newcar.update_odometers(5200); cout << "\n--------newcar's info--------\n"; newcar.info(); }
battery.hpp源码
#include<iostream> using namespace std; class Battery{ public: Battery(int n_capacity=70):capacity(n_capacity){} int get_capacity(){ return capacity; } private: int capacity; };
car.hpp源码
#include<iostream> #include<string> using namespace std; class Car{ public: Car(string n_maker,string n_model,int n_year):maker(n_maker),model(n_model),year(n_year),odometers(0){} virtual void info(){ cout<<"maker: "<<maker<<endl; cout<<"model: "<<model<<endl; cout<<"year: "<<year<<endl; cout<<"odometers: "<<odometers<<endl; } void update_odometers(float n_odometers){ if(n_odometers<odometers) cout<<"the odometer you want to update is wrong."<<endl; else odometers=n_odometers; } private: string maker; string model; int year; float odometers; };
electricCar.hpp源码
#include<iostream> #include"battery.hpp" #include"car.hpp" using namespace std; class ElectricCar:public Car { public: ElectricCar(string n_maker,string n_model,int n_year):Car(n_maker,n_model,n_year){} void info(){ Car::info(); cout<<"battery: "<<battery.get_capacity()<<"-kwh"<<endl; } private: Battery battery; };
task3实验结果截图
task3修改后实验结果截图
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); }
pets.hpp源码
#include<iostream> #include<string> using namespace std; class MachinePets{ public: MachinePets(const string n_nickname):nickname(n_nickname){} virtual string talk(){ return "i love u"; } string get_nickname(){ return nickname; } private: string nickname; }; class PetCats:public MachinePets { public: PetCats(const string n_nickname):MachinePets(n_nickname){} string talk(){ return "miao wu~"; } }; class PetDogs:public MachinePets { public: PetDogs(const string n_nickname):MachinePets(n_nickname){} string talk(){ return "wang wang~"; } };
task4实验结果截图