Skip to content

Instantly share code, notes, and snippets.

@kabeer11000
Created January 23, 2024 11:04
Show Gist options
  • Save kabeer11000/c03e1a06c93aec5d9bf2962aba489116 to your computer and use it in GitHub Desktop.
Save kabeer11000/c03e1a06c93aec5d9bf2962aba489116 to your computer and use it in GitHub Desktop.
/**
* Everyone is familiar with a television. It is the object we are going to create in this lab. First we need
a blueprint. All manufacturers have the same basic elements in the televisions they produce as well as
many options. We are going to work with a few basic elements that are common to all televisions. Think
about a television in general. It has a brand name (i.e. it is made by a specific manufacturer). The
television screen has a specific size. It has some basic controls. There is a control to turn the power on
and off. There is a control to change the channel. There is also a control for the volume. At any point in
time, the television’s state can be described by how these controls are set. We will write the television
class. Each object that is created from the television class must be able to hold information about that
instance of a television in fields. So a television object will have the following attributes:
• manufacturer. The manufacturer attribute will hold the brand name. This
cannot change once the television is created, so will be a named constant.
• screenSize. The screenSize attribute will hold the size of the television
screen. This cannot change once the television has been created so will be a
named constant.
• powerOn. The powerOn attribute will hold the value true if the power is on,
and false if the power is off.
• channel. The channel attribute will hold the value of the station that the television
is showing.
• volume. The volume attribute will hold a number value representing the loudness
(0 being no sound).
These attributes become fields in our class.
The television object will also be able to control the state of its attributes. These
controls become methods in our class.
• setChannel. The setChannel method will store the desired station in the
channel field.
• Power. The power method will toggle the power between on and off, changing
the value stored in the powerOn field from true to false or from false to true.
• increaseVolume. The increaseVolume method will increase the value
stored in the volume field by 1.
• decreaseVolume. The decreaseVolume method will decrease the value
stored in the volume field by 1.
• getChannel. The getChannel method will return the value stored in the
channel field.
• getVolume. The getVolume method will return the value stored in the volume
field.
Last updated: 2024-01-23 08:36
**/
#include <iostream>
class Television {
std::string manufacturer;
int screenSize, channel, volume;
bool state = false; /* Powered Off by default */
private:
void __setVolume(int vol) {
this->volume = vol;
}
public:
Television(int screenSize, int channel = 1, int volume = 0) {
this->screenSize = screenSize;
this->channel = channel;
this->volume = volume;
this->manufacturer = "LG";
}
void powerOn() {
this->state = !this->state;
}
void setChannel(int channel) {
this->channel = channel;
}
void Power() {
this->state = state;
}
void increaseVolume() {
this->__setVolume(this->volume + 1);
}
void decreaseVolume() {
this->__setVolume(this->volume - 1);
}
int getVolume() {
return this->volume;
}
int getChannel() {
return this->channel;
}
std::string getManufacturer() {
return this->manufacturer;
}
int getScreenSize() {
return this->screenSize;
}
};
int main() {
Television tv(1920*1080);
std::cout << tv.getManufacturer();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment