Question

I need to write a class in C++. I need to make a class that uses...

I need to write a class in C++.

I need to make a class that uses measurements of a triangle and outputs its area.

It needs to have the following things.

variables the hold the base and height, and those should be doubles for precision.

Member functions to get and set the variables from part one and that check to ensure the values entered are greater than zero. If the values are incorrect, the function must prompt the user for a new value and keep doing so until a valid value is given.

I need another member function that returns the area of the triangle.

There also needs to be a non-member function, main to run the program.

The output should be like this:

What is the height of the triangle in meters? -1

That is an invalid height, please try again. 1

What is the length of the base in meters? 1

The area of the triangle is 0.5 m^2

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

class triangle {
private:
    double base, height;
public:
    double getBase() const {
        return base;
    }

    void setBase(double base) {
        while (base <= 0) {
            cout << "That is an invalid length, please try again. ";
            cin >> base;
        }
        this->base = base;
    }

    double getHeight() const {
        return height;
    }

    void setHeight(double height) {
        while (height <= 0) {
            cout << "That is an invalid height, please try again. ";
            cin >> height;
        }
        this->height = height;
    }

    double area() {
        return 0.5 * base * height;
    }
};

int main() {
    triangle t;
    double b, h;
    cout << "What is the height of the triangle in meters? ";
    cin >> h;
    t.setHeight(h);
    cout << "What is the length of the base in meters? ";
    cin >> b;
    t.setBase(b);
    cout << "The area of the triangle is " << t.area() << " m^2" << endl;
    return 0;
}

What is the height of the triangle in meters? That is an height, What is the length of the base in meters? The area of the triangle is 0.5 m^2 3 nvalid height, Please Ey agai 5

Add a comment
Know the answer?
Add Answer to:
I need to write a class in C++. I need to make a class that uses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • c++ I need help! create Student.h In this class, you are provided with a class skeleton...

    c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • I need help with the C++ for the following problem: Write a program uses objected oriented...

    I need help with the C++ for the following problem: Write a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(),...

  • Menu-driven programs will display the menu options to the user and then prompt them for a...

    Menu-driven programs will display the menu options to the user and then prompt them for a menu choice. This program will display the following menu in the following format: Calculator Options: Calculate the area of a circle Calculate the area of a rectangle Calculate the area of a triangle Calculate the area of a trapezoid Calculate the area of a sphere Exit Enter your choice (1-6) Once the user enters a choice for the menu, the program should use a...

  • C++ Assignment 4 - Robot Speed Estimator Be sure to read through Chapter 7 Structured Data...

    C++ Assignment 4 - Robot Speed Estimator Be sure to read through Chapter 7 Structured Data and Classes before starting this assignment. Your job is to write a program to estimate the speed of a robot. Your program will use a class called Robot to represent a robot. To keep things simple, this class will focus only on one aspect of a Robot - the maximum speed at which it can move. Background The speed of the robot is largely...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • Hi I need help doing this problem *The program must re-prompt as long as invalid input...

    Hi I need help doing this problem *The program must re-prompt as long as invalid input is inserted Implement a program that manages shapes. Implement a class named Shape with a virtual function area()which returns the double value. Implement three derived classes named Diamond, Oval, and Pentagon. Declare necessary properties in each including getter and setter function and a constructor that sets the values of these properties. Override the area() function in each by calculating the area using the defined...

  • Java Please - Design and implement a class Triangle. A constructor should accept the lengths of...

    Java Please - Design and implement a class Triangle. A constructor should accept the lengths of a triangle’s 3 sides (as integers) and verify that the sum of any 2 sides is greater than the 3rd(i.e., that the 3 sides satisfy the triangle inequality). The constructor should mark the triangle as valid or invalid; do not throw an exception. Provide get and set methods for the 3 sides, and recheck for validity in the set methods. Provide a toString method...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT