previous | start | next

8. Pointers and Classes

Pointers can also hold the address of (i.e., point to) a value of class type.

class Car
{
  double fuel;
  double mpg;
public:
  Car();
  Car(double theMpg);

  void addGas(double amt);
  void drive(double amt);
}

int main()
{
  Car myHybrid(30);

  Car *p = &myHybrid;

  myHybrid.addGas(20);

// equivalent to:

  (*p).addGas(20);

  myHybrid.drive(100);

// equivalent to:

  (*p).drive(100);
  ...
}
         


previous | start | next