Sign in to follow this  

2 questions

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

x += k is an abreviation for x = x + k. In other words say you want Xpos = Xpos + 1. You could also say that as Xpos += 1. Just saves typing space

Virtual functions are a more complicated topic. Say you have a class named vehicle. It has methods (functions in the class) such as drive, and turn. Then lets say you have two more classes, truck and car which inherit from vehicle (if you don't know inheritance yet this is to advacned of a subject by the way). Anyway, truck and car automatically get the methods drive and turn. But lets say you want the truck to have a different drive method then car. You would need to declare the drive method in vehicle as virtual. This means you can overwrite it in inherited classes. That is the basics of virtual functions, there is lots more you can do with it that I'm sure others will tell you about.

class Vehicle{
public:
virtual void Drive();
void Turn();
};

class Truck: public Vehicle{
void Drive(); //Overwrites cechile's drive method
};

Look at some good C++ tutorials for more information I think the following sites might have stuff:
http://cplus.about.com
http://www.cprogramming.com
http://www.cpp-home.com/

EDIT:
Created spelling of vehicle :)

[Edited by - Squirell on September 14, 2004 10:25:33 AM]

Share this post


Link to post
Share on other sites
x += 1 is the functional equivalent of x = x + 1 for builtins and proper class types. Other operators use the same notation; operators *=, /=, -= exist.

Virtual functions are functions that are possible to redefine in sub classes. They, nor inheritence, will not play an important role in any amateur projects.

Share this post


Link to post
Share on other sites
Quote:
Original post by Squirell
x += k is an abreviation for x = x + k. In other words say you want Xpos = Xpos + 1. You could also say that as Xpos += 1. Just saves typing space


For built-in types, this is the behaviour, and it is the intended behaviour for operator += when defined for other classes, but of course you *can* write it to do anything you like.

In the general case, using += lets you do things more efficiently, since you avoid having to create a new object with addition and then copying it back over the original variable. Again the compiler can optimize this for built-ins so that it becomes the same code anyway, but for other classes this isn't really possible:


class OmgLeetHaxor {
int data;
public:
OmgLeetHaxor & operator+ (int stuffToAdd) {
// I will pwn the n00bs by ignoring their stuffz0rz. lolololololol!
return new OmgLeetHaxor(42);
// omg, I will probably leak their memory too. w00t.
}
OmgLeetHaxor & operator= (OmgLeetHaxor& rhs) {
this.data = rhs.data * 2; // o_O
return *this;
}
void operator+= (int stuffToAdd) {
data += stuffToAdd; // wtf, too boring :(
data ^= 251254325; // kekekekekekeke
}
}



You see, since the compiler can't prevent you from doing stupid things, it can't make assumptions about how it's allowed to optimize things. AFAIK, anyway.

But that's all pedantic, really. Typing "foo += 42" is quick and C++-idiomatic, in much the same way as "++foo" is.

Quote:
Virtual functions are a more complicated topic. Say you have a class named vechile.


I would rather say I have a class named vehicle. :) Otherwise, good summary. Actually, you can override non-virtual methods too, but because you didn't request dynamic method lookup, you get static method lookup:


class Foo {
public:
void wibble() { cout << "FOO!" << endl; }
}

class Bar : public Foo {
public:
void wibble() { cout << "BAR!" << endl; }
}

Foo x = new Bar();
x.wibble(); // FOO! static linkage, and the variable is type Foo.
Bar y = new Bar();
y.wibble(); // BAR! because the Bar-ness of y is known at compile time.
// You could also cast x in a variety of ways, but it's a bad idea generally speaking.



Chris: Bah. Better to get them started on it early, I say. If you don't learn how to think in higher-level concepts, you'll spend all your time fighting the compiler and counting *'s and &'s.

Share this post


Link to post
Share on other sites

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this