. /../C++ Classes.../ 1
the bestest ever
written by Medeivalstargazer on Nov 21, 2007 17:42
Ok, so I understand C++ and I feel like I can now program successfully, but I'm having trouble grasping one concept: Classes. I've seen them many times in source codes for programs and read about them in tutorials, but I'm having trouble understanding them or how to use them... Could someone explain it to me so that a monkey would understand? Thanks in advance
who needs titles?
written by Pomelos on Nov 21, 2007 19:53
Go to Wikipedia's entry on Object-oriented programming. Read everything. Click on every useful link. Read said links. Then come back.
"gheeh!" (c)h.azuma
written by Yayo on Nov 22, 2007 01:37
hoho! The OOP mess!
good luck! : P

y.
a title is a curious thing...
written by Granpire viking man on Nov 22, 2007 02:55
C++ for Dummies. Nuff' said.
written by Barebones on Nov 22, 2007 09:15
You could google for: C++ class tutorial, and pick a tutorial that feels nice to follow for you. In my opinion, this is a matter of personal taste.

For example, here is a tutorial that explains you the basics in the first screenful, but as you scroll down it gets messy.

Bear in mind that, after grasping the concept and writing a few programs, you won't really know yet. Pomelos pointed you to learning about OOP (and, I would add, Design Patterns), because classes look like something pretty artificial until you learn to design with them in mind. When the phrase "programming in layers of code" changes into "programming to interfaces", then things begin to get much more organized.

By the way, to the admins/mods: /me would think that this should belomg to Member projects, for example. Or even Linoleum, notwithstanding the small technical issue of Lino being in the antipodes. I mean, recently we don't hear in Whatever about anything but programming this, my Linux that. This forums used to center around more useless (thus interesting) things than geeky penis comparisons.
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);
meep
written by Naavis on Nov 22, 2007 12:51
Peterpaul to the rescue!
the bestest ever
written by Medeivalstargazer on Nov 22, 2007 23:48
Peterpaul kl h said:
...
Thank you! I think I'm understanding now I'll mess around with it tonight (tis thanksgiving now though )
reading this thread
no members are reading this thread
. /../C++ Classes.../ 1
29389, 12 queries, 0.060 s.this frame is part of the AnyNowhere network