实验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下运行截图

原文地址:https://www.cnblogs.com/jiahewang/p/9147272.html

时间: 2024-08-13 20:41:42

实验6 类的继承和多态的相关文章

实验五--类的继承派生多态二次练习

实验目的 1. 理解运行时多态 2. 掌握虚函数和抽象类 实验准备 1. 概念 多态,运行时多态 虚函数, 纯虚函数, 抽象类 2. 语法和应用 虚函数的定义和使用纯虚函数.抽象类的定义和使用 实验内容 设计并实现一个机器宠物类MachinePets. 每个机器宠物有如下信息:昵称(nickname) 每个机器宠物有如下成员函数: 带参数的构造函数MachinePets(const string s) ,为机器宠物初始化昵称. 纯虚函数string talk()为机器宠物派生类提供宠物叫声的统一

实验6:类的继承和多态

南京信息工程大学实验报告 实验名称 类的继承和多态 实验日期 2018-5-29 得分 指导教师 耿学华 系 计软院 专业 计嵌+软嵌 年级 2017 级 班次 (1) 姓名 施昊阳 学号 20161334026 一.实验目的 理解类的继承和派生机制 掌握派生类的定义和使用 掌握派生类成员的标识与访问中同名覆盖原则.二元作用域分辨符和虚基类的用法 掌握派生类构造函数和析构函数的定义及调用次序 理解运算符重载的目的,掌握运算符重载函数的编写方法 二.实验准备 类的继承和派生 请结合第 7 章课件和

python之7-2类的继承与多态

类的继承的意思就如同父子关系一样,这个儿子继承了父亲的一切,但是在某些地方(属性)相同的时候,儿子的属性大于老子的属性(覆盖),最底层类,总会继承最接近它的那个类的属性init 类的多态总是和继承相连的,没有继承,就没有多态一说.一个子类的实例,它即属于这个子类,也属于父类,比如:父亲A和儿子B,儿子B即属于儿子类,也属于人类,但是它不属于父亲类 多态是面向对象语言的一个基本特性,多态意味着变量并不知道引用的对象是什么,根据引用对象的不同表现不同的行为方式.在处理多态对象时,只需要关注它的接口即

【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态

一.简介 Lua是一门非常强大.非常灵活的脚本语言,自它从发明以来,无数的游戏使用了Lua作为开发语言.但是作为一款脚本语言,Lua也有着自己的不足,那就是它本身并没有提供面向对象的特性,而游戏开发是一项庞大复杂的工程,如果没有面向对象功能势必会为开发带来一定的不便.不过幸好Lua中有table这样强大的数据结构,利用它再结合元表(metatable),我们便可以很方便地在Lua中模拟出类.继承和多态等面向对象编程具有的特性. 二.前提知识 按照惯例,我们还是先来熟悉一下必要的前提知识,以便方便

综合运用类、继承、多态,完成一个公司人员管理类层次结构(未完待续)

1.Target /*综合运用类.继承.多态等技术,完成一个公司人员管理类层次结构,用来描述人员信息等, 重载各种运算符,完成数据库内容的赋值.添加.工资增长等.*/ 2.Code #include <iostream> #include <cstring> #include <string> #include <cstdio> #include<cstdlib> #define TECH const string name, const int

类的继承和多态细思

类的继承和多态细思 一.前言   类的继承和多态光靠概念和想象是不够的,我们需要编码去分析之,因此我使用代码来说明一些很难分清的东西. 二.分析代码 A类代码: package zyr.study.poly; public class A { public A(){ System.out.println("初始化A..."); } public String show(A obj) { return ("A and A"); } public String show

python类的继承和多态

python类的继承和多态 现在属于是老年人的脑子,东西写着写着就忘了,东西记着记着就不知道了.之前学C++的时候就把类.对象这块弄得乱七八糟,现在是因为很想玩python,所以就看看python的类和对象. 就像说的,类有三个特征:封装.继承.多态. 1.封装:类封装了一些方法,可通过一定的规则约定方法进行访问权限. C++中的成员变量有public.private.ptotected. 在python中,默认的成员变量都是public的,如果成员变量前面是加两个下划线,表示该成员变量是私有的

实验4 类的继承、派生和多态(1)

实验目的 1. 理解类的继承和派生机制 2. 掌握派生类的定义和使用 3. 理解和掌握派生类成员的标识和访问中同名覆盖原则.二元作用域分辨符和虚基类的用法 4. 掌握派生类构造函数和析构函数的定义及调用次序 5. 理解运算符重载的目的,掌握运算符重载函数的编写方法 实验准备 1. 类的继承和派生 引入继承和派生机制的目的 基本概念:继承.派生.基类.直接基类.间接基类 .派生类 语法 派生类定义的语法格式(单重继承和多重继承) 派生类构造函数及其初始化列表书写形式 派生类成员的标识与访问 同名覆

c++实验四 类的继承、派生和多态

实验目的 1. 理解类的继承和派生机制 2. 掌握派生类的定义和使用 3. 理解和掌握派生类成员的标识和访问中同名覆盖原则.二元作用域分辨符和虚基类的用法 4. 掌握派生类构造函数和析构函数的定义及调用次序 5. 理解运算符重载的目的,掌握运算符重载函数的编写方法 实验内容 一.ElectricCar类 #ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int nb = 70); int showbattery