首页 > 其他 > 详细

实验三-类的实现

时间:2019-04-20 00:51:47      阅读:301      评论:0      收藏:0      [点我收藏+]

part1:

技术分享图片技术分享图片技术分享图片技术分享图片

 

 

技术分享图片

void Graph::draw() {

   int i,a,b,t;

   a=size;

   b=size;

   t=1;

   while(a--)

   {

       for(i=0;i<b-1;i++)

           cout<<‘ ‘;

       for(i=0;i<t;i++)

       cout<<symbol;

       for(i=0;i<b-1;i++)

           cout<<‘ ‘;

       cout<<endl;

       b=b-1;

       t=t+2;

   }

}技术分享图片

part3:

#ifndef FRACTION_H

#define FRACTION_H

#include<iostream>

using namespace std;

class Fraction

{

public:

     Fraction (int x = 0, int y = 1);

     friend void jia(Fraction a,Fraction b);

     friend void jian(Fraction a, Fraction b);

     friend void cheng(Fraction a, Fraction b);

     friend void chu(Fraction a, Fraction b);

     friend void bijiao(Fraction a, Fraction b);

private:

    int top;

    int bottom;

};

#endif

#include"Fraction.h"

#include<iostream>

Fraction::Fraction(int x, int y) {

    top = x;

    bottom = y;

}

 

#include"Fraction.h"

#include<iostream>

using namespace std;

void jia(Fraction a, Fraction b) {

    int m, n;

    m = a.top*b.bottom + b.top*a.bottom;

    n = a.bottom*b.bottom;

    cout << m << ‘/‘ << n << endl;

}

void jian(Fraction a, Fraction b) {

    int m, n;

    m = a.top*b.bottom - b.top*a.bottom;

    n = a.bottom*b.bottom;

    cout << m << ‘/‘ << n << endl;

}

void cheng(Fraction a, Fraction b){

    int m, n;

    m = a.top*b.top;

    n = a.bottom*b.bottom;

    cout << m << ‘/‘ << n << endl;

}

void chu(Fraction a, Fraction b) {

    int m, n;

    m = a.top*b.bottom;

    n = a.bottom*b.top;

    cout << m << ‘/‘ << n << endl;

}

void bijiao(Fraction a, Fraction b) {

    int m;

    m = a.top*b.bottom - b.top*a.bottom;

    if (m > 0)

        cout << a.top << ‘/‘ << a.bottom;

    else

        cout << b.top << ‘/‘ << b.bottom;

}

int main() {

    Fraction a;

    Fraction b(3, 4);

    Fraction c(5);

    jia(c, b);

    jian(c, b);

    cheng(c, b);

    chu(c, b);

    bijiao(c, b);

    return 0;

}

技术分享图片

实验反思:

1、小球的函数设计十分精妙,要仔细再多看几次。

2、类的定义及实现时要注意各种小细节,这次第三部分就因为一个分号的缺少导致了卡壳,还是掌握的不够熟练。

3、Fraction中要考虑函数的友元用法,刚开始没有想到,怎么也无法实现。就只在fraction.cpp中在类下定义了函数,最后在室友及同学的帮助下想到了友元函数。

实验三-类的实现

原文:https://www.cnblogs.com/yfwg/p/10739568.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!