The \"solution\" to the question \"Chapter 2, Problem 1PE\" for \"Data Structure
ID: 3785382 • Letter: T
Question
The "solution" to the question "Chapter 2, Problem 1PE" for "Data Structures Using C++ 2nd edition by D.S. Malik" is incomplete and no matter what I try I cannot get the program, as written, to execute. Step three is missing, I think that is the problem. please help.
***
Chapter 2, Problem 1PE:
In Chapter 1, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockType by adding a data member to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write a test program to test your class.
Malik, D. S.. Data Structures Using C++ (Page 124). Cengage Textbook.
***
Explanation / Answer
I am not sure of how the class was defined in the textbook you have mentioned.
The below code does exactly as the problem description explains.
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
// Base Class
class clockType{
private:
int hr;
int min;
int sec;
public:
// constructor of class.
clockType(int myHr=12, int myMin=0, int mySec=0);
// return hour
int getHr();
// return minute
int getMin();
// return second
int getSec();
};
// Derived Class
class extClockType: public clockType
{
private:
string timeZ;
public:
extClockType(int hour=12, int min=0, int sec=0, string TZ="CST");
void setTimeZone(string&);
void displayTime();
};
// base class constructor definition
clockType::clockType(int myHr, int myMin, int mySec)
{
hr = myHr;
min = myMin;
sec = mySec;
};
int clockType::getHr(){
return hr;
}
int clockType::getMin(){
return min;
}
int clockType::getSec(){
return sec;
}
// Defining the functions for the derived class
extClockType::extClockType(int hour, int min, int sec, string TZ)
:clockType(hour, min, sec)
{
timeZ = TZ;
};
// explicitly set the time zone.
void extClockType::setTimeZone(string& timeZone)
{
timeZone = timeZ;
};
// displayTime shows the entire time.
void extClockType::displayTime()
{
cout << getHr() << ":" << getMin() << ":" << getSec() << endl;
cout << "Timezone is: " << timeZ << endl;
};
//Main Code
int main(int argc, char *argv[])
{
// create an instance of extClockType and pass all the parameters.
extClockType myClass(11, 30, 15, "CST");
// displayTime with timeZone
cout << "The time is ";
myClass.displayTime();
return 0;
}
OUTPUT:
$ g++ file.cpp
$ ./a.out
The time is 11:30:15
Timezone is: CST
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.