关键词:
实验1:
A.h
#ifndef A_H #define A_H #include"base.h" class A:public Base public: A(int m,int n):Base(m,n); int sub(int m,int n)return m-n;; ; #endif
B.h
#ifndef B_H #define B_H #include"base.h" class B:public Base public: B(int m,int n):Base(m,n); int mul(int m,int n)return m*n;; ; #endif
‘
C.h
#ifndef C_H #define C_H #include"base.h" class C:public Base public: C(int m,int n):Base(m,n); int div(int m,int n)return m/n;; ; #endif
Base.h
#ifndef BASE_H #define BASE_H class Base public: Base(int a,int b):m(a),n(b); int add(int m,int n)return m+n;; protected: int m; int n; ; #endif
main.cpp
#include<iostream> #include"A.h" #include"B.h" #include"C.h" using namespace std; int main() int m,n; cout << "请输入整数m和n:"; cin >> m >> n; A a(m,n); B b(m,n); C c(m,n); cout << "m + n = " << a.add(m,n) << endl; cout << "m - n = " << a.sub(m,n) << endl; cout << "m * n = " << b.mul(m,n) << endl; cout << "m / n = " << c.div(m,n) << endl; return 0;
截图:
实验2:
vehicle.h
#ifndef VEHICLE_H #define VEHICLE_H class vehicle public: vehicle(int m,int w); ~vehicle(); void run(); void stop(); int showMaxspeed(); int showWeight(); private: int maxspeed; int weight; ; #endif
vehicle.cpp
#include<iostream> #include"vehicle.h" using namespace std; vehicle::vehicle(int m,int w):maxspeed(m),weight(w) cout << "constructing vehicle..." << endl; vehicle::~vehicle() cout << "destructing vehicle..." << endl; void vehicle::run()cout << "Run." <<endl; void vehicle::stop()cout << "Stop." <<endl; int vehicle::showMaxspeed()return maxspeed; int vehicle::showWeight()return weight;
bicycle.h
#ifndef BICYCLE_H #define BICYCLE_H #include"vehicle.h" class bicycle:virtual public vehicle public: bicycle(int h,int m,int w); ~bicycle(); int showHeight(); private: int height; ; #endif
bicycle.cpp
#include<iostream> #include"bicycle.h" using namespace std; bicycle::bicycle(int h,int m,int w):height(h),vehicle(m,w) cout << "constructing bicycle..." << endl; bicycle::~bicycle() cout << "destructing bicycle..." << endl; int bicycle::showHeight()return height;
motorcar.h
#ifndef MOTORCAR_H #define MOTORCAR_H #include"vehicle.h" class motorcar:virtual public vehicle public: motorcar(int s,int m,int w); ~motorcar(); int showSeatnum()return seatnum;; private: int seatnum; ; #endif
motorcar.cpp
#include<iostream> #include"motorcar.h" using namespace std; motorcar::motorcar(int s,int m,int w):seatnum(s),vehicle(m,w) cout << "constructing motorcar..." << endl; motorcar::~motorcar() cout << "destructing motorcar..." << endl;
motorcycle.h
#ifndef MOTORCYCLE_H #define MOTORCYCLE_H #include"motorcar.h" #include"bicycle.h" class motorcycle:public bicycle,public motorcar public: motorcycle(int h,int s,int m,int w); ~motorcycle(); ; #endif
motorcycle.cpp
#include<iostream> #include"motorcycle.h" using namespace std; motorcycle::motorcycle(int h,int s,int m,int w):bicycle(h,m,w),motorcar(s,m,w),vehicle(m,w) cout << "constructing motorcycle..." <<endl; motorcycle::~motorcycle() cout << "destructing motorcycle..." << endl;
main.cpp
#include<iostream> #include"motorcycle.h" using namespace std; int main() int h,s,m,w; motorcycle a(h,s,m,w); a.run(); a.stop(); return 0;
截图:
实验3:
faction.h
#ifndef FACTION_H #define FACTION_H class Faction public: Faction(); //构造函数 Faction(int t0); //构造函数的重载 Faction(int t0,int b0); //构造函数的重载 void compare(Faction &f2); //比较大小函数 void show(); //输出函数 int GetTop(); //获取分子 int GetBottom(); //获取分母 Faction operator+(Faction &f0); //重载+ Faction operator-(Faction &f0); //重载- Faction operator*(Faction &f0); //重载* Faction operator/(Faction &f0); //重载/ private: int top; //分子 int bottom; //分母 ; #endif
faction.cpp
#include "Faction.h" #include <iostream> using namespace std; //构造函数 Faction::Faction():top(0), bottom(1) //构造函数的重载 Faction::Faction(int t0):top(t0), bottom(1) //构造函数的重载 Faction::Faction(int t0,int b0):top(t0), bottom(b0) //输出函数 void Faction::show() cout << top << "/" << bottom <<endl; Faction Faction::operator+(Faction &f0) //重载+ Faction f; f.top = top *f0.bottom + f0.top*bottom; f.bottom = bottom*f0.bottom; return f; Faction Faction::operator-(Faction &f0) //重载- Faction f; f.top = top *f0.bottom - f0.top*bottom; f.bottom = bottom*f0.bottom; return f; Faction Faction::operator*(Faction &f0) //重载* Faction f; f.top *= f0.top; f.bottom *= f0.bottom; return f; Faction Faction::operator/(Faction &f0) //重载/ Faction f; f.top *= f0.bottom; f.bottom *= f0.top; return f; //比较大小函数 void Faction::compare(Faction &f1) Faction f2; f2.top = top * f1.bottom - f1.top*bottom; f2.bottom = bottom * f1.bottom; if(f2.bottom*f2.top > 0) cout << top << "/" << bottom << ">" << f1.top << "/" << f1.bottom <<endl; else cout << top << "/" << bottom << "<=" << f1.top << "/" << f1.bottom <<endl; int Faction::GetTop() return top; int Faction::GetBottom() return bottom;
ifaction.h
#include "Faction.h" #include <iostream> using namespace std; class iFaction:public Faction public: iFaction(int t,int b):Faction(t,b); iFaction(int t):Faction(t,1); iFaction():Faction(0,1); void ishow() int a = GetBottom(); //a是最大公约数 int t0 = GetTop(); int b0 = GetBottom(); int t = GetTop(); int b = GetBottom(); while (t0 % b0 != 0) a = t0% b0; t0 = b0; b0 = a; b/=a; t/=a; if (b == 0) cout << "分母不得为0!" << endl; else if (t == 0) cout << t << endl; else if ( t < b ) cout << t << "/" << b << endl; else int nu = 0; //计算系数 while ( t > 0 ) t -= b; nu++; cout << b << "(" << t+b << "/" << b << ")" <<endl; ;
main.cpp
#include <iostream> #include "iFaction.h" using namespace std; int main() iFaction a; iFaction b(18,16); iFaction c(5); iFaction d; cout << "有以下三个分数:" << endl; a.ishow(); b.ishow(); c.ishow(); d = b + c; cout << "b + c =" ; d.show(); d = b - c; cout << "b - c =" ; d.show(); d = b * c; cout << "b * c =" ; d.show(); d = b / c; cout << "b / c =" ; d.show(); cout << "b与c大小关系为:"; b.compare(c); return 0;
这个报错说using namespace std;那一行错了,我不太懂……把派生类删了也是那个地方报错。
实验4类的继承派生和多态(代码片段)
实验目的 1.理解类的继承和派生机制2.掌握派生类的定义和使用3.理解和掌握派生类成员的标识和访问中同名覆盖原则、二元作用域分辨符和虚基类的用法4.掌握派生类构造函数和析构函数的定义及调用次序5.理解运算符重载... 查看详情
实验四类的继承派生和多态(代码片段)
一、实验内容1、车辆基本信息管理基于Car类派生出ElectricCar类、派生类ElectricCar中新增数据成员Battery类对象。#ifndefBATTERY_H#defineBATTERY_HclassBatterypublic:Battery(intbatterySize0=70);intgetSize();private:intbatterySize;;#endifbatter 查看详情
实验五--类的继承派生多态二次练习(代码片段)
实验目的1.理解运行时多态2.掌握虚函数和抽象类实验准备1.概念多态,运行时多态虚函数,纯虚函数,抽象类2.语法和应用虚函数的定义和使用纯虚函数、抽象类的定义和使用实验内容设计并实现一个机器宠物类MachinePets。每个... 查看详情
c++实验四类的继承派生和多态(代码片段)
实验目的1.理解类的继承和派生机制2.掌握派生类的定义和使用3.理解和掌握派生类成员的标识和访问中同名覆盖原则、二元作用域分辨符和虚基类的用法4.掌握派生类构造函数和析构函数的定义及调用次序5.理解运算符重载的目... 查看详情
实验五类的继承派生和多态(代码片段)
一、验证性实验1.在多层继承中,派生类中出现与基类同名成员时,通过对象名.成员名的方式,访问的成员什么? 对象名.成员名:派生类对象2.通过基类指针访问派生类对象时,基类中成员函数有无关键字... 查看详情
实验五——类的多态,继承和派生2(代码片段)
Part1:验证型实验1.在派生类函数中,如果是使用对象名.成员名进行访问,则最后运行结果是访问每一个派生类中的成员函数;2.如果是通过指针来进行访问那么只会访问base1当中的成员函数,因为第一个程序里的BASE1并没有虚函... 查看详情
实验七继承附加实验(代码片段)
实验七继承附加实验实验时间2018-10-111、实验目的与要求(1)进一步理解4个成员访问权限修饰符的用途;(2)掌握Object类的常用API用法;(3)掌握ArrayList类用法与常用API;(4)掌握枚举类使用方法;(5)结合本章知识,... 查看详情
c++进阶:多态多态的构成条件|虚函数的重写|抽象类|多态的原理|多继承的虚函数表(代码片段)
...藏)、重写(覆盖)的对比三、抽象类💦概念💦接口继承和实现继承四、多态的原理Ǵ 查看详情
c++进阶:多态多态的构成条件|虚函数的重写|抽象类|多态的原理|多继承的虚函数表(代码片段)
...藏)、重写(覆盖)的对比三、抽象类💦概念💦接口继承和实现继承四、多态的原理Ǵ 查看详情
c++进阶:多态多态的构成条件|虚函数的重写|抽象类|多态的原理|多继承的虚函数表(代码片段)
...藏)、重写(覆盖)的对比三、抽象类💦概念💦接口继承和实现继承四、多态的原理Ǵ 查看详情
java面对对象三大特征之继承多态(代码片段)
面对对象三大特征继承、多态继承1.继承的概念:2.继承的实现:3.继承的好处与弊端:4.super5.继承中的成员访问特点以及访问修饰符:JAVA中的访问修饰符6.方法重写以及继承的注意事项:多态1.多态的概念2.多态中成员访问特点3.多态的... 查看详情
python入门-6面向对象编程:07面向对象三大特征(封装继承多态)-继承(代码片段)
...hon是面向对象的语言,也支持面向对象编程的三大特性:继承、封装(隐藏)、多态。 封装(隐藏) 隐藏对象的属性和实现细节,只对外提供必要的方法。相当于将“细节封装起来”,只对外暴露“相关调... 查看详情
第七周课程总结&实验报告(代码片段)
第七周课程总结一、接口的实际应用接口在实际中更多的作用是用来制定标准的。抽象类和接口的的关系No.区别点抽象类接口1定义包含一个抽象方法的类抽象方法和全局常量的集合2组成构造方法、抽象方法、普通方法、常量、... 查看详情
c++多态(代码片段)
...al重载、覆盖(重写)、隐藏(重定义)的对比抽象类概念接口继承和实现继承多态的原理虚函数表多态的原理动态绑定和静态绑定单继承和多继承关系的虚函数表单继承中的虚函数表多继承中的虚函数表菱形继承、菱形虚拟继承继承... 查看详情
深度剖析—继承和多态(代码片段)
深度剖析—继承和多态继承什么是继承?继承的语法注意事项继承的意义super关键字super和this关键字的区别🔺protected关键字多层继承final关键字继承和组合多态多态概念多态前提:向上转型向下转型动态绑定(运行时绑... 查看详情
c++学习:3多态(代码片段)
...类的成员函数实现8、虚析构函数9、纯虚函数、抽象类多继承1、多继承体系下的构造函数调用2、多继承的虚表3、同名函数、成员变量的调用4、菱形继承5、虚继承6、多继承的应用static静态成员1、静态成员变量2、静态成员函数3... 查看详情
面向对象中的多态(代码片段)
6.1.封装6.2继承6.3多态多态就是指对象的不同形态,具体表现为子类对象可以转换为父类对象,父类对象可以调用子类的方法。多态操作还是比较常用的,特别是在泛型中。父类和子类具有继承关系,子类对象可以安全地转换为... 查看详情
[c++]多态详解(代码片段)
...函数表的构建规则6.多态原理6.1动态绑定与静态绑定6.2单继承和多继承的 查看详情