1

I'm new to programming, I have been reading Sams Teach Yourself C++ in 24 hours. I just started learning classes and am confused on how to allow user input on private data. I created the following class that returns the area of a Trapezoid. Any suggestions would be greatly appreciated.

    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;

}
Xmitten
  • 11
  • 1
  • 1
    *Sams Teach Yourself C++ in 24 hours* That's a false promise. If you can, invest in [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the language properly. – R Sahu Aug 22 '18 at 21:34
  • 1
    In other words i'm wasting my time reading Sams Teach Yourself C++ in 24 hours – Xmitten Aug 22 '18 at 21:37
  • See the section in your book about gathering user input. It will probably be in the I/O section of the text. If they don't cover *that* in their 24 hours, its not worth the paper it's printed on. Regardless, I concur with @RSahu. there are some great books out there. Do some browsing and read some reviews. – WhozCraig Aug 22 '18 at 21:37
  • 1
    You should declare a [constructor](https://en.cppreference.com/w/cpp/language/initializer_list). The first 24h are certainly the 24h where you will learn the most. But 20 years later I am still learning every day! – Oliv Aug 22 '18 at 21:38
  • I just went to a local Barnes and Noble. I definitely will look at the books @RSahu posted. Thank you – Xmitten Aug 22 '18 at 21:39
  • Basically, for changing private data you create setter method, like setHeight(int h) in your case. If you want to change it by user input, you could use additional variable and setter or overload operator >>. – Denis Sablukov Aug 22 '18 at 21:44
  • These "learn X fast" books are usually the worst. You're going to want to take your time to absorb the concepts and have a solid, complete reference book as a foundation for your learning. [*The C++ Programming Language*](http://www.stroustrup.com/4th.html) is really indispensable. The [*Effective C++*](https://www.aristeia.com/books.html) series is worth considering. – tadman Aug 22 '18 at 21:44

1 Answers1

3

You need to read the user's input, and then expose public access to assign new values to the class's private members. For example:

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

Alternatively, use a constructor instead:

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770