程序设计二(面向对象)_实训4_对象与函数

第1关:类对象作为函数形参

任务描述

编写一个 output 函数,以 Int 的常引用作为形参,其功能是输出形参的成员变量的值。输出为一行。

/********* Begin ********/
#include"Int.h"
#include<stdio.h>
void output(Int const&rhs)
{
    int v=rhs.getValue();
    printf("%d",v);
}

/********* End **********/

第2关:对象作为函数返回值

任务描述

编写 2 个函数,分别是 addmul,分别完成 Int 的加法操作与乘法操作。2 个函数均拥有 2 个 Int 常引用的形参,返回类型均为 Int

/********* Begin ********/
#include"Int.h"
Int add(const Int&lhs,const Int&rhs)
{
    int v=lhs.getValue()+rhs.getValue();
    Int m=Int(v);
    return m;
}
Int mul(Int const&lhs,Int const&rhs)
{
    int v=lhs.getValue()*rhs.getValue();
    Int m=Int(v);
    return m;
}

/********* End **********/

第3关:类对象作为输出参数

任务描述

编写 2 个函数,分别是 addmul,分别完成 Int 的加法操作与乘法操作。这个 2 个函数均为 void 类型,拥有 3 个参数。其中前 2 个是输入参数,最后 1 个是输出参数

/********* Begin ********/
#include "Int.h"
void add(Int const&lhs,Int const&rhs,Int&ret)
{
    int v=lhs.getValue()+rhs.getValue();
    ret.setValue(v);
}
void mul(Int const&lhs,Int const&rhs,Int&ret)
{
    int v=lhs.getValue()*rhs.getValue();
    ret.setValue(v);
}

/********* End **********/

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