Code will not compile for program using class/derived class

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;
}

// 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;
}
[/End My Code]
Last edited on
Please add code tags and post the compiler error messages you're getting, including the corresponding line numbers.
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
[code]Your
code
here[/code]
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.
here is my updated code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdlib>
#include <iostream>
#include <cstring>
#include "time.h"

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;     
};

// 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
Last edited on
You forgot the introductory [code].
I really appreciate all your responses but I don't know what introductory [code] is. Are you referring to a prototype or a consturctor?
No, Athar meant that you forgot the [code] tag that starts the [code] block.

-Albatross
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.

You use them as follows:
[code]code here[/code]

E.g.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cstdlib>
#include <iostream>
#include <cstring>
#include "time.h"


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;
};

// 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.
Last edited on
Topic archived. No new replies allowed.