zoukankan      html  css  js  c++  java
  • 实验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;
    }

  • 相关阅读:
    uCOS的软件定时器、uCOS时钟节拍和滴答定时器的关系
    学习ucosii要用到的几本书
    freertos知识点笔记——队列、二值信号量、计数信号量
    《嵌入式软件设计基础——基于ARM Cortex—M3》读书笔记
    大小端测试C实现
    static 的三个作用
    基于IAR6或者IAR7建立STM32开发工程(通过实际测试,使用IAR6.30.4)
    STM32中stm32f0xx_flash.icf文件的作用详解!(不错的!)
    CRC点滴
    int *a[] 与(int *)a【5】的区别
  • 原文地址:https://www.cnblogs.com/tacore/p/9148140.html
Copyright © 2011-2022 走看看