473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

c++- Inheritence Help

I'm having some trouble using Inheritence classes in c++. I have my base class (Time.h) and my derived class is called (MilTime.h). I started writing my MilTime.cpp for the function definitions. Now, here is when I run into problems. I have a function in MilTime.h known as void setHours . Now when I try to type in the definition of the function, I do it like this:

void MilTime::setHours()

However, even though I am including MilTime.h, I get an error that says 'MilTime' is not a class name or definition name.

So my first question is..How do I define the functions of derived classes? What is the syntax? Or do I need to include the Time.h and Time.cpp in my MilTime.cpp?

Also...when I get to my main program...and how you usually declare a class object like this:
Time object;

Do I also have to do the same for the derived class like this:
MilTime object2;

Or does it get included with Time? What is the syantax for that? what do I have to do?
Dec 15 '07 #1
6 2327
If you are declaring your MilTime functions without including the MilTime header it wont work. MilTime.cpp needs to see MilTime.h or it won't - as it says - know that MilTime is a class. The same goes for anywhere else you need to use your MilTime.

To create a MilTime object you can use the same syntax as for Time.
Expand|Select|Wrap|Line Numbers
  1. MilTime x;
Time x wont create a MilTime though, but:
Expand|Select|Wrap|Line Numbers
  1. Time* x = new MilTime(); // whether x behaves as Time or Miltime depends on the functions called are virtual or not
  2. delete x;
could be used. Try googling 'polymorphism c++' as well.
Dec 15 '07 #2
I do have MilTime.h included, but yet it is still saying the same error. Same for if I also included Time.h and Time.cpp.
Dec 15 '07 #3
So I defined all the MilTime functions in the MilTime.h file, and it did not have any errors.

Now when I get to my main fine (I'm doing a visual C++ gui, so it's called Form1.h)...If I include MilTime.h, 37 errors pop up. If I declare an object like you said:
MilTime x;
That accounts for 3 more errors.

So...What the hell do I do?
Dec 15 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
First, there is already a time.h that is part of C. I wouldn't use that name.

Second, let's assume you out your base class declaration in a MilTimeBase.h.

Third, you #include your MilTimeBase.h in your MilTime.h or you include them separately in each source file. Whichever way is dictated by your coding preferences. Just be sure the the MilTimeBase.h is included before the derived class declaration.

If this does not work then you have other problems and will need to post your code.
Dec 16 '07 #5
Here is my code:
This is Time.h

Expand|Select|Wrap|Line Numbers
  1. // Specification file for the Time class
  2.  
  3. #ifndef TIME_H
  4.  
  5. #define TIME_H
  6.  
  7.  
  8.  
  9. class Time
  10.  
  11. {
  12. private:
  13.     int milHour;
  14.     int milMin;
  15.     int milSec;
  16. protected:
  17.  
  18.    int hour;
  19.  
  20.    int min;
  21.  
  22.    int sec;
  23.    char AMPM;
  24.  
  25. public:
  26.  
  27.    // Default constructor
  28.  
  29.    Time() 
  30.  
  31.       { hour = 0; min = 0; sec = 0; }
  32.  
  33.  
  34.  
  35.    // Constructor
  36.  
  37.    Time(int h, int m, int s) 
  38.  
  39.       { hour = h; min = m; sec = s; }
  40.  
  41.  
  42.  
  43.    // Accessor functions
  44.  
  45.    int getHour() const
  46.  
  47.       { return hour; }
  48.  
  49.  
  50.  
  51.    int getMin() const
  52.  
  53.       { return min; }
  54.  
  55.  
  56.  
  57.    int getSec() const
  58.  
  59.       { return sec; }
  60.  
  61.  
  62.  
  63.   // Set functions          
  64.  
  65.    void setHour(int h); 
  66.  
  67.       //{ hour = h; }
  68.  
  69.  
  70.  
  71.    void setMin(int m);
  72.  
  73.       //{ min = m; }
  74.  
  75.  
  76.  
  77.    void setSec(int s); 
  78.  
  79.      // { sec = s; }
  80.  
  81.  
  82.  
  83. };
  84.  
  85. #endif
  86.  
  87.  
