实验6继承和多态
Posted werol714
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验6继承和多态相关的知识,希望对你有一定的参考价值。
实验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;那一行错了,我不太懂……把派生类删了也是那个地方报错。
以上是关于实验6继承和多态的主要内容,如果未能解决你的问题,请参考以下文章