Design a class named fan to represent a fan. The class contains: An int data fie
ID: 3694186 • Letter: D
Question
Design a class named fan to represent a fan. The class contains: An int data field named speed that specifies the speed of the fan. A has three speeds indicated with a value 1, 2, or 3. A bool data field named on that specifies whether the fan is on. A double data field named radius that specifies the fan is on. A no-arg constructor that creates a default fan with speed 1. on false, and radius 5. The accessor and mutator functions for all the data fields. Draw the UML diagram for the class. Implement the class. Write a test program that creates two Fan objects. Assign speed 3, radius 10, and turn it on to the first object. Assign speed 2,radius 5, and turn it off to the second object. Invoke their accessor functions to display the fan.properties.Explanation / Answer
#include <iostream>
using namespace std;
class Fan
{
int speed;
bool on;
double radius;
public:
Fan()
{
speed=1;
on=false;
radius=5;
}
int getspeed()
{
return speed;
}
bool geton()
{
return on;
}
double getradius()
{
return radius;
}
void setspeed(int s)
{
speed=s;
}
void seton(bool t)
{
on=t;
}
void setradius(double r)
{
radius =r;
}
};
int main()
{
Fan f1;
Fan f2;
f1.setspeed(3);
f1.setradius(10);
f1.seton(true);
f2.setspeed(2);
f2.setradius(5);
f2.seton(false);
cout<<"The speed of fan 1 is "<<f1.getspeed()<<endl;
cout<<"The radius of fan 1 is "<<f1.getradius()<<endl;
cout<<"Is fan 1 on: "<<f1.geton()<<endl;
cout<<endl;
cout<<"The speed of fan 2 is "<<f2.getspeed()<<endl;
cout<<"The radius of fan 2 is "<<f2.getradius()<<endl;
cout<<"Is fan 2 on: "<<f2.geton()<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.