You're in luck. I'm actually learning all this C++ stuff in my sophomore year at college, and it's somewhat of a difference from Java.
Now, Sida pretty much explained constructors, destructors and copy-constructors, etc....just ONE note on them: in a constructor,
it's good practice to pass a reference to an object instead of the object itself. You want to minimize as many operations as possible. C++ is a very efficient language, but it relies heavily on the programmer. Now, you may say: goddamn it, but I don't get pointers and references and stuff.
Well, I'm going to be brutally honest with you -
you HAVE to understand pointers to code efficiently, not only in C++, but in C or any pointed based language. Java is easy, you have the virtual machine do everything and we don't use pointers there. In C++, MEMORY allocation is super important. That's why we use pointers.
For example, it's much more cost-effective (we say it like this in programming "syntax", it means less operations) to pass a reference to an object than copy it to memory and then assign it to ANOTHER variable in memory. It's a waste, you use too much temporary memory. You might say "oh but my programs are light". Yeah, but imagine one day, you're building a heavy or critical app and it's crucial to minimize the processing time. You will carry on bad habits, and it's never good.
A reference is like saying "that object is THERE" instead of grabbing it, copying it to temporary memory and making a copy. The reference goes DIRECTLY to the spot in memory where that object is located and copies it. This is very important in functions because you don't need to mess around with temporary objects.
Say, for example, you have a class Place. A place has a name, right? So, what you'd do is, for example:
class Place {
protected:
string name; // place name
public:
Place();
Place(const string &p);
Place(const Place &p);
~Place();
};
Place::Place(){
this->name = NULL;
}
Place::Place(const string &p){ // ATTRIBUTE CONSTRUCTOR
this->name = p.name;
}
Place::Place(const Place &p){ // COPY CONSTRUCTOR!
this->name = p.name;
}
You may have noticed a
const keyword before the object type. That means the argument I pass to the constructor is read-only, I can't modify it. In this case, we're only going to copy the attributes of p, we're not going to ALTER p, so it makes no sense to make it modifiable. Again, optional, but these are good practices.
And remember pointers:
int x = 1;
int *xp;
xp = &x; // this passes the REFERENCE of x.
cout << xp; // this prints the memory address of x, imagine 0xFF00C0A (memory addresses are in hex value)
cout << *xp; // this prints out the CONTENT of x at memory position pointed by xp, which is 1.
-----------------
Now, dynamic memory. Imagine I have space for 10 cars in a parking lot. And I'm a happy camper because I'm the security guard (just for fun haha) and I say "no, no, only 10 cars enter at one time". And then my crappy boss decides to be a bitch and say "Now you have to let 20 cars in". So what do I do? In real life...well, I'd expand the parking lot with loads of money and stuff. In programming it's easier. Say you have a Car array and an integer that tells you how many cars are in the parking lot:
Car cars[10];
int numberofCars = 0;
So what do I have to do? I let 10 cars in and increment my numberofCars variable. Then when it's full (the var is 10), I create
ANOTHER array of double the size (it's good practice to do this in C++ - 10, 20, 40, 80, and so on), COPY the Cars that are in the array to the new one, and then I still have 10 spaces left. Simple huh? If only life was this easy!
You could also use a linked list, stack, queue, basically any type of data structure in C++, but those are a little more complex because you work almost exclusively with pointers. With arrays it's easier, but you have to have a function that does the size verifying and stuff when you insert new things there.
As for operator overloading...well, that's basically redefining operators (like =, +, -, ++, --, << (stream out)) to doing what YOU want them to do for your objects.
Let's take the attribute operator (which is =).
Now, in standard C++, you can do this natively (include <iostream> and 'using namespace std;' in your headers):
string c = "Hello World";
string IS a native data type of C++ (BUT not C), so you can attribute directly. Why? Because in the <iostream> library there is an '=' overload that lets you do that.
However, when YOU create your objects you have to do this manually because the compiler can't guess. Sida pretty much explained that part.
That should cover most of it. If you need any future help, post here or PM me, I'll be happy to help!
Edited by Lovato, 24 October 2011 - 01:05 PM.