
Patrick B. answered 03/23/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef
class Television
{
protected:
int channel;
bool on_off_flag;
public:
Television()
{
channel = 0; on_off_flag = true;
}
int GetChannel() { return(channel); }
int SetChannel(int x) { if (x>=0) { channel=x; } } //can you change the channel if the TV is off
void ChannelUP() { channel++; }
void ChannelDOWN() { if (this->channel) { channel--; } }
On() { on_off_flag = true; }
Off() { on_off_flag = false; }
bool IsOn() { return(on_off_flag==true); }
bool IsOff() { return(on_off_flag==false); }
Television(const Television& tv)
{
this->channel = tv.channel;
this->on_off_flag = tv.on_off_flag;
}
void SerializeToStrCSV(char * csvBuff)
{
sprintf(csvBuff,"%d,%d \n",this->channel,this->on_off_flag);
}
Television(char * csvBuff)
{
this->channel = atoi( strtok(csvBuff,","));
this->on_off_flag = (atoi( strtok(NULL,","))==1);
}
void Dump(char * strMsg=NULL,FILE * fptr=stdout )
{
if (strMsg!=NULL)
{
fprintf(fptr,"***************************************************\n");
fprintf(fptr,strMsg); fprintf(fptr,"\n");
}
fprintf(fptr,"***************************************************\n");
fprintf(fptr," channel = %d : on_off_flag = %d",this->channel, this->on_off_flag);
fprintf(fptr,"\n");
}
} * TTelevision;
#define TV_SIZE (sizeof(Television))
#define TV_BUFF_SIZE (25)
int main()
{
Television tv1;
Television tv2;
char tvCSVbuff[] = "169,1";
Television tv3(tvCSVbuff);
tv1.On();
tv1.SetChannel(27); tv1.Dump();
tv1.SetChannel(21); tv1.Dump();
tv1.SetChannel(33); tv1.Dump();
tv2.On();
tv2.SetChannel(45);
for (int iLoop=0; iLoop<10; iLoop++) { tv2.ChannelUP() ; }
for (int iLoop=0; iLoop<5; iLoop++) { tv2.ChannelDOWN(); }
tv2.Dump((char*)"TV #2"); //channel 50
char tvBuffCSV[TV_BUFF_SIZE];
tv3.SerializeToStrCSV(tvBuffCSV);
printf(tvBuffCSV);
}