c++ - 如何修改一个类,使其只有一个成员函数,所有参数都默认?

标签 c++ class

我是编程(一般)和 C++(特别是)的新手,目前正在学习类和对象。

我将以下内容定义为练习:

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

class X
{
public:

    void setNum_1(int);
    void setNum_2(int);
    void setNum_3(int);
    void setNum_4(int);

    double getNum_1();
    double getNum_2(int num_1);
    double getNum_3(int num_1, int num_2);
    double getNum_4(int num_1, int num_2, int num_3);

private:
    int num_1;
    int num_2;
    int num_3;
    int num_4;
};

int main()
{
    X testObject;

    int lNum_1 = 0;
    int lNum_2 = 0;
    int lNum_3 = 0;
    int lNum_4 = 0;

    cout << endl;
    cout << "Please enter an integer: ";
    cin >> lNum_1;

    cout << "Please enter an integer: ";
    cin >> lNum_2;

    cout << "Please enter an integer: ";
    cin >> lNum_3;

    cout << "Please enter an integer: ";
    cin >> lNum_4;

    testObject.setNum_1(lNum_1);
    testObject.setNum_2(lNum_2);
    testObject.setNum_3(lNum_3);
    testObject.setNum_4(lNum_4);

    cout << endl;
    cout << "The 1st number returned is: " << testObject.getNum_1() << endl;
    cout << "The 2nd number returned is: " << testObject.getNum_2(lNum_1) << endl;
    cout << "The 3rd number returned is: " << testObject.getNum_3(lNum_1, lNum_2) << endl;
    cout << "The 4th number returned is: " << testObject.getNum_4(lNum_1, lNum_2, lNum_3) << endl;
    cout << endl;

    return 0;
}

void X::setNum_1(int n_1)
{
    num_1 = n_1;
}

void X::setNum_2(int n_2)
{
    num_2 = n_2;
}

void X::setNum_3(int n_3)
{
    num_3 = n_3;
}

void X::setNum_4(int n_4)
{
    num_4 = n_4;
}

double X::getNum_1()
{
    return sqrt(num_1);
}

double X::getNum_2(int num_1)
{
    return pow(num_2,3);
}

double X::getNum_3(int num_1, int num_2)
{
    return num_1 * num_2;
}

double X::getNum_4(int num_1, int num_2, int num_3)
{
    return (num_1 + num_2) / num_3;
}

谁能提供一些关于如何修改此类的指导,使其只有一个成员函数且所有参数都默认?

提前致谢, 瑞安

最佳答案

当然,有很多方法可以做到这一点,其中大部分都比我在这里所做的更优雅。但这应该会给您一些想法。

一个。您应该使用类中定义的变量 b.如果需要在函数中执行某些条件操作,请使用 switch case 或 if 语句。并根据传递的参数决定操作。

同样,有很多方法可以使它更优雅,但这应该让您开始思考。

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

class X
{
public:

    void setNums(int, int, int, int);
    double performOp(int);

private:
    int num_1;
    int num_2;
    int num_3;
    int num_4;
};

int main()
{
    X testObject;

    int lNum_1 = 0;
    int lNum_2 = 0;
    int lNum_3 = 0;
    int lNum_4 = 0;

    cout << endl;
    cout << "Please enter an integer: ";
    cin >> lNum_1;

    cout << "Please enter an integer: ";
    cin >> lNum_2;

    cout << "Please enter an integer: ";
    cin >> lNum_3;

    cout << "Please enter an integer: ";
    cin >> lNum_4;

    testObject.setNums(lNum_1,lNum_2,lNum_3,lNum_4);

    cout << endl;
    cout << "The 1st number returned is: " << testObject.performOp(1) << endl;
    cout << "The 2nd number returned is: " << testObject.performOp(2) << endl;
    cout << "The 3rd number returned is: " << testObject.performOp(3) << endl;
    cout << "The 4th number returned is: " << testObject.performOp(4) << endl;
    cout << endl;

    return 0;
}

void X::setNums(int n_1, int n_2, int n_3, int n_4)
{
    num_1 = n_1;
    num_2 = n_2;
    num_3 = n_3;
    num_4 = n_4;
}

double X::performOp(int n)
{
    if(n == 1) return sqrt(num_1);
    if(n == 2) return pow(num_2,3);
    if(n == 3) return num_1 * num_2;
    if(n == 4) return (num_1 + num_2) / num_3;
}

关于c++ - 如何修改一个类,使其只有一个成员函数,所有参数都默认?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24681116/

相关文章:

c++ - OpenGL 中顶点数组缓冲区绘制的每个函数有什么作用?

c++ - 错误: Object is undeclare, 第一次使用这个函数

php - 只有变量应该通过引用传递

c++ - 可以使用模板在 C++ 中按名称访问位字段成员吗?

c++ - clang::ast_type_traits::DynTypedNode::get() 无法推断模板参数 'T'

android - 无法在 fragment 类中获取操作栏

c++ - C++ 类构造中的奇怪错误

java - Class as Hashtable 键——这是个好主意吗?

c++ - 为什么起源进程在 Qt-app 中以僵尸启动。 Linux

c++ - 无法连接到远程ip