第6周项目1-分数类的雏形(1)

/*
*Copyright(C) 2016,计算机与控制工程学院
*All rights reserved.
*文件名:test.cpp
*作者:张志新
*完成日期:2016年4月9日
*版本号:v1.0
*
*问题描述:分数的输入输出(1)。
*/
#include <iostream>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1);   //构造函数,初始化用
    void output(int style=0);   //输出:以8/6为例,style为0时,原样输出8/6;
};
CFraction::CFraction(int nu,int de)
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
    else
    {
        cout<<"输入格式错误";
    }
}
void CFraction::output(int style)
{
    if(style==0)
    cout<<nume<<"/"<<deno<<endl;
}
int main()
{
    CFraction a1,a2(2,4),a3(3,6);
    a1.output(0);
    a2.output(0);
    a3.output(0);
    return 0;
}
<img src="https://img-blog.csdn.net/20160412192115356?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

学习心得:

  这是一个较简单的程序,注意一个细节的地方:在类里声明的默认函数在写那些函数是注意不要加上默认值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值