-3

I have made a little snippet of what my project is essentially based on - except much bigger. I am having a hard to grasping this particular concept. I need to make a car class which inherits the vehicle class and then there must be separate classes such as window, door, engine etc... I need to access those classes through the car class to adjust its "properties"

#include <iostream>
using namespace std;

class Vehicle
{
    virtual void print() = 0;
};

class Car : public Vehicle
{
    virtual void print();
    Wheel tires[4];
};

class Wheel
{
public:
    int pressure = 0;
    int inflate(int psi)
    {
        pressure = pressure + psi;
    }
};

int main()
{
//What would I have to put here or anywhere else to increase the pressure of a car object's FIRST     tire's pressure - Car car1; car1.tires[0].inflate(10); (this doesn't seem to work)
}

#include <iostream>
#include <vector>
using namespace std;


class Vehicle
{
    virtual void print() = 0;
};

class Car : public Vehicle
{
    virtual void print();
    Car(Wheel wheel[4])
    {

    }
};

class Wheel
{
public:
    int pressure = 0;
    int inflate(int psi)
    {
        pressure = pressure + psi;
    }
};

int main()
{
    Car car1(Wheel wheel[4]);
    //I still don't know what I could add here that would inflate the first tire
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Mike Blair
  • 1,821
  • 2
  • 11
  • 11

4 Answers4

2

The reason it doesn't work is that default access in a class is private (as opposed to structs, where it is public). With

class Vehicle
{
public:
    virtual void print() = 0;
};

// Note that class Wheel has to be defined before class Car, or
// the compiler will complain that class Wheel is unknown here.
class Car : public Vehicle
{
public:
   virtual void print();
   Wheel tires[4];
};

it should be possible to write

int main() {
  Car c;
  c.tires[0].inflate(10);
}

Whether it is a good idea to have public data members is another question (it is not a good idea).

Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • You do not understand how grateful I am at the moment... Thank you so much – Mike Blair Nov 29 '14 at 00:33
  • 1
    @Sallar: the proper way of acknowledging an answer as 'the' answer is to accept it. You skipped the suggestion to take the [short Introductory Tour](http://stackoverflow.com/tour) when signing on, so you may not be aware of that. – Jongware Nov 29 '14 at 00:47
0

Would this work?

class Window
{
 //...
};

class Car
: public Vehicle
{
  std::vector<Windows> car_windows(5);
};

This is showing that a Car is-a Vehicle that contains 5 Windows.

This should be applied similarly for doors, engine, etc.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Wheel tires[4] will not create any Wheel objects, but just an array. That is why accessing it will not give you anything.

You need to have a Car constructor where you create the wheels of the car.

Also, print is a virtual pure function in Vehicle, so you need to implement it somewhere. Can have print() {}; or similar in your header file.

Or you can just use it to test/debug.

crazyGuy
  • 338
  • 2
  • 15
0

I am assuming you have working constructors. Try something like this instead:

car1->tires[0]->inflate(10); 
Dillon
  • 76
  • 7