5. Write a C++ program that implements an oval class. The program should output
ID: 3735857 • Letter: 5
Question
5. Write a C++ program that implements an oval class. The program should output the width, length and area of the oval. The user should let the user enter the values from the keyboard to pass to the constructor. Use UML class diagram provided. Place all classes into one file. Output should be user friendly. Oval -length: double -width: double +Oval(double, double) +setLength(double): void +getLength(): double +setWidth(double): void +getWidth(): double +area(): double Name the program: TestovalxX.cpp, where XX are your initialsExplanation / Answer
#include <iostream>
using namespace std;
// class oval defination
class Oval
{
private:
/// length and width are private member so they cannot access out of the class
double length;
double width;
public:
//constructor which is tacking double double as parameter
Oval(double x, double y)
{
length = x,width = y;
}
// function to set length given by user
void setLength(double l)
{
std::cout<<"setting length = "<<l<<endl;
length = l;
}
// function to set width given by user
void setWidth(double w)
{
std::cout<<"setting width = "<<w<<endl;
width = w;
}
// function to return length given by user
double getLength(void)
{
return length;
}
// function to return width given by user
double getWidth(void)
{
return width;
}
/*
function to find the area of Oval
area of the oval is 3.14* half_length * half_width
returning answer of the calculation
*/
double area(void)
{
// calculation obal_area = pai * half_length * half_width
return ((3.14 * (length/2)*(width/2)));
}
};
int main()
{
double length,width;
cout<<"Enter length and width ";
// getting user input
cin>>length>>width;
//creating class object with parameter given by user
Oval obj(length,width);
//finding ares by area() method
cout<<"area of oval is "<<obj.area()<<endl;
// Demo of setWidth() and setLength() function use case
obj.setWidth(20);
obj.setLength(20);
//finding ares by area() method
cout<<"area of oval is "<<obj.area()<<endl;
return 0;
}
/*
OUTPUT:
Enter length and width
100 100
area of oval is 7850
setting width = 20
setting length = 20
area of oval is 314
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.