Question

In: Computer Science

7.24 LAB: Triangle area comparison (classes) Language: C++ Given class Triangle (in files Triangle.h and Triangle.cpp),...

7.24 LAB: Triangle area comparison (classes)

Language: 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

Solutions

Expert Solution

Program

/*** Triangle.h ***/

//Triangle.h
#ifndef TRI_H

#define TRI_H

//Triangle class declaration
class Triangle
{
private:
//private data members
float base;
float height;
public:
//constructor
Triangle(float b=0,float h=0);
//accessors functions
float getBase();
float getHeight();
//mutators functions
void setBase(float b);
void setheight(float h);
//calculate area
float getArea();
//print the triangle
void print();
};

#endif //TRI_H

/*** Triangle.cpp ***/

//Triangle.cpp
#include<iostream>
#include<iomanip>
#include "Triangle.h"
using namespace std;

//parameterized constructor
Triangle::Triangle(float b,float h)
{
base = b;
height = h;
}

/* accessors functions */

//function to return base
float Triangle::getBase()
{
return base;
}
//function to return height
float Triangle::getHeight()
{
return height;
}

/*mutators functions*/

//function to set base
void Triangle::setBase(float b)
{
base = b;
}
//function to set height
void Triangle::setheight(float h)
{
height = h;
}


//calculate area
float Triangle::getArea()
{
return (0.5*base*height);
}

//print the triangle
void Triangle::print()
{
cout<<fixed<<setprecision(2);
cout<<"Base: " <<base<<endl;
cout<<"Height: "<<height<<endl;
cout<<"Area: "<<getArea()<<endl;
}

/*** main.cpp ***/

#include <iostream>
#include"triangle.h"

using namespace std;

//main function
int main()
{
Triangle t1, t2;
float base, height;
cout<<"Enter base: ";
cin >>base;
cout<<"Enter height: ";
cin >>height;
t1.setBase(base);
t1.setheight(height);

cout<<"Enter base: ";
cin >>base;
cout<<"Enter height: ";
cin >>height;
t2.setBase(base);
t2.setheight(height);

cout<<"Triangle with larger area:"<<endl;
if(t1.getArea()>t2.getArea())
t1.print();
else
t2.print();

return 0;
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

7.28 LAB: Artwork label (classes/constructors). Written in C++ Given main(), complete the Artist class (in files...
7.28 LAB: Artwork label (classes/constructors). Written in C++ Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and...
7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp)...
7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.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 2.0 1.0 where M&M's...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp)...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors...
C programming language only! a file is given that has comma-separated integers. the files contents are...
C programming language only! a file is given that has comma-separated integers. the files contents are -1,-9,1,45,3,2,1,-1... Create a function that takes as an input a filename and returns an array containing the list of integers in the file
5.11 LAB: Drawing a right triangle c++ This program will output a right triangle based on...
5.11 LAB: Drawing a right triangle c++ 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...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation. Each polygon should have a function area() that returns its...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the formula is: team_wins / (team_wins + team_losses) Note: Use floating-point division. Ex: If the input is: Ravens 13 3 where Ravens is the team's name, 13 is the 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 Angels 80 82, the output is: Team Angels has a...
in c++: The area of an arbitrary triangle can be computed using the formula where a,...
in c++: The area of an arbitrary triangle can be computed using the formula where a, b, and c are the lengths of the sides, and s is the semiperimeter : s = (a + b + c) / 2 1. (5 points) Write a void function that uses five parameters : three value parameters that provide the lengths of the edges and two reference parameters that compute the area and perimeter(not the semiperimeter). Make your function robust.Note that not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT