This C++ class is a good demonstration of Object Orientation in code.
// definition file for class Radio
#ifndef RADIO_H
#define RADIO_H
#include <iostream>
using namespace std;
const bool FM = true;
const bool AM = false;
const bool ON = true;
const bool OFF = false;
const int MIN_VOLUME = 0;
const int MAX_VOLUME = 11; // it goes to eleven!
class Radio
{
public:
// constructors:
Radio();
Radio(bool b,bool p,double fm,double am,int v);
// accessors:
bool getBand();
bool getPower();
double getFMStation();
double getAMStation();
int getVolume();
// mutators:
bool setBand(bool b);
bool setPower(bool p);
bool setFMStation(double s);
bool setAMStation(double s);
bool setVolume(int v);
// utility functions:
void displayRadio();
private:
bool band; // FM = true, AM = false
bool power; // on = true, off = false
double FMstation , AMstation ; // as per norms
int volume; // 0..11 is the valid range
};
#endif
// Implementation file for class Radio
#include <iostream>
#include "Radio.h"
using namespace std;
// constructors:
Radio::Radio()
{
band = FM;
volume = 4;
power = OFF;
FMstation = 88.1;
AMstation = 530;
}
bool Radio::setFMStation(double s)
{
if((s < 88.1) || (s > 107.9)) // Station's input out of range
return false;
if((int(s * 10) % 2) == 0) // station's .x is even and therefore invalid
return false;
FMstation = s;
return true;
}
bool Radio::setAMStation(double s)
{
if((s < 530) || (s > 1600))
return false;
if((int(s) % 10) != 0) // AMstation not divisible by 10 and therefore invalid
return false;
AMstation = s;
return true;
}
// utility functions:
void Radio::displayRadio()
{
cout << "Band = " <<; ((band == FM) ? "FM":"AM") << endl;
cout << "Power = " << ((power == ON) ? "ON":"OFF") << endl;
cout << "FM Station = " << FMstation << endl;
cout << "AM Station = " << AMstation << endl;
cout << "Volume = " << volume << endl << endl;
}
// mutators:
bool Radio::setBand(bool b)
{
band = b;
return true;
}
bool Radio::setPower(bool p)
{
power = p;
return true;
}
bool Radio::setVolume(int v)
{
if((v < MIN_VOLUME) || (v > MAX_VOLUME))
return false;
volume = v;
return true;
}
//custom constructor , placed later on purpose.
Radio::Radio(bool b,bool p,double fm,double am,int v) // build a radio with custom defaults
{
band = FM;
setBand(b);
power = OFF;
setPower(p);
FMstation = 88.1;
setFMStation(fm);
AMstation = 530;
setAMStation(am);
volume = 4;
setVolume(v);
}
// accessors:
bool Radio::getBand()
{
return band;
}
bool Radio::getPower()
{
return power;
}
double Radio::getFMStation()
{
return FMstation;
}
double Radio::getAMStation()
{
return AMstation;
}
int Radio::getVolume()
{
return volume;
}
// Driver file for class Radio
#include <iostream>
#include "Radio.h"
using namespace std;
//Radio object driver, this file sends the commands that "push the radio buttons" via function calls.
void main()
{
Radio myJamBox; // Build a radio to be referred to as 'myJamBox'.
myJamBox.displayRadio(); // display myJamBox's current attributes.
myJamBox.setAMStation(550); // adjust's myJamBox's AM station to 530
myJamBox.setPower(ON); // flips myJamBox's power swith ON
myJamBox.displayRadio(); // display myJamBox's current attributes.
system("pause");
}