实验6,继承

//7.1#include<iostream>
using namespace std;
class Total{
    public:
        Total(int i,int j){
            cout<<"* constructor is called"<<endl;
            n=i;
            m=j;
        }
        Total(){
            cout<<"* constructor is called"<<endl;
            n=0;
            m=0;
        }
        Total(Total &total){
            cout<<"* copy constructor is called"<<endl;
            n=total.n;
            m=total.m;
        }
        int add(){
            return n+m;
        }
        ~Total() {cout<<"* destructor is called"<<endl;}
    private:
        int n;
        int m;
};
class A: public Total{
    private:
        int n;
        int m;
    public:
        A(int a,int b): Total(a,b),n(a),m(b) {cout<<"A constructor is called"<<endl;};
        A():Total(0,0),n(0),m(0) {cout<<"A constructor is called"<<endl;};
        int subtract(){
            return n-m;
        }
        ~A(){cout<<"A destructor is called"<<endl;}
};
class B: public Total{
    private:
        int n;
        int m;
    public:
        B(int a,int b): Total(a,b),n(a),m(b) {cout<<"B constructor is called"<<endl;};
        B():Total(0,0),n(0),m(0) {cout<<"B constructor is called"<<endl;};
        int multiply(){
            return n*m;
        }
        ~B(){cout<<"B destructor is called"<<endl;}
};
class C: public Total{
    private:
        int n;
        int m;
    public:
        C(int a,int b): Total(a,b),n(a),m(b) {cout<<"C constructor is called"<<endl;};
        C():Total(0,0),n(0),m(0) {cout<<"C constructor is called"<<endl;};
        int divide(){
            return n/m;
        }
        ~C(){cout<<"C destructor is called"<<endl;}
};
int main()
{
    int m,n;
    cout<<"make sure of the value of m and n"<<endl;
    cin>>m>>n;
    A a(m,n);
    B b(m,n);
    C c(m,n);
    cout<<"m + n =";
    cout<<a.add()<<endl;
    cout<<"m - n ="<<a.subtract()<<endl;
    cout<<"m * n ="<<b.multiply()<<endl;
    cout<<"m / n ="<<c.divide()<<endl;
    return 0;
}

//7.2#include<iostream>
using namespace std;
class vehicle{
    public:
        vehicle(int i,int j) : maxspeed(i) , weight(j){cout<<"vehicle‘s constructor is called"<<endl;}
        vehicle() : maxspeed(0) , weight(0){cout<<"vehicle‘s constructor is called"<<endl;}
        void initvehicle(int n,int m){maxspeed=n;weight=n;}
        void run(){
            cout<<"the vehicle‘s weight is "<<weight<<endl;
            cout<<"the vehicle‘s speed is "<<maxspeed<<endl;
        }
        void stop(){
            cout<<"the vehicle has stopped "<<endl;
        }
        ~vehicle(){cout<<"vehicle‘s destructor is called"<<endl;}
    protected:
        int maxspeed;
        int weight;
};
class bicycle:virtual public vehicle{
    public:
        bicycle(int i,int j,int k):vehicle(i,j),height(k){cout<<"the vehicle(bicycle)‘s constructor is called"<<endl;}
        bicycle():vehicle(0,0),height(0){cout<<"the vehicle(bicycle)‘s constructor is called"<<endl;}
        void initbicycle(int n){height=n;}
        ~bicycle(){cout<<"vehicle(bicycle)‘s destructor is called"<<endl;}
    protected:
         int height;
};
class motorcar:virtual public vehicle{
    public:
        motorcar(int i,int j,int k):vehicle(i,j),seatnum(k){cout<<"the vehicle(motorcar)‘s constructor is called"<<endl;}
        motorcar():vehicle(0,0),seatnum(0){cout<<"the vehicle(motorcar)‘s constructor is called"<<endl;}
        void initmotorcar(int n){seatnum=n;}
        ~motorcar(){cout<<"vehicle(motorcar)‘s destructor is called"<<endl;}
    protected:
        int seatnum;
};
class motorcycle:public bicycle,public motorcar{
    public:
    motorcycle(int i,int j,int k,int p):vehicle(i,j),bicycle(i,j,k),motorcar(i,j,p){cout<<"the vehicle(motorcycle)‘s constructor is called"<<endl;}
    motorcycle():vehicle(),bicycle(),motorcar(){cout<<"the vehicle(motorcycle)‘s constructor is called"<<endl;}
    ~motorcycle(){cout<<"vehicle(motorcycle)‘s destructor is called"<<endl;}
};
int main(){
    int m,n,p,q;
    cout<<"please decide your vehicle‘s maxspeed and weight:"<<endl;
    cin>>m>>n;
    cout<<"please decide your motorcar‘s height and seatnum:"<<endl;
    cin>>p>>q;
    motorcycle mc(m,n,p,q);
    system("pause");
    mc.run();
    system("pause");
    mc.stop();
    system("pause");
    return 0;
}

//7.3 代码大部分见上次fraction类

