This program is in C++: This is the sample output: Length: 20 ft Width: 3.78 in
ID: 3824063 • Letter: T
Question
This program is in C++:
This is the sample output:
Length: 20 ft
Width: 3.78 in
Depth: 12 in
Material: steel
Name:
Length: 40 ft
Width: 4.41 in
Depth: 14 in
Material: Steel
Name: Kevin
Length: 20 ft
Width: 3.78 in
Depth: 12 in
Material: steel
Name:
Length: 40 ft
Width: 4.41 in
Depth: 14 in
Material: Steel
Name: Kevin
Instructions In this assignment, you have to create a class and one object named beam. Beam has multiple properties such as length, width depth, and material. Width is a function of depth with the following relationship: width o. 315 setDepth for assigning the length and You need to write functions named setLength and depth respectively (there must also be functions to return their values. Define getLength and getDepth functions) Material can be either "steel" or "aluminum A new beam must be defined that consists of two original beam. For that purpose, you have to overload the operator to be able to add the beam object to each other, making a new object, then name this new beam as you wish Finally, a function named printstats is required to display the class properties You will add some additional functionality the b to class adding additional functions and/or attributes to the files beam.cpp and beam.h Your program must have or do the following An additional (overloaded) constructor function that takes 2 float parameters of length and depth and sets those instead of the default values. Everything else will be the same as the default case A new function called setDepth (float depth). Be sure to correctly set the width within this function A new operator that takes a string and assigns it to the name of the beam object. This means you also need to add a private data member of type string name to the object. o Modify printStats() to also print the beam name You need to add all three files to a new project named "beam" and upload the project file to the blackboard. Your completed assignment will compile and link with this version of main.cpp to produce the output shown belowExplanation / Answer
//beam.h
#include<iostream>
#include<string>
using namespace std;
class Beam
{
float length, width, depth;
string name;
public:
Beam();
Beam(float l, float d);
void setLength(float l);
void setDepth(float d);
float getLength();
float getDepth();
Beam operator+(Beam &rhs);
friend Beam &operator<<(Beam &out, string &s);
void printStats();
void setWidth();
float getWidth();
void setName(string s);
string getName();
};
-------------------------------------------------------------------
//beam.cpp
#include"beam.h"
using namespace std;
Beam::Beam()
{
length = 0;
width = 0;
depth = 0;
name = "";
}
Beam::Beam(float l, float d)
{
length = l;
depth = d;
width = 0.315*depth;
}
void Beam::setLength(float l)
{
length = l;
}
void Beam::setDepth(float d)
{
depth = d;
}
float Beam::getLength()
{
return length;
}
float Beam::getDepth()
{
return depth;
}
Beam Beam::operator + (Beam &rhs)
{
Beam tmp;
float l, d, w;
l = length + rhs.getLength();
d = depth + rhs.getDepth();
w = width + rhs.getWidth();
tmp.setLength(l);
tmp.setDepth(d);
tmp.setWidth();
return tmp;
}
Beam &operator<<(Beam &out, string &s)
{
out.setName(s);
return out;
}
void Beam::printStats()
{
cout << " Length: " << getLength() << " ft Width: " << getWidth() <<" in Depth: " << getDepth() << " in Material: " << getName() << endl;
}
void Beam::setWidth()
{
width = 0.315*depth;
}
float Beam::getWidth()
{
return width;
}
void Beam::setName(string s)
{
name = s;
}
string Beam::getName()
{
return name;
}
--------------------------------------------------------------------------------------------
//main.cpp
#include"beam.h"
int main()
{
Beam b1, b2,b3;
float l, d;
string s;
cout << "Enter length = ";
cin >> l;
cout << "Enter depth = ";
cin >> d;
cout << "Enter name of beam = ";
cin >> s;
//assign valiues for first beam 1
b1.setLength(l);
b1.setDepth(d);
b1.setWidth();
//set name for beam1
b1<<s;
//input for beam2
cout << "Enter length = ";
cin >> l;
cout << "Enter depth = ";
cin >> d;
cout << "Enter name of beam = ";
cin >> s;
//assign valiues for second beam 2
b2.setLength(l);
b2.setDepth(d);
b2.setWidth();
//set name for beam2
b2 << s;
b1.printStats();
b2.printStats();
b3 = b1 + b2;
b3.setName("Kevin");
cout << "name: "<< b3.getName()<<endl;
}
---------------------------------------------------------------------------------------------------------------------------------------------
//output
Enter length = 20
Enter depth = 12
Enter name of beam = Steel
Enter length = 40
Enter depth = 14
Enter name of beam = Steel
Length: 20 ft
Width: 3.78 in
Depth: 12 in
Material: Steel
Length: 40 ft
Width: 4.41 in
Depth: 14 in
Material: Steel
name: Kevin
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.