. /Whatever/C++ Classes.../ MESSAGE #90277
kamikazemadman
written by Peterpaul kl h on Nov 22, 2007 10:43
Medeivalstargazer said:
Ok, so I understand C++ and I feel like I can now program successfully, but I'm having trouble grasping one concept: Classes.
Uhm, C++ is classes, when you strip classes, you'll end up (almost) with plain C. Ok, let me see if I can give a short introduction...

Are you familiar with structs? A struct is a concept to bundle multiple variables in one data type. See the following example for a definition of a vehicle. With functions that initialize and move the vehicle.
struct Vehicle {
    float posX, posY; // The position
    float speedX, speedY; // The current speed
};
// function that initializes the vehicle
void initVehicle(struct Vehicle * vehicle, float x, float y, float vx, float vy) {
    vehicle->posX = x;
    vehicle->posY = y;
    vehicle->speedX = vx;
    vehicle->speedY = vy;
}
// function that calculates the new position where the vehicle moved for deltaT seconds.
void moveVehicle(struct Vehicle * vehicle, double deltaT) {
    vehicle->posX += vehicle->speedX * deltaT;
    vehicle->posY += vehicle->speedY * deltaY;
}
// Example calling both functions
struct Vehicle theVehicle;
initVehicle(&theVehicle, 0.0, 0.0, 5.0, 1.0);
moveVehicle(&theVehicle, 1.0);
The above example is plain C, nothing fancy. C++ introduced classes. Think of classes as structs, but with the addition of functions to the datatype. This allows you to call these function (we'll call them methods) in the same manner as you can access the attributes of a class. The following is the example translated to C++:
class Vehicle {
    float posX, posY; // The position
    float speedX, speedY; // The current speed

    // constructor, initializes the object upon creation
    Vehicle(float x, float y, float vx, float vy) {
        posX = x;
        posY = y;
        speedX = vx;
        speedY = vy;
    }
    // function that calculates the new position where the vehicle moved for deltaT seconds.
    void move(double deltaT) {
        posX += speedX * deltaT;
        posY += speedY * deltaY;
    }
};
// Example using both methods
// the following line initializes the object through the constructor.
Vehicle theVehicle = Vehicle(0.0, 0.0, 5.0, 1.0);
theVehicle.move(1.0);
Note that the class methods don't use a pointer to the vehicle anymore, as the method is specific for the vehicle class. Also the class uses a constructor to initialize itself.
Constructors can do all kinds of fancy stuff, but i'll leave that for you to discover.

Now, one of the main advantages of OOP is inheritance. Say you want to implement functionality for a Car, then you can base it on the Vehicle class, so that you can reuse the available source code. I'll only give you an example, I advise you to play with the examples and extend them, print the values to the screen etc, so you become familiar with classes.

class Car : Vehicle {
    float topSpeed;
    // This constructor uses the constructor of Vehicle to initialize the vehicle specific attributes
    Car(float x, float y, float vx, float vy, float speed) : Vehicle(x, y, vx, vy) {
        topSpeed = speed;
        // call accelerate to check if the topspeed is valid
        accelerate(0.0, 0.0);
    }
    void accelerate(float dVx, float dVy) {
        speedX += dVx;
        speedY += dVy;
        float speed = sqrt(speedX * speedX + speedY * speedY);
        if (speed > topSpeed) {
            speedX *= topSpeed / speed;
            speedY *= topSpeed / speed;
        }
    }
};
// Example
Car theCar = Car(0.0, 0.0, 5.0, 1.0, 7.0);
theCar.move(1.0);
theCar.accelerate(1.0, 2.0);
theCar.move(1.0);
reading this thread
(not applicable to single message display)
. /Whatever/C++ Classes.../ MESSAGE #90277
15639, 7 queries, 0.026 s.this frame is part of the AnyNowhere network