Question

Code in C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and...

Code in C++

Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and set the base and height of triangle1 and of triangle2, determine which triangle's area is larger, and output that triangle's info, making use of Triangle's relevant member functions.

Ex: If the input is:

3.0 4.0

4.0 5.0

where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is:

Triangle with larger area:

Base: 4.00

Height: 5.00

Area: 10.00

___________________________________________________________________

GIVEN (in main.cpp):

#include
#include "Triangle.h"
using namespace std;

int main(int argc, const char* argv[]) {
Triangle triangle1;
Triangle triangle2;

// TODO: Read and set base and height for triangle1 (use SetBase() and SetHeight())
  
// TODO: Read and set base and height for triangle2 (use SetBase() and SetHeight())

// TODO: Determine larger triangle (use GetArea())
cout << "Triangle with larger area:" << endl;

// TODO: Output larger triangle's info (use PrintInfo())

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Triangle.h

#pragma once
#include<iostream>
#include<iomanip>
using namespace std;

class Triangle
{
private:
   double base, height;
public:
   Triangle();
   Triangle(double, double);
   double GetBase();
   void SetBase(double);
   double GetHeight();
   void SetHeight(double);
   double CalculateArea();
   void PrintInfo();
};

Triangle.cpp

#include "Triangle.h"

Triangle::Triangle()
{
   this->base = this->height = 0.0;
}

Triangle::Triangle(double base, double height)
{
   SetBase(base);
   SetHeight(height);
}

double Triangle::GetBase() { return base; }

void Triangle::SetBase(double base) { this->base = base; }

double Triangle::GetHeight() { return height; }

void Triangle::SetHeight(double height) { this->height = height; }

double Triangle::CalculateArea() { return (0.5 * base * height); }

void Triangle::PrintInfo()
{
   cout << setprecision(2) << fixed;
   cout << "Base: " << base << "\tHeight: " << height << "\tArea: " << CalculateArea() << endl;
}

Main.cpp (Main class)

#include "Triangle.h"

int main()
{
   double base1, height1, base2, height2;
   Triangle triangle1, triangle2;
   cout << "Enter base and height (separated by space) of triangle 1: ";
   cin >> base1 >> height1;
   cout << "Enter base and height (separated by space) of triangle 2: ";
   cin >> base2 >> height2;
  
   triangle1.SetBase(base1);
   triangle1.SetHeight(height1);
   triangle2.SetBase(base2);
   triangle2.SetHeight(height2);

   double area1 = triangle1.CalculateArea();
   double area2 = triangle2.CalculateArea();

   cout << "\nTriangle with larger area:\n";

   if (area1 > area2)
       triangle1.PrintInfo();
   else
       triangle2.PrintInfo();

   return 0;
}

********************************************************* SCREENSHOT *******************************************************

CODE SCREENSHOTS :

1 |{ Main.cpp Triangle.cpp Triangle.h + x 9 TriangleWith LargerArea #pragma once 2 #include<iostream> 3 #include<iomanip> 4 u

(Global Scope) Main.cpp Triangle.cpp + x Triangle.h $ TriangleWithLargerArea #include Triangle.h 2 3 Triangle::Triangle 1 4

Main.cpp - X Triangle.cpp Triangle.h $ TriangleWithLargerArea #include Triangle.h (Global Scope) 1 2 3 int main() 4 { 5 6 7

CONSOLE OUTPUT :

A Microsoft Visual Studio Debug Console Enter base and height (separated by space) of triangle 1: 3.0 4.0 Enter base and heig

Add a comment
Know the answer?
Add Answer to:
Code in C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and...
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
  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • C++ The Shape.hcontains the information of the length of an edge. The derived triangle class has...

    C++ The Shape.hcontains the information of the length of an edge. The derived triangle class has the length of the height of the corresponding edge. Use the two values to compute the area of this triangle in getarea member function.Create the instance and output its result in the main function. Triangle.cpp: #include "triangle.h" Triangle ::Triangle() { //default constructor } float Triangle ::getarea() { //compute the area of the shape } void Triangle ::setheight( float h ) { //set the height...

  • Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the...

    Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the Car class (in files Car hand Car.cpp) with member functions to set and get the purchase price of a car (SetPurchase Price().GetPurchase Price)and to output the car's information (Printinfo). Ex If the input is 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is Car's information Model year: 2011 Purchase...

  • In C programming language: This program will output a right triangle based on user specified height...

    In C programming language: This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or *....

  • In C++ please. 7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in...

    In C++ please. 7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in files Foodltem.h and Fooditem.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. Ex: If the input is: M&M's 10.0 34.0...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • Solve in C++ for Team.h and Team.cpp Given main(). define the Team class (in files Team.h...

    Solve in C++ for Team.h and Team.cpp Given main(). define the Team class (in files Team.h and Team.cpp). For class member function GetWinPercentage, the formula is: teamwins / (teamins + teamLosses) Note: Use casting to prevent integer division Ex: If the input is Ravens 13 3 where Ravens is the team's name, 13 is number of team wins, and 3 is the number of team losses, the output is: Congratulations, Team Ravens has a winning average! If the input is...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • 11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl...

    11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl = oofraction.o0Fraction( 2, 5) f2 = oofraction.o0Fraction ( 1, 3) f3 = f1 + f2 print (str(3)) main() def __init__(self, Num, Den): self.mNum = Num self.mDen = Den return def_add_(self,other): num = self.mNumother.mDen + other.mNum*self.mDen den = self.mDen*other.mDen f = OOFraction(num,den) return f 1. Write the output below. def_str_(self): s = str( self.mNum )+"/" + str( self.mDen) returns 2. Write a class called wholeNum...

  • .Your solution must include header, implementation file, and test files .In C++ write a code to...

    .Your solution must include header, implementation file, and test files .In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius. In a well-designed system, these would be derived from a common class, Figure. You are to...

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