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

实验三-类的实现

时间:2019-04-20 09:20:39      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:.com   ret   oid   draw   实现   str   define   没有   ios   

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中在类下定义了函数,最后在室友及同学的帮助下想到了友元函数。

实验三-类的实现

标签:.com   ret   oid   draw   实现   str   define   没有   ios   

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

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