小白成长之路(7)--C++多层派生类继承实例(期中考试)
2017-11-02 18:09
337 查看
没有一点点防备,专业老师来了一次突击期中考试,题目倒是不难,可做的时候总是有点紧张,以至于刚写完红线不少,不过还好,完美通关,特在此贴上代码,请各位大神帮助指出不足之处,给小白指点指点,再次感谢(小白代码,可笑之处轻喷)。
题目:
定义一个父类Vehicle,包含一个私有变量name,一个函数run();
定义两个子类Car,Plane,各包含一个私有变量move(运行方式);
输出结果:Car is running through the wheel.
Plane is running through the wings.
(1).首先定义类文件和头文件
(2).Vehicle头文件代码
(3).Vehicle类文件代码
(4).Car头文件代码
(5).Car类文件代码
(6).Plane头文件代码
(7).Plane类文件代码
(8).main文件代码
运行结果:
如有不足之处请各位大神不吝赐教,谢谢!
题目:
定义一个父类Vehicle,包含一个私有变量name,一个函数run();
定义两个子类Car,Plane,各包含一个私有变量move(运行方式);
输出结果:Car is running through the wheel.
Plane is running through the wings.
(1).首先定义类文件和头文件
(2).Vehicle头文件代码
#pragma once #include <iostream> #include <string> using namespace std; class Vehicle { private: string name; public: Vehicle(string name);//含参数构造函数 ~Vehicle(); public: void set_name(string name); string get_name(); public: virtual void run(); };
(3).Vehicle类文件代码
#pragma once #include "Vehicle.h" Vehicle::Vehicle(string name) {//含参数构造函数 this->name = name; } Vehicle::~Vehicle() { } void Vehicle::set_name(string name) { this->name = name; } string Vehicle::get_name() { return name; } void Vehicle::run() { cout << name << " " << "is running" << " " ; }
(4).Car头文件代码
#pragma once #include "Vehicle.h" class Car : public Vehicle { private: string move; public: Car(string name, string move); ~Car(); public: void set_move(string move); string get_move(); public: void run(); };
(5).Car类文件代码
#include "Car.h" Car::Car(string name, string move) :Vehicle(name) { this->move = move; } Car::~Car() {//析构函数 } void a43f Car::set_move(string move) { this->move = move; } string Car::get_move() { return move; } void Car::run() { Vehicle::run(); cout << " " << "through" << " " << move << endl; }
(6).Plane头文件代码
#pragma once #include "Vehicle.h" class Plane : public Vehicle { private: string move; public: Plane(string name, string move); ~Plane(); public: void set_move(string move); string get_move(); public: void run(); };
(7).Plane类文件代码
#include "Plane.h" Plane::Plane(string name, string move) :Vehicle(name) { this->move = move; } Plane::~Plane() {//析构函数 } void Plane::set_move(string move) { this->move = move; } string Plane::get_move() { return move; } void Plane::run() { Vehicle::run(); cout << " " << "through" << " " << move << endl; }
(8).main文件代码
#pragma once #include "Vehicle.h" #include "Car.h" #include "Plane.h" #include <iostream> using namespace std; void show_vechiel(Vehicle* v) { v->run(); } int main() { Vehicle* d = new Car("Car","the wheel."); show_vechiel(d); delete d; Vehicle* p = new Plane("Plane", "the wings."); show_vechiel(p); delete p; cin.get(); }
运行结果:
如有不足之处请各位大神不吝赐教,谢谢!
相关文章推荐
- C++私有继承派生类转换成基类引用实例
- C++ 基类构造函数带参数的继承方式及派生类的初始化
- C++基础(九)继承与派生——派生类成员的构造函数和析构函数
- 学习笔记之深入浅出MFC 第8章 C++重要性质----基类与派生类:谈继承(Inheritance)
- C++中 public和private派生类继承问题和访问权限问题
- struct使用继承c++代码实例及运行结果
- C++派生类继承的理解
- [C++]派生类构造函数举例(多继承、含有内嵌对象)
- 共有继承,保护继承,私有继承的区别c++代码实例
- 继承与派生类实例
- C++中 public和private派生类继承问题和访问权限问题
- 从零开始学C++之继承(二):继承与构造函数、派生类到基类的转换
- C++继承时派生类与基类有同名函数时如何分别引用
- C++中继承使用实例
- 小白学c++之继承、动态绑定
- struct使用继承c++代码实例及运行结果
- 【C++的探索路11】继承与派生之拓展篇--多形式派生以及派生类指针转换
- 共有继承,保护继承,私有继承的区别c++代码实例
- 第十二周C++【任务1】理解基类中成员的访问限定符和派生类的继承方式
- 【C++继承与派生之二】有子对象的派生类的构造函数