# Newb C++ question about object instantiation

## Recommended Posts

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

##### Share on other sites
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.
//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.

##### Share on other sites

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();

##### Share on other sites
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

##### Share on other sites
thank you very much, you were all a great help.

##### Share on other sites
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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978519

• 10
• 10
• 12
• 22
• 13