首页 > 解决方案 > 从创建的类中获取用户输入

问题描述

我是编程新手,我在 24 小时内一直在阅读 Sams Teach Yourself C++。我刚开始学习课程,对如何允许用户输入私人数据感到困惑。我创建了以下返回梯形面积的类。任何建议将不胜感激。

    class Trapezoid
{
    //assigns a number to a variable
private:
    int a = 20;
    int b = 25;
    int height = 30;
    int area;
public:
    int getArea();
};

int Trapezoid::getArea()
{
    // calculates the area and returns it.
    area = (a + b) / 2 + height;
    return area;
}

#include "AreaTrapezoid.hpp"
#include <iostream>

int main()
{
    // accesses area inside the Trapeziod class.
    Trapezoid areaT;
    areaT.getArea();


    // displays the area result
    std::cout << "The area of a Trapezoid: " << areaT.getArea() << std::endl; 
    std::cout << system("pause"); 
    return 0;

}

标签: c++

解决方案


您需要阅读用户的输入,然后公开公共访问权限以将新值分配给类的私有成员。例如:

class Trapezoid
{
    //assigns a number to a variable
private:
    int a = 20;
    int b = 25;
    int height = 30;
public:
    int getArea();

    void setA(int value);
    void setB(int value);
    void setHeight(int value);
};

int Trapezoid::getArea()
{
    // calculates the area and returns it.
    return (a + b) / 2 + height;
}

void Trapezoid::setA(int value)
{
    a = value;
}

void Trapezoid::setB(int value);
{
    b = value;
}

void Trapezoid::setHeight(int value)
{
    height = value;
}

#include <iostream>
#include <limits>
#include <cstdlib>
#include "AreaTrapezoid.hpp"

int main()
{
    Trapezoid areaT;
    int value;

    // get the user's input and apply it to the Trapeziod.

    std::cout << "Enter A: ";
    std::cin >> value;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    areaT.setA(value);

    std::cout << "Enter B: ";
    std::cin >> value;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    areaT.setB(value);

    std::cout << "Enter Height: ";
    std::cin >> value;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    areaT.setHeight(value);

    // displays the area result

    std::cout << "The area of the Trapezoid: " << areaT.getArea() << std::endl; 
    std::system("pause"); 

    return 0;
}

或者,改用构造函数:

class Trapezoid
{
    //assigns a number to a variable
private:
    int a;
    int b;
    int height;
public:
    Trapezoid(int a, int b, int height);

    int getArea();
};

Trapezoid::Trapezoid(int a, int b, int height)
    : a(a), b(b), height(height)
{
}

int Trapezoid::getArea()
{
    // calculates the area and returns it.
    return (a + b) / 2 + height;
}

#include <iostream>
#include <limits>
#include <cstdlib>
#include "AreaTrapezoid.hpp"

int main()
{
    int a, b, h;

    // get the user's input and apply it to the Trapeziod.

    std::cout << "Enter A: ";
    std::cin >> a;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::cout << "Enter B: ";
    std::cin >> b;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    areaT.setB(value);

    std::cout << "Enter Height: ";
    std::cin >> h;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // displays the area result

    Trapezoid areaT(a, b, h);
    std::cout << "The area of the Trapezoid: " << areaT.getArea() << std::endl; 
    std::system("pause"); 

    return 0;
}

推荐阅读