Newb C++ question about object instantiation
Hi,
Im working on a program in c++ that is just a basic group of classes that i need to work with. I am a decent java programmer but im having a bit of difficulty converting to c++. here is what i need to do:
i have 3 classes:
class car in file car.cpp
class track in file track.cpp
class ford in file ford.cpp
Now, i want to create a car object in the track class file. (in java this would be something like: car myCar; or car myCar = new car();)
in c++ i believe it is the same: car myCar(); but i get errors when i compile saying that the car class isnt an identifier, and it defaults it to int, or something like that. I thought maybe i have to do an #include of the car class but i dont think i should have to right???
the second question i have is that the class ford implements class car:
class ford : public car
{
}
is that correct? I can get that to work(when i only compile car and ford), however when i compile the whole project(car, track, and ford) i get an error that the methods in the car class are being redefined.
I can find a lot of examples on instantiation however they all have something where the car class declaration is in the same file as the ford class declaration. is there a way i can do it in separate files???
Thank you very much for your help,
cp51
For separate files, many people declare the class and members in a header file, and define the functions in a cpp file, which includes the class header file.
and then the cpp file for the ford class, etc.
Your inheritance for the ford class is correct also, just need a semi-colon after the second curly bracket.
//car.h//this prevents the header from being included multiple times#ifndef CAR_H#define CAR_Hclass car{ public: car(); ~car(); void drive(); float mpg;};#endif
//car.cpp#include "car.h"car::car(){ cout << "car" << endl;}car::~car(){}void car::drive(){ cout << "vroom" << endl;}
//ford.h#include "car.h"class ford : public car{ public: ford(); ~ford(); void breakDown();};
and then the cpp file for the ford class, etc.
Your inheritance for the ford class is correct also, just need a semi-colon after the second curly bracket.
For most of your questions, you should probably read this article: Organization Code Files, and then ask if you still have any confusions.
However, it doesn't explain one thing, and this is one of C++'s ugly warts. This:
Doesn't create a car named myCar. It declares a function that return a car that takes no arguments called myCar. Basically anything that can be interpreted as a function declaration is interpreted as a function declaration in C++. It's something of a pain. To declare a car named myCar, leave off the parenthesis.
However, since you were talking about inheritance, you probably want a pointer.
However, it doesn't explain one thing, and this is one of C++'s ugly warts. This:
car myCar();
Doesn't create a car named myCar. It declares a function that return a car that takes no arguments called myCar. Basically anything that can be interpreted as a function declaration is interpreted as a function declaration in C++. It's something of a pain. To declare a car named myCar, leave off the parenthesis.
car myCar;
However, since you were talking about inheritance, you probably want a pointer.
car * myCar = new car(); // or new ford();
Are you using include guards? If one files includes another one twice, errors like this might occur. To prevent this, the following file template is usually used :
#ifndef FILENAME_H_INCLUDED
#define FILENAME_H_INCLUDED
#include "whateveryouneedtoinclude.h"
class YourClass
{
};
#endif
This prevents the file from being included twice
#ifndef FILENAME_H_INCLUDED
#define FILENAME_H_INCLUDED
#include "whateveryouneedtoinclude.h"
class YourClass
{
};
#endif
This prevents the file from being included twice
Something else to note is that public inheritance almost always denotes an "is-a" relationship in C++ i.e. you're designing a hierarchy for run-time polymorphism. If this is the case, you'll need to use virtual functions and make the destructor virtual, too.
Edd
Edd
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement