从创建的类中获取用户输入 我是新手,我在24小时内一直在阅读SAMS自学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; } 类梯形 { //为变量指定一个数字 私人: INTA=20; int b=25; 整数高度=30; 内部区域; 公众: int getArea(); }; int梯形::getArea() { //计算面积并返回它。 面积=(a+b)/2+高度; 返回区; } #包括“梯形区域.hpp” #包括 int main() { //访问梯形类内的区域。 梯形区; getArea(); //显示区域结果 std::cout

从创建的类中获取用户输入 我是新手,我在24小时内一直在阅读SAMS自学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; } 类梯形 { //为变量指定一个数字 私人: INTA=20; int b=25; 整数高度=30; 内部区域; 公众: int getArea(); }; int梯形::getArea() { //计算面积并返回它。 面积=(a+b)/2+高度; 返回区; } #包括“梯形区域.hpp” #包括 int main() { //访问梯形类内的区域。 梯形区; getArea(); //显示区域结果 std::cout,c++,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 setHeig

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

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

#包括
#包括
#包括
#包括“梯形区域.hpp”
int main()
{
int a,b,h;
//获取用户的输入并将其应用于梯形图。
std::cout>a;
std::cin.ignore(std::numeric_limits::max(),'\n');
std::cout>b;
std::cin.ignore(std::numeric_limits::max(),'\n');
收缩面积(值);
std::cout>h;
std::cin.ignore(std::numeric_limits::max(),'\n');
//显示区域结果
梯形区域(a、b、h);

STD:在24小时内自学C++,这是一个错误的承诺。如果你能,投资和学习正确的语言。换句话说,我在浪费时间阅读SAMS自学C++在24小时内看到你的书中关于收集用户输入的部分。它可能在文本的I/O部分。如果他们不在24中覆盖这个部分。几个小时,它不值得印刷在纸上。不管怎样,我同意@RSahu。那里有一些很棒的书。浏览一下,读一些评论。你应该申报a。第一个24小时当然是你学习最多的24小时。但是20年后,我仍然每天都在学习!我刚刚去了当地的巴恩斯和诺布尔。我定义了它ely将查看@RSahu发布的书籍。谢谢
#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;
}