码迷,mamicode.com
首页 > 其他好文 > 详细

实验6

时间:2018-06-07 20:51:07      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:called   des   prot   ios   public   weight   TE   tor   include   

1

#include<iostream>
using namespace std;

class Xinpian {
public:
    Xinpian(int a, int b);
    int plus();
protected:
    int x;
    int y;
};

class A :public Xinpian {
public:
    A(int a, int b) :Xinpian(a, b) {};
    int minus();

};

class B :public Xinpian {
public:
    B(int a, int b) :Xinpian(a, b) {};
    int mul();
};

class C :public Xinpian {
public:
    C(double a, double b) :Xinpian(a, b) {};
    double div();
};



Xinpian::Xinpian(int a, int b)
{
    x = a;
    y = b;
}

int Xinpian::plus() {
    return x + y;
}

int A::minus() {
    return x - y;
}

int B::mul() {
    return x * y;
}

double C::div() {
    return x / y;
}

int main() {
    A x1(2,3);
    B x2(2,3);
    C x3(2.0,3.0);
    
    cout << "2+3=" << x1.plus() << " " << "2-3=" <<x1.minus() << endl;
    cout << "2+3=" << x2.plus ()<< " " << "2*3=" << x2.mul() << endl;
    cout << "2+3=" << x3.plus() << " " << "2/3=" << x3.div()<< endl;
    return 0;
}

技术分享图片

2

#include<iostream>
using namespace std;

class Vehicle {
public:
    Vehicle(int m,int w);
    ~Vehicle();
    void run();
    void stop();
private:
    int maxspeed;
    int weight;
};

class bicycle :virtual Vehicle {
public:
    bicycle(int m,int w,int h);
    ~bicycle();
private:
    int height;

};

class motorcar :virtual Vehicle {
public:
    motorcar(int m,int w,int s);
    ~motorcar();
private:
    int seatnum;

};

class motorcycle :public bicycle,public motorcar {
public:
    motorcycle(int m, int w, int h, int s);
    ~motorcycle();

};


void Vehicle::run() {
    cout << "run" << endl;
}

void Vehicle::stop() {
    cout << "stop" << endl;
}

Vehicle::Vehicle(int m, int w) :maxspeed(m), weight(w) {
    cout << "Vehicle constructor called."<<endl;
}

Vehicle::~Vehicle() {
    cout << "Vehicle constructor called."<<endl;
}

bicycle::bicycle(int m,int w,int h) :Vehicle(m,w),height(h){
    cout << "bicycle constructor called."<<endl;
}

bicycle::~bicycle()
{
    cout << "bicycle destructor called."<<endl;
}

motorcar::motorcar(int m, int w, int s) :Vehicle(m, w), seatnum(s) {
    cout << "motorcar constructor called."<<endl;
}

motorcar::~motorcar(){
    cout << "motorcar destructor called."<<endl;
}

motorcycle::motorcycle(int m, int w, int h, int s) :Vehicle(m, w),motorcar(m, w, s),bicycle(m, w, h){
    cout << "Motorcycle constructor called." << endl;
}

motorcycle::~motorcycle()
{
    cout << "Motorcycle destructor called." << endl;
}

int main() {
    motorcycle x1(2,4,5,3);
    Vehicle x2(2,1);
    x2.run();
    x2.stop();
    return 0;
}

技术分享图片

3

#pragma once
class Fraction {
public:
    Fraction();
    Fraction(int a);
    Fraction(int a, int b);
    void compare(Fraction &p);
    void output();
    Fraction operator+(const Fraction &p);
    Fraction operator-(const Fraction &p);
    Fraction operator*(const Fraction &p);
    Fraction operator/(const Fraction &p);

protected:
    int top;
    int bottom;

};
#pragma once
class iFraction :public Fraction {
public:
    iFraction(int x,int y);
    iFraction(int x, int y,int z);
    void show();
    void convertF();

private:
    int before;
    int top;
    int bottom;

};
#include <iostream>
#include <cmath>
#include "Fraction.h"
using namespace std;

Fraction::Fraction() {
    top = 0;
    bottom = 1;
    
}
Fraction::Fraction(int a) {
    top = a;
    bottom = 1;
}
Fraction::Fraction(int a, int b) {
    top = a;
    bottom = b;
}

void Fraction::compare(Fraction &p) {
    if (p.top / p.bottom > top / bottom)
        cout << "t>b" << endl;
    else if (p.top / p.bottom < top / bottom)
        cout << "t<b" << endl;
    else if (p.top / p.bottom == top / bottom)
        cout << "t=b" << endl;
}
void Fraction::output() {
    cout << "The result is:" << top << ‘/‘ << bottom << endl;
}

Fraction Fraction::operator+(const Fraction &p){
    Fraction sum;
    sum.top = p.top*bottom + p.bottom*top;
    sum.bottom = p.bottom*bottom;
    return sum;
}

Fraction Fraction::operator-(const Fraction &p) {
    Fraction diff;
    diff.top = p.top*bottom - p.bottom*top;
    diff.bottom = p.bottom*bottom;
    return diff;
}

Fraction Fraction::operator*(const Fraction &p){
    Fraction result;
    result.top = p.top*top;
    result.bottom = p.bottom*bottom;
    return result;
}

Fraction Fraction::operator/(const Fraction &p){
    Fraction quotien;
    quotien.top = p.top*bottom;
    quotien.bottom = p.bottom*top;
    return quotien;
}
#include<iostream>
#include"Fraction.h"
#include"iFraction.h"
using namespace std;

iFraction::iFraction(int x,int y) {
    top = x;
    bottom = y;
}

iFraction::iFraction(int x, int y,int z)
{
    top = x;
    bottom = y;
    before = z;
}

void iFraction::show() {
    cout<<"The result is:" << before << " " << top << "/" << bottom << endl;
}

void iFraction::convertF() {
    int t = 0, x, y;
    if (top >= bottom) {
        x = top;
        y = bottom;
        if (x%y != 0)
        {
            while (x>y)
            {
                x = x - y;
                t++;
            }
            cout<<"The result is:" << t << " " << x << "/" << y << endl;
        }
        if (x%y == 0)
            cout << x / y << endl;
    }
    if (top<bottom)
        cout << top << "/" << bottom << endl;
}
#include<iostream>
#include"Fraction.h"
#include"iFraction.h"
using namespace std;

int main() {
    Fraction a;
    Fraction b(3, 4);
    Fraction c(5);
    Fraction d;
    iFraction e(2, 3,1);
    iFraction f(5, 3);
    d.compare(b);
    d.output();
    d=b + c;
    d.output();
    d=b - c;
    d.output();
    d=b*c;
    d.output();
    d=b / c;
    d.output();
    e.show();
    f.convertF();
    return 0;
    
}

技术分享图片

实验6

标签:called   des   prot   ios   public   weight   TE   tor   include   

原文地址:https://www.cnblogs.com/breadbaek/p/9107654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!