This is Time.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "Stdafx.h"
  2. #include <math.h>
  3. #include "Time.h"
  4. using namespace System;
  5. void Time::setHour(int h)
  6. {
  7.     hour=h;
  8.     milHour=h;
  9.     if (milHour>12 && milHour<24)
  10.     {
  11.     milHour=milHour-12;
  12.     hour=milHour;
  13.     }
  14.  
  15. }
  16. void Time::setMin(int m)
  17. {
  18.     min=m;
  19.     milMin=min;
  20.     if (milMin<59)
  21.         min=milMin;
  22. }
  23. void Time::setSec(int s)
  24. {
  25.     sec=s;
  26.     milSec=sec;
  27.     if (milSec<59)
  28.         sec=milSec;
  29. }
  30.  
  31.  
MilTime.h:
Expand|Select|Wrap|Line Numbers
  1. #include "Time.h"
  2. #include "Time.cpp"
  3. class MilTime : public Time
  4. {
  5. protected:
  6.     int milHours;
  7.     int milMinutes;
  8.     int milSeconds;
  9.     int milHundred;
  10. public:
  11.     MilTime (int, int, int);
  12.     void SetMilTime(int, int);
  13.     int getHours {return hour;}
  14.     int getMinutes {return min;}
  15.     int getSeconds {return seconds;}
  16.     void SetHours(int hour)
  17.     {
  18.         milHours=hour;
  19.         if (AMPM=P)
  20.         {
  21.             milHours=(milHours+12);
  22.         }
  23.     }
  24.     void SetMinutes(int min);
  25.     {
  26.         if (min<59)
  27.             milMinutes=min;
  28.     }
  29.     void SetSeconds(int sec);
  30.     {
  31.         if (sec<59)
  32.             milSeconds=sec;
  33.     }
  34.     void SetMilTime(int hour, int min)
  35.     {
  36.         milHours=hour;
  37.         if (AMPM=P)
  38.         {
  39.             milHours=(milHours+12);
  40.         }
  41.         milHours=(milHours*100);
  42.         milMinutes=min;
  43.         milHundred=(milHours+milMinutes);
  44.     }
  45.  
  46.  
  47.  
  48. };
  49.  
And this is my main (Form1.h cause I am making a GUI)
Expand|Select|Wrap|Line Numbers
  1. #include "Time.h"
  2. #include "MilTime.h"
  3. #include "Time.cpp"
  4.     Time convert;
  5.     MilTime x;
  6.  
  7.  
The rest in Form1.h is just GUI stuff.
Taking out MilTinme.h and Miltime x out of my Form1 makes this run completely error free. If left in..40 errors.
Dec 17 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
#include "Time.h"
#include "Time.cpp" <<<<<<<<<<<<<<<<<<<<<<
class MilTime : public Time
{
You never include source files!

When you do you get a copy of the source file everytime you include it and this leads to redefinition errors in the linker. Like below:

#include "Time.h"
#include "MilTime.h"
#include "Time.cpp" <<<<<<<<<<<<<<<<<<<<<<<<<<<<,
Time convert;
MilTime x;
You have two copies of Time.cpp in your build.

Header files are never to contain code of any kind that generates memory usage. Header files are for declarations only.

In the above example, it's OK to include a header file inside a herader file but the Time.cpp needs to be added as a file in your project and not as an include.

I think your errors will go away when you fix this.
Dec 17 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
8
by: Digital Puer | last post by:
I made the following table to help me (re)learn inheritence basics. Can someone check if it's correct? This table requires a courier-like font. Java C++ ---- ...
0
by: Jan Elbæk | last post by:
Hi, I would like to make a base form in my project - which (almost) all forms must inherit from. The baseform must have some visible elements (a toolbar, a topaligned panel and a picturebox and...
7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
3
by: Mark Turner | last post by:
Help! I want to serialize mu C# objects uzing the xmlserializer class. It works well whem all my classes are flat, but when I use inheritence to split the data and implementation I get an...
2
by: bmsgharr | last post by:
On a web site that I am working on I have created a base page called public abstract class WebsiteBasePage : System.Web.UI.Page { ..... } The reason for this is to put standard code in it, for...
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
9
by: Chrissy | last post by:
I took a C# class as an elective and received an incomplete in it and am desparate for help. I have two assignments left (arrays and inheritance) and would gladly pay anyone that can assist me with...
11
by: Wayne Pedersen | last post by:
Need some help - and I may be doing this wrong, so please correct and suggest! I'm learning the MVP method, which I seem to have a good grasp of. Now I am trying something a bit more advanced. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.