实验四 继承
2021/11/30 23:07:27
本文主要是介绍实验四 继承,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录- 实验四 继承
- 实验任务二
- task2.cpp
- 运行结果
- 总结
- 实验任务三
- Battery.hpp
- Car.cpp
- ElectricCar.hpp
- task3.cpp
- 运行结果
- 实验任务四
- pets.hpp
- task.cpp
- 运行结果
- 实验任务二
实验四 继承
实验任务二
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); }
运行结果
总结
同名覆盖原则:如果派生类声明了一个与基类成员函数同名的函数成员,那么直接使用成员名只能访问派生类的对应成员。
二元作用域分辨符:使用作用域分辨符和基类名来访问被隐藏的基类成员函数。
类型兼容原则:派生类对象可以作为基类对象使用,但是只能使用从基类继承的成员
实验任务三
Battery.hpp
#include <iostream> #include <string> #include <iomanip> using namespace std; class Battery { public: Battery(int _cap = 70) :capacity(_cap){} int get_capacity() { return capacity; } private: int capacity; };
Car.cpp
#include <iostream> #include <string> #include <iomanip> using namespace std; class Car { public: Car() = default; Car(string _maker, string _model, int _year, int _odo = 0) :maker(_maker), model(_model), year(_year), odometers(_odo){} void info() { cout.setf(ios::left); cout << setw(15) << "maker:" << maker << endl; cout << setw(15) << "model:" << model << endl; cout << setw(15) << "year:" << year << endl; cout << setw(15) << "odometers:" << odometers << endl; } void update_odometers(int _meters) { if ( _meters < odometers ) { cout << "Incorrect Value!!!" << endl; return; } else { odometers = _meters; } } private: string maker; string model; int year; int odometers; };
ElectricCar.hpp
#include "battery.hpp" #include "car.hpp" #include <iostream> using namespace std; class ElectricCar :public Car { public: ElectricCar(string _maker, string _model, int _year, int _odo = 0, Battery _bat = Battery()) :Car(_maker, _model, _year, _odo),battery(_bat){} void info() { Car::info(); cout << setw(15) << "capacity:" << battery.get_capacity() << "-kWh" << endl; } private: Battery battery; };
task3.cpp
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("PPTTYY", "b1", 2021); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(2288); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("slkadj", "ksjdl", 1234); newcar.update_odometers(423749); cout << "\n--------newcar's info--------\n"; newcar.info(); }
运行结果
实验任务四
pets.hpp
#include <iostream> #include <string> using namespace std; class MachinePets { public: MachinePets(const string s) :nickname(s){} virtual string talk() { return "xixi"; } string get_nickname()const { return nickname; } private: string nickname; }; 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~"; } };
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); }
运行结果
这篇关于实验四 继承的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?