实验6 类的继承和多态
Posted jiahewang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验6 类的继承和多态相关的知识,希望对你有一定的参考价值。
四、实验结论
1.实验内容1
Base.h
#ifndef Base_h
#define Base_h
using namespace std;
class Base {
public:
Base (int m0 ,int n0):m(m0),n(n0){}
int add (){return m+n;};
int getm() const {return m;};
int getn() const {return n;};
private:
int m, n;
};
#endif /*Base_h*/
A.h
#ifndef A_h
#define A_h
#include "Base.h"
class A:public Base {
public:
A (int m0, int n0):Base(m0,n0){};
int sub(){ return getm()-getn(); };
};
#endif /* A_h */
B.h
#ifndef B_h
#define B_h
#include "Base.h"
class B:public Base{
public:
B(int m0, int n0): Base(m0,n0){};
int mul(){ return getm()*getn(); };
};
#endif /* B_h */
C.h
#ifndef C_h
#define C_h
#include "Base.h"
class C : public Base{
public:
C(int m0, int n0):Base(m0,n0){};
int div(){ return getm()/getn(); };
};
#endif /* C_h */
main.cpp
#include <iostream>
#include <cmath>
#include "A.h"
#include "B.h"
#include "C.h"
using namespace std;
int main() {
int m,n;
cin>>m>>n;
A a(m,n);
cout<<"m+n="<<a.add()<<endl;
cout<<"m-n="<<a.sub()<<endl;
cin>>m>>n;
B b(m,n);
cout<<"m+n="<<b.add()<<endl;
cout<<"m*n="<<b.mul()<<endl;
cin>>m>>n;
C c(m,n);
cout<<"m+n="<<c.add()<<endl;
cout<<"m/n="<<c.div()<<endl;
return 0;
}
- Xcode下运行截图
2.实验内容2
vehicle.h
#ifndef vehicle_h
#define vehicle_h
using namespace std;
class vehicle{
private:
int maxspeed;
int weight;
public:
vehicle(int x ,int y):maxspeed(x),weight(y){}
~vehicle(){cout<<"destructing vehicle..."<<endl;};
int getmaxspeed()const{ return maxspeed; };
int getweight() const { return weight; };
void run(){cout<<"run"<<endl;};
void stop(){cout<<"stop"<<endl;};
};
#endif /* vehicle_h */
bicycle.h
#ifndef bicycle_h
#define bicycle_h
#include "vehicle.h"
class bicycle:virtual public vehicle{
private:
int height;
public:
bicycle(int x,int y, int z ):vehicle(x,y),height(z){};
~bicycle(){cout<<"destructing bicycle..."<<endl;};
int getheight() const { return height;};
};
#endif /* bicycle_h */
motorcar.h
#ifndef motorcar_h
#define motorcar_h
#include "vehicle.h"
class motorcar:virtual vehicle{
private:
int seatnum;
public:
motorcar(int x,int y,int s):vehicle(x,y),seatnum(s){};
~motorcar(){cout<<"destructing motorcar..."<<endl;};
int getseatnum() const { return seatnum;};
};
#endif /* motorcar_h */
motorcycle.h
#ifndef motorcycle_h
#define motorcycle_h
#include "bicycle.h"
#include "motorcar.h"
class motorcycle:public bicycle,public motorcar{
public:
motorcycle(int x,int y,int z,int s):bicycle(x,y,z),motorcar(x,y,s),vehicle(x,y){};
~motorcycle(){cout<<"destructing motorcycle..."<<endl;};
};
#endif /* motorcycle_h */
main.cpp
#include <iostream>
#include "motorcycle.h"
using namespace std;
int main() {
motorcycle A(12,10,8,13);
cout<<"maxspeed="<<A.getmaxspeed()<<endl;
cout<<"weight="<<A.getweight()<<endl;
cout<<"height="<<A.getheight()<<endl;
cout<<"seatnum="<<A.getseatnum()<<endl;
A.run();
A.stop();
return 0;
}
- Xcode 下运行截图:
3.实验内容3
Fraction.h
#ifndef Fraction_h
#define Fraction_h
class Fraction {
public:
Fraction():top(0),bottom(1){}; //构造函数
Fraction(int t, int b):top(t),bottom(b){}; //构造函数的重载
Fraction(int t):top(t),bottom(1){}; //构造函数的重载
//运算符重载成员函数
Fraction operator +(const Fraction &f0) const;
Fraction operator -(const Fraction &f0) const;
Fraction operator *(const Fraction &f0) const;
Fraction operator /(const Fraction &f0) const;
void show() const;
int gettop(){ return top; };
int getbottom(){ return bottom; };
private:
int top; //分子
int bottom; //分母
};
#endif /* Fraction_h */
Fraction.cpp
#include <iostream>
#include "Fraction.h"
using namespace std;
//重载运算符函数的实现
Fraction Fraction::operator+(const Fraction &f0) const{
Fraction f;
f.top=top*f0.bottom+bottom*f0.top;
f.bottom=bottom*f0.bottom;
return f;
}
Fraction Fraction::operator-(const Fraction &f0) const{
Fraction f;
f.top=top*f0.bottom-bottom*f0.top;
f.bottom=bottom*f0.bottom;
return f;
}
Fraction Fraction::operator*(const Fraction &f0) const{
Fraction f;
f.top=top*f0.top;
f.bottom=bottom*f0.bottom;
return f;
}
Fraction Fraction::operator/(const Fraction &f0) const{
Fraction f;
f.top=top*f0.bottom;
f.bottom=bottom*f0.top;
return f;
}
//输出函数的实现
void Fraction::show() const {
cout<<top<<"/"<<bottom<<endl;
}
iFraction.h
#ifndef iFraction_h
#define iFraction_h
#include "Fraction.h"
class iFraction:public Fraction{
public:
//为派生类 iFraction 定义构造函数,实现 iFraction 对象的初始化
iFraction():Fraction(0,1),real(0){};
iFraction(int t, int b,int a):Fraction(t,b),real(0){};
iFraction(int t):Fraction(t,1),real(0){};
void prinf();
int getreal(){ return real;};
//convertF()用于对 iFraction类对象进行规范化处理
friend void convertF(iFraction &f);
private:
int real;
};
#endif /* iFraction_h */
iFraction.cpp
#include <iostream>
#include "iFraction.h"
using namespace std;
void iFraction::prinf(){
cout<<gettop()<<endl;
cout<<"--"<<endl;
cout<<getbottom()<<endl;
}
void convertF(iFraction &f){
int t, b, r;
t=f.gettop();
b=f.getbottom();
r=f.getreal();
if(t>b){
r=t/b;
t=t%b;
}
cout<<" "<<t<<endl;
cout<<r<<"---"<<endl;
cout<<" "<<b<<endl;
}
main.cpp
#include <iostream>
#include "iFraction.h"
#include <cmath>
using namespace std;
int main() {
Fraction a(5,6);
cout<<"a=";
a.show();
Fraction b(3);
cout<<"b=";
b.show();
Fraction c;
c=a+b;
cout<<"a+b=";
c.show();
c=a-b;
cout<<"a-b=";
c.show();
c=a*b;
cout<<"a*b=";
c.show();
c=a/b;
cout<<"a/b=";
c.show();
iFraction x(5,3,0);
x.prinf();
cout<<"可化为真分数:"<<endl;
convertF(x);
return 0;
}
- Xcode下运行截图
以上是关于实验6 类的继承和多态的主要内容,如果未能解决你的问题,请参考以下文章