Classes and Inheritance

Ok, I have a class project to basically design a program that handles classes and vehicles. What I have so far is far from the ending product. I have made 3 classes, car, vehicle, and motorcycle. Car and motorcycle are dervied from vehicle. The car class has a set const variable for number of wheels to 4. mpg and model name are the other variables. Motorcycle is a class with const for 2 wheels and model and mpg. Vehicle only has number of wheels and mpg.

Here is my code thus far.

Class: car.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef CAR_H
#define CAR_H

#include <string>

#include "vehicle.h"

using std::string;

class Car  : public Vehicle {
public:
  Car();
  Car(string const& model, double milesPerGallon);

  void setModel(string const& model);

  string const& getModel() const;

  virtual void print() const;

private:
  string model;
};

#endif 


motorcycle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef MOTORCYCLE_H
#define MOTORCYCLE_H

#include <string>

#include "vehicle.h"

using std::string;

class MotorCycle : public Vehicle {
public:
  MotorCycle();
  MotorCycle(string const& model, double milesPerGallon);

  void setModel(string const& model);

  string const& getModel() const;

  virtual void print() const;

private:
  string model;
};

#endif 


vehicle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef VEHICLE_H
#define VEHICLE_H

class Vehicle {
public:
  Vehicle();
  Vehicle(int wheels, double milesPerGallon);

  void setWheels(int wheels);
  void setMilesPerGallon(double milesPerGallon);

  double getMilesPerGallon() const;
  int    getWheels() const;

  virtual void print() const;

private:
  int wheels;
  double milesPerGallon;
};

#endif 


car.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "car.h"

#include <iostream>

using namespace std;

Car::Car() {
}

Car::Car(string const& model, double milesPerGallon)
    : Vehicle(4, milesPerGallon), model(model) {
}

void Car::setModel(string const& model) {
  this->model = model;
}

string const& Car::getModel() const {
  return model;
}

void Car::print() const {
  Vehicle::print();
  cout << "Car model: " << model << "\n";
}


motorcycle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "motorcycle.h"

#include <iostream>

using namespace std;

MotorCycle::MotorCycle() {
}

MotorCycle::MotorCycle(string const& model, double milesPerGallon)
    : Vehicle(2, milesPerGallon), model(model) {
}

void MotorCycle::setModel(string const& model) {
  this->model = model;
}

string const& MotorCycle::getModel() const {
  return model;
}

void MotorCycle::print() const {
  Vehicle::print();
  cout << "Motorcycle model: " << model << "\n";
}


vehicle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "vehicle.h"

#include <iostream>

using namespace std;

Vehicle::Vehicle() {
  wheels = 0; milesPerGallon = 0;
}

Vehicle::Vehicle(int wheels, double milesPerGallon)
  : wheels(wheels), milesPerGallon(milesPerGallon) {
}

void Vehicle::setWheels(int wheels) {
  this->wheels = wheels;
}

void Vehicle::setMilesPerGallon(double milesPerGallon) {
  this->milesPerGallon = milesPerGallon;
}

double Vehicle::getMilesPerGallon() const {
  return milesPerGallon;
}

int Vehicle::getWheels() const {
  return wheels;
}

void Vehicle::print() const {
  cout << "Wheels number: " << wheels << "\n"
    << "Miles per gallon: " << milesPerGallon << "\n";
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

#include "car.h"
#include "vehicle.h"
#include "motorcycle.h"

using namespace std;

int main() {
  double mpg; 
  int wheelss;
  Car c("Ford", 8);
  MotorCycle mc("Bike", 5);
  

  
  cout << "Please enter the miles per gallon for vehicle:" << endl;
  cin >> mpg;
  Vehicle milesPerGallon(double mpg);
  cout << "Please enter the number of wheels for the vehicle:" << endl;
   cin >> wheelss;
  Vehicle wheels(int wheelss);

 
  Vehicle.print();
  c.print();
  mc.print();

  return 0;
}


Problem I am having is getting users to input the code instead of having it statically held by the program. So i am attempting this with the vehicle class. I am prompting for mpg and number of wheels. when I compile I get the follow error:
Error 1 error C2143: syntax error : missing ';' before '.' \Desktop\vehicle\main.cpp 25 test15
Error 2 error C2143: syntax error : missing ';' before '.' \Desktop\vehicle\main.cpp 25 test15

I tried googleing the error and found that it typically has something to do with omission in previos lines. I have checked previous lines and have semi colons through out. Any help/guidance would be great.

THank you in advance for your time and effort!

Last edited on
In order to get the previous list of code to compile you have to remove the line

 
Vehicle.print();


Anyways there are lots of misconceptions in your code, this is one of those. You're actually calling the print() which is non-static member of Vehicle without referring to any object. In order to make a call to a non-static member function you always need an object to call that function from, just like you did in:

1
2
 c.print();
 mc.print();


Applying the concept to a real life example, what you did there (Vehicle.print();) is like tying to pump up the tyres (well, this would have been a different function but you got the idea) of a non-existing vehicle, quite of an impossible task, isn't it?


This part is also quite weird:
1
2
3
4
5
6
  cout << "Please enter the miles per gallon for vehicle:" << endl;
  cin >> mpg;
  Vehicle milesPerGallon(double mpg);
  cout << "Please enter the number of wheels for the vehicle:" << endl;
   cin >> wheelss;
  Vehicle wheels(int wheelss);


First of all we have some syntax problems: calling the constructor of a class is like calling any other function, furthermore mpg and wheelss have already been declared, therefore writing Vehicle milesPerGallon(double mpg); or Vehicle wheels(int wheelss); is wrong!
Instead you should have written:

1
2
3
4
5
6
  cout << "Please enter the miles per gallon for vehicle:" << endl;
  cin >> mpg;
  Vehicle milesPerGallon(mpg);
  cout << "Please enter the number of wheels for the vehicle:" << endl;
   cin >> wheelss;
  Vehicle wheels(wheelss);


Anyways, apart from the syntax, there is a logical misconception here: how the heck can milesPerGallon or wheelss be vehicles? Try to think it over, that doesn't make any sense.

Hope it helped out! ;)
Last edited on
Topic archived. No new replies allowed.