I am very new to programming. I am having problems getting my code to compile. I thought I followed the samples in the book but obviously not. Is there someone that can helpme figure out my problem.
Problem:Derive the class extClockType from the class clockType by adding a member variable to store the time zone. Add the necessary member funciotns and constructors to make the class functional.Write the definitions of the member funcitons and the constructors. Write a test program to test the class.
[Start My Code]
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
// Base Class
class clockType
{
public:
void getTime(int& hour, int& minute, int& second);
// void printTime() const;
clockType(int myHr=12, int myMin=0, int mySec=0);
private:
int hr;
int min;
int sec;
}
// Derived Class
class extClockType: public clockType
{
public:
void getTimeZone(string&);
extClockType(int hour=12, int min=0, int sec=0, string TZ="CST");
private:
string timeZ;
}
clockType::clockType(int myHr, int myMin, int mySec)
{
hr = myHr;
min = myMin;
sec = mySec;
}
// Defining the functions for the derived class
extClockType::extClockType(int hour, int min, int sec, string TZ)
:clockType(int hour, int min, int sec)
{
timeZ = TZ;
}
I apologize, but I don't know what code tags are? Below are the errors in the Compiler--
44 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp new types may not be defined in a return type
44 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp two or more data types in declaration of `clockType'
44 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp return type specification for constructor invalid
C:\Dev-Cpp\Unit 2 Assign Ch12.cpp In constructor `extClockType::extClockType(int, int, int, std::string)':
52 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp expected primary-expression before "int"
52 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp expected primary-expression before "int"
52 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp expected primary-expression before "int"
C:\Dev-Cpp\Unit 2 Assign Ch12.cpp In function `int main(int, char**)':
66 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp `timeZ' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
C:\Dev-Cpp\Makefile.win [Build Error] ["Unit 2 Assign Ch12.o"] Error 1
Put and around your code or select it and click on the "Source code" button on the right.
And you forgot to terminate your class declarations with a semicolon.
#include <cstdlib>
#include <iostream>
#include <cstring>
#include "time.h"
usingnamespace std;
// Base Class
class clockType
{
public:
void getTime(int& hour, int& minute, int& second);
// void printTime() const;
clockType(int myHr=12, int myMin=0, int mySec=0);
private:
int hr;
int min;
int sec;
};
// Derived Class
class extClockType: public clockType
{
public:
void getTimeZone(string&);
extClockType(int hour=12, int min=0, int sec=0, string TZ="CST");
private:
string timeZ;
};
// Defining the functions in the base class
/*void clockType::printTime()
{
cout << hr << ":" << min << ":" << sec << endl;
}
*/
clockType::clockType(int myHr, int myMin, int mySec)
{
hr = myHr;
min = myMin;
sec = mySec;
};
// Defining the functions for the derived class
extClockType::extClockType(int hour, int min, int sec, string TZ)
:clockType(int hour, int min, int sec)
{
timeZ = TZ;
};
void extClockType::getTimeZone(string& timeZone)
{
timeZone = timeZ;
};
//Main Code
int main(int argc, char *argv[])
{
extClockType myClass(11, 30, 15, "CST");
cout << "The time zone is " << timeZ << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Compile Errors
C:\Dev-Cpp\Unit 2 Assign Ch12.cpp In constructor `extClockType::extClockType(int, int, int, std::string)':
50 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp expected primary-expression before "int"
50 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp expected primary-expression before "int"
50 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp expected primary-expression before "int"
C:\Dev-Cpp\Unit 2 Assign Ch12.cpp In function `int main(int, char**)':
66 C:\Dev-Cpp\Unit 2 Assign Ch12.cpp `timeZ' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
C:\Dev-Cpp\Makefile.win [Build Error] ["Unit 2 Assign Ch12.o"] Error 1
No. These are tags which will show any code between them with syntax highlighting and line numbers.
Whenever you post code you should use them, because otherwise
1) Your code will be unreadable.
1b) Formatting is destroyed.
2) Line numbers aren't visible, so specifying line numbers is useless.
#include <cstdlib>
#include <iostream>
#include <cstring>
#include "time.h"
usingnamespace std;
// Base Class
class clockType
{
public:
void getTime(int& hour, int& minute, int& second);
// void printTime() const;
clockType(int myHr=12, int myMin=0, int mySec=0);
private:
int hr;
int min;
int sec;
};
// Derived Class
class extClockType: public clockType
{
public:
void getTimeZone(string&);
extClockType(int hour=12, int min=0, int sec=0, string TZ="CST");
private:
string timeZ;
};
// Defining the functions in the base class
/*void clockType::printTime()
{
cout << hr << ":" << min << ":" << sec << endl;
}
*/
clockType::clockType(int myHr, int myMin, int mySec)
{
hr = myHr;
min = myMin;
sec = mySec;
};
// Defining the functions for the derived class
extClockType::extClockType(int hour, int min, int sec, string TZ)
:clockType(int hour, int min, int sec)
{
timeZ = TZ;
};
void extClockType::getTimeZone(string& timeZone)
{
timeZone = timeZ;
};
//Main Code
int main(int argc, char *argv[])
{
extClockType myClass(11, 30, 15, "CST");
cout << "The time zone is " << timeZ << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
My apologies. Now I see why I was supposed to include the tags. I updated the code, recompiled it, and included the new compile errors in my post above.