gpt4 book ai didi

c++ - 从创建的类中获取用户输入

转载 作者:行者123 更新时间:2023-11-30 02:18:36 25 4
gpt4 key购买 nike

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

    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;

}

最佳答案

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

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;
}

关于c++ - 从创建的类中获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51975420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com