//main中添加代码
system("pause");
        cout<<"about ifraction---------------"<<endl;
    cout<<"make sure of the times2:"<<endl;
    int times2;
    cin>>times2;
    while(times2--){
        int p,q;
        cout<<"please make sure of the top and bottom of the fraction: "<<endl;
        cin>>p>>q;
        iFraction ifraction(p,q);
        ifraction.legal();
        convertF(ifraction);
        ifraction.print();

//iFraction类
class iFraction :public Fraction{
    public:
        iFraction(int n,int m):Fraction(n,m),wholepart(0),fractionparttop(0),fractionpartbottom(0){}
        ~iFraction(){}
        friend void convertF(iFraction &A);
        void print();
    private:
        int wholepart;
        int fractionparttop;
        int fractionpartbottom;
};
//ifraction 实现
#include<iostream>
#include"ifraction.h"
using namespace std;
void convertF(iFraction &A){
    int i;
    i=A.top;
    int j;
    j=A.bottom;
    if(i>0){
        A.wholepart=i/j;
        A.fractionparttop=i-A.wholepart*j;
        A.fractionpartbottom=j;
    }
    if(i<0){
        i=-1*i;
        A.wholepart=i/j;
        A.fractionparttop=i-A.wholepart*j;
        A.fractionpartbottom=j;
        A.wholepart=-1*A.wholepart;
    }
}

void iFraction::print(){
    if(wholepart!=0)
        cout<<wholepart<<" ("<<fractionparttop<<"/"<<fractionpartbottom<<")"<<endl;
    else
        cout<<fractionparttop<<"/"<<fractionpartbottom<<endl;
}

原文地址:https://www.cnblogs.com/tacore/p/9148140.html

时间: 2024-10-17 10:07:07

实验6,继承的相关文章

实验6 继承

一.实验目的与要求: 复习运算符重载并应用. 了解并掌握通过继承派生出一个新的类的方法. 二.实验过程: 完成实验6继承中A-C三道题,见:http://acm.hpu.edu.cn/contest.php?cid=1018,密码:c++06.将答题过程简单记录到实验过程中. 将答题结果写到实验结果中,并根据答题结果进行分析.反思,将其写到实验分析中,并写上实验时间. 请按以上要求认真填写实验报告.

实验六 继承定义与使用

实验六 继承定义与使用 实验时间 2018-9-28 1.实验目的与要求 (1) 理解继承的定义: (2) 掌握子类的定义要求 (3) 掌握多态性的概念及用法: (4) 掌握抽象类的定义及用途: (5) 掌握类中4个成员访问权限修饰符的用途: (6) 掌握抽象类的定义方法及用途: (7)掌握Object类的用途及常用API: (8) 掌握ArrayList类的定义方法及用法: (9) 掌握枚举类定义方法及用途. 2.实验内容和步骤 实验1: 导入第5章示例程序,测试并进行代码注释. 测试程序1:

201771010124 王海珍 《实验六 继承定义与使用》第六章实验总结

第一部分:理论知识学习部分 第五章 第五章学习内容主要分为七个模块,分别为: 1.类.超类和子类: a. 类继承的格式: class 新类名extends已有类名. b. 已有类称为:超类(superclass).基类(base class) 或父类(parent  class) 新类称作:子类(subclass).派生类(derived  class)或孩子类(child class) c.super是一个指示编译器调用超类方法的特有关键字,它不是一个对象的引用,不能将super赋给另一个对象

C++第五次实验-------多继承

一.问题及代码 [cpp] view plain copy /* * 文件名称: duojiicheng * 作 者:夏廷辉 * 完成日期: 2016 年 5 月 8 日 * 版 本 号:v1.0 * 对任务及求解方法的描述部分:无 * 输入描述: 无 * 问题描述: 多继承 * 程序输出: 略 * 问题分析: 略 * 算法设计: 略 */ #include<string> #include <iostream> using namespace std; class Teacher

C++ 实验七 继承与派生

1. 设计一个Person类,它有两个派生类Student和Employee,Employee有两个派生类Faculty和Staff. •Person类有一个string类型的名字name,string型的身份号id,string型的电话号码phonenumber, Person类构造函数的对应name的参数不能有默认形参值. •Student类有一个年级grade属性(Freshman.Sophomore.Junior或Senior),将属性值定义为常量: •Employee类有一个strin

C++实验5-多继承

一.问题及代码 /* 文件名称:教师干部类 作者 :尚超 日期 :2016.5.11 平台 :visual c++ 6.0 对任务及求解方法的描述部分: * 输入描述: * 问题描述:分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部). * 程序输出: 略 * 问题分析: 略 * 算法设计: 略 */ #include<iostream> #include<string> using namespac

C++第5次实验-多继承

项目填空 一.问题及代码 #include <iostream> #include <string> using namespace std; class Person { string name; // 姓名 int age; // 年龄 public: Person() {} void setname(string na) { name=na; } void setage(int a) { age=a; } string getname() { return name; } i

【TOJ 5254】C++实验:继承中的构造函数和析构函数

描述 实现C++类Base和Derived,并编写相关构造函数和析构函数,使其能够输出样例信息. 主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { Base *p = new Derived(1, 2); delete p; Base b; Derived d; return 0; } 输入 无 输出 输出样例信息. 样例输入 无 样例输出 Base Constructor 1Derived Constructor 2Derived Destructo

继承与接口课堂作业

(1)动手实验:继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大! 结论:通过 super 调用基类构造方法,必须是子类构造方法中的第一个语句.   Super调用基类构造方法的使用: 1.它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数). 2.调用super()必须写在

第43课 继承的概念及意义

1. 类之间的组合关系 (1)组合关系:整体与部分的关系 [实例分析]组合关系的描述 #include <iostream> using namespace std; //内存类 class Memory { public: Memory() { cout << "Memory()" << endl; } ~Memory() { cout << "~Memory()" << endl; } }; //硬盘类