The Fan Class

2 minute read

The code has been modified slightly and now uploaded to the site.

A UML diagram is not provided.

Question

<— Return Home

92.cpp

#include <iostream>
#include "Fan92.h"
using namespace std;

int main() {
  Fan92 fan(3, 10, true);
  Fan92 fan2(2, 5, false);

  std::cout << fan.getState() << "\n";
  std::cout << fan.getRadius() << "\n";
  std::cout << fan.getSpeed() << "\n";

  std::cout << fan2.getState() << "\n";
  std::cout << fan2.getRadius() << "\n";
  std::cout << fan2.getSpeed() << "\n";
} 

<— Return Home

Fan92.h

#ifndef Fan92_H
#define Fan92_H

class Fan92{
  public:
    Fan92();
    Fan92(double s, double r, bool o);
    bool getState();
    double getRadius();
    int getSpeed();

  private:
    bool on;
    double radius;
    int speed;
};

#endif

<— Return Home

Fan92.cpp

#include "Fan92.h"
#include <cmath>

Fan92::Fan92(){
  speed = 1;
  on = false;
  radius = 5;
}

Fan92::Fan92(double s, double r, bool o){
  speed = s;
  radius = r;
  on = o;
}

bool Fan92::getState(){
  return on;
}

double Fan92::getRadius(){
  return radius;
}

int Fan92::getSpeed(){
  return speed;
}

<— Return Home

Here is a 2nd version of the same question.

92V2.cpp

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

int main(){
    Fan f1;
    std::cout << f1;
    std::cout << f1.getSpeed() << std::endl;
    std::cout << f1.getOn() << std::endl;
    std::cout << f1.getRadius() << std::endl;
    std::cout << "-------------------------" << std::endl;
    Fan f2(3, true, 10);
    std::cout << f2;
    std::cout << f2.getSpeed() << std::endl;
    std::cout << f2.getOn() << std::endl;
    std::cout << f2.getRadius() << std::endl;
    std::cout << "-------------------------" << std::endl;
    Fan f3(2, false, 5);
    std::cout << f3;
    std::cout << f3.getSpeed() << std::endl;
    std::cout << f3.getOn() << std::endl;
    std::cout << f3.getRadius() << std::endl;
    return 0;
}

<— Return Home

Fan92V2.h

#ifndef FAN92V2_H
#define FAN92V2_H

#include <iostream>
class Fan{
    public:
        Fan();
        Fan(int spe, bool fanSwitch, double rad);

        int getSpeed();

        bool getOn();

        double getRadius();

        void setSpeed(int spe);

        void setOn(bool fanSwitch);

        void setRadius(double rad);

        friend std::ostream & operator << (std::ostream & out, Fan & f);
    private:
        int speed;
        bool on;
        double radius;

};

std::ostream & operator << (std::ostream & out, Fan & f){
    out << "Speed: " << f.speed << "\nOn: " << f.on << "\nRadius: " << f.radius << std::endl;
    return out;
}

void Fan::setRadius(double rad){
    radius = rad;
}

void Fan::setOn(bool fanSwitch){
    on = fanSwitch;
}

void Fan::setSpeed(int spe){
    if (spe > 3 || spe < 0){
        std::cout << "Invalid Speed!" << std::endl;
        std::exit(1);
    }
    speed = spe;
}

double Fan::getRadius(){
    return radius;
}

bool Fan::getOn(){
    return on;
}

int Fan::getSpeed(){
    return speed;
}

Fan::Fan(int spe, bool fanSwitch, double rad){
    speed = spe;
    on = fanSwitch;
    radius = rad;
}

Fan::Fan(){
    speed = 1;
    on = false;
    radius = 5;
}

#endif

<— Return Home