Question: I'm currently learning about inheritance, and I'm using SublimeText3 as an editor. I've created a simple inheritance. Vehicle as a superclass, and Car as a

I'm currently learning about inheritance, and I'm using SublimeText3 as an editor. I've created a simple inheritance. Vehicle as a superclass, and Car as a subclass. I only have one method, which is located in my Vehicle superclass that prints out how many wheels the car has. The terminal keeps giving me this error provided below. Also, below the error, I provide my main.cpp, Vehicle.cpp, Vehicle.h, Car.cpp, Car.h, and Makefile. Can anyone fix this? I'm getting really frustrated, I've been trying to make this work for a whole day, trying out a lot of experiments. I do have a hunch that I'm not including the right header files.

ERROR:

g++ -c -Werror main.cpp

g++ -c -Werror Car.cpp

Car.cpp:5:6: error: redefinition of 'Car'

Car::Car() : Vehicle(4) {

^

./Car.h:9:5: note: previous definition is here

Car() : Vehicle(4) {};

^

1 error generated.

Here are my codes

main.cpp

#include #include "Vehicle.h" #include "Car.h"

int main(int argc, char** argv){ Car c1; Vehicle& v1 = c1; Vehicle* v2 = new Car(); // print how many wheels the car has v1.speak(); return 0; } // end of main

Vehicle.cpp

#include #include "Vehicle.h"

Vehicle::Vehicle(int numwheels) { nwheels = numwheels; } // end of Vehicle contructor

// prints out how many wheels the car has void Vehicle::speak() const throw() { printf("The car has %d wheels ", nwheels); } // end of constructor

Vehicle.h

#ifndef VEHICLE_H #define VEHICLE_H #include

class Vehicle { int nwheels; public: Vehicle(int numwheels); void speak() const throw(); }; // end of Vehicle class

#endif

Car.cpp

#include #include "Car.h"

// constructor Car::Car() : Vehicle(4) { } // end of constructor

Car.h

#ifndef CAR_H #define CAR_H #include #include "Vehicle.h"

class Car : public Vehicle {

public: Car() : Vehicle(4) {}; }; // end of Car class

#endif

Makefile

all:main

main.o: main.cpp Vehicle.h Car.h g++ -c -Werror main.cpp

Vehicle.o: Vehicle.cpp Vehicle.h g++ -c -Werror Vehicle.cpp

Car.o: Car.cpp Car.h g++ -c -Werror Car.cpp

main: main.o Vehicle.o Car.o g++ -o main main.o Vehicle.o Car.o

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!