Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,920 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,580 people online right now. Registration is fast and FREE... Join Now!




Need some help with basic class construction

 
Reply to this topicStart new topic

Need some help with basic class construction

Outtey22
18 May, 2008 - 01:30 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 27



Thanked: 1 times
My Contributions
When I try to compile, I'm getting all kinds of errors regarding new types in return types. I have no idea of what this means and really need some help.



CODE
#include <iostream>
#include <string>

using namespace std;

class time
{
      public:
             int minute;
             int hour;
             int getHour();
             int getMinute();
             int addHour();
             int addMinute();
             time();
             void printTime();              
}

class extTime : time
{
      public:
             string timeZone;
             string getTimeZone();
             void printTimeZone();
             extTime();
}

int time::getHour()
{
    cout << "Please enter the hour:\n";
    cin >> hour;
    return hour;
}

int time::getMinute()
{
    cout << "Please enter two digits for the minutes:\n";
    cin >> minute;
    return minute;
}

int time::addHour()
{
    hour++;
    return hour;
}

int time::addMinute()
{
    minute++;
    return minute;
}

int time::time()
{
     hour = 0;
     minute = 0;
}

void time::printTime()
{
     cout << "The time is " << hour << ":" << minute << ".";
}

string extTime::getTimeZone()
{
       cout << "Please enter the time zone abreviation: \n";
       cin >> timeZone
       return timeZone;
}

void extTime::printTimeZone()
{
     cout << "The Time Zone is " << timeZone << ".\n";
}

void extTime::extTime()
{
     timeZone = "abc";
}

int main()
{
    time.userTime;    //create user object of time class
    extTime.userExtTime;    //create user object of extTime class
    userTime.time();
    userTime.getHour();
    userTime.getMinute();
    userExtTime.extTime();
    userExtTime.getTimeZone();
    userTime.printTime();
    userExtTime.printTimeZone();
    
    cin.ignore(2);  
    return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Need Some Help With Basic Class Construction
18 May, 2008 - 01:42 PM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,514



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
My bet's on it being something to do with your variables not being declared tongue.gif

You might want to try passing variables to your functions as paramaters, too (or, you could have them as members in your class)

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

skater_00
RE: Need Some Help With Basic Class Construction
18 May, 2008 - 01:44 PM
Post #3

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
Try this:

cpp

#include <iostream>
#include <string>

using namespace std;

class time
{
public:
time();
int getHour();
int getMinute();
int addHour();
int addMinute();
void printTime();

private:
int hour, minute;
};

class extTime
{
public:
extTime();
string getTimeZone();
void printTimeZone();

private:
string timeZone;
};

time::time():hour(0), minute(0)
{

}

int time::getHour()
{
cout << "Please enter the hour: ";
cin >> hour;
return hour;
}

int time::getMinute()
{
cout << endl << "Please enter two digits for the minutes: ";
cin >> minute;
return minute;
}

int time::addHour()
{
++hour;
return hour;
}

int time::addMinute()
{
++minute;
return minute;
}

void time::printTime()
{
cout << endl << "The time is " << hour << ":" << minute << ".";
}

extTime::extTime():timeZone("abc")
{

}

string extTime::getTimeZone()
{
cout << endl << "Please enter the time zone abreviation: ";
cin >> timeZone;
return timeZone;
}

void extTime::printTimeZone()
{
cout << endl << endl << "The time zone is " << timeZone << "." << endl << endl;
}

int main()
{
time * userTime = new time();
extTime * userExtTime = new extTime();
userTime->getHour();
userTime->getMinute();
userExtTime->getTimeZone();
userTime->printTime();
userExtTime->printTimeZone();

system("PAUSE");

delete userTime;
delete userExtTime;

return 0;
}


This post has been edited by skater_00: 18 May, 2008 - 01:48 PM
User is offlineProfile CardPM
+Quote Post

Outtey22
RE: Need Some Help With Basic Class Construction
18 May, 2008 - 01:44 PM
Post #4

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 27



Thanked: 1 times
My Contributions
Wow... I feel like a tool.

Let me tweak it and see where we're at.
User is offlineProfile CardPM
+Quote Post

Outtey22
RE: Need Some Help With Basic Class Construction
18 May, 2008 - 02:22 PM
Post #5

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 27



Thanked: 1 times
My Contributions
Thanks for the help guys!

I tried to use the code that skater posted but I was still getting errors. After thinking about it for a minute, I realized that it was pointless to make a time object since an extTime object would have the inheritance of time. I tweaked it a bit and got it to work. Thanks for the help guys!

CODE

#include <iostream>  
#include <string>  
  
using namespace std;  
  
class time  
{  
public:  
    time();  
    int getHour();  
    int getMinute();  
    int addHour();  
    int addMinute();  
    void printTime();  
  
private:  
    int hour, minute;  
};  
  
class extTime : public time  
{  
public:  
    extTime();  
    string getTimeZone();  
    void printTimeZone();  
  
private:  
    string timeZone;  
};  
  
time::time():hour(0), minute(0)  
{  
  
}  
  
int time::getHour()  
{  
    cout << "Please enter the hour: ";  
    cin >> hour;  
    return hour;  
}  
  
int time::getMinute()  
{  
    cout << endl << "Please enter two digits for the minutes: ";  
    cin >> minute;  
    return minute;  
}  
  
int time::addHour()  
{  
    ++hour;  
    return hour;  
}  
  
int time::addMinute()  
{  
    ++minute;  
    return minute;  
}  
  
void time::printTime()  
{  
     cout << endl << "The time is " << hour << ":" << minute << ".";  
}  
  
extTime::extTime():timeZone("abc")  
{  
  
}  
  
string extTime::getTimeZone()  
{  
       cout << endl << "Please enter the time zone abreviation: ";  
       cin >> timeZone;  
       return timeZone;  
}  
  
void extTime::printTimeZone()  
{  
     cout << endl << endl << "The Time Zone is " << timeZone << "." << endl << endl;  
}  
  
int main()  
{  
    extTime userExtTime;  
    userExtTime.getHour();  
    userExtTime.getMinute();  
    userExtTime.getTimeZone();  
    userExtTime.printTime();  
    userExtTime.printTimeZone();  
      
    system("PAUSE");  
    return 0;  
}  

User is offlineProfile CardPM
+Quote Post

skater_00
RE: Need Some Help With Basic Class Construction
18 May, 2008 - 02:34 PM
Post #6

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
That's good. The code I posted worked just fine here though, but it has to work for you in the end. Feel free to post any questions that are bugging you.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:05AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month