learn c++ in 24 hours = really hard
by someone · in Technical Issues · 09/13/2001 (1:55 am) · 4 replies
i've been learning c++ from one of those 'learn c++ in 24 hours' books, i got off to a good start as i flew through the first few chapters and got an okay grip of functions and the basics but now im onto classes and there pissing me off, its as if the author doesnt understand that people reading the book dont already understand c++, so does anyone here know any good c++ resources and tutorals online?thanks.
About the author
#2
anyway, I just wanted to clear up what the "virtual" is for. It only specifies that the desctrutor can be overridden...oversomething...I dunno (however you spell it, sorry) anyway, it can be over"whatever" by classes inheriting info from this class
i.e.
09/22/2001 (9:00 am)
wow... you did a good job of explaining that... I'm amazed someone would take the time to write all that, I know I wouldn't :Panyway, I just wanted to clear up what the "virtual" is for. It only specifies that the desctrutor can be overridden...oversomething...I dunno (however you spell it, sorry) anyway, it can be over"whatever" by classes inheriting info from this class
i.e.
class van : public car
{
//whatever
~car();//err, I'm not sure about this, but on normal
//functions, that's how it would work... and it
//would just over(whatever it is) the old
//function
};
#3
Overridden functions (or "methods" if you prefer) don't need to be protected or virtual. They can be just plain old functions.
Protected things are private, but accessible to derived classes. (As opposed to private, which even derived classes cannot access.)
Destructors should be declared virtual for the case of polymorphism. That is, a pointer of class A pointing to an instance of class B, where B derives from A. Virtual methods work by ensuring that the pointer to A calls methods in B - so a virtual destructor in A ensures that B's destructor is called:
Strictly speaking, B's destructor doesn't need to be virtual in this case. However, it's always safest to declare a destructor virtual, in case it's used as a base class in polymorphism.
I know that all this is slightly advanced for an introduction, and you all have done a good job of explaining things. I just don't want people to get the wrong idea about those little things.
Grant
09/22/2001 (10:52 am)
Just a couple of corrections niggling at me:Overridden functions (or "methods" if you prefer) don't need to be protected or virtual. They can be just plain old functions.
Protected things are private, but accessible to derived classes. (As opposed to private, which even derived classes cannot access.)
Destructors should be declared virtual for the case of polymorphism. That is, a pointer of class A pointing to an instance of class B, where B derives from A. Virtual methods work by ensuring that the pointer to A calls methods in B - so a virtual destructor in A ensures that B's destructor is called:
class A
{
virtual ~A();
};
class B : public A
{
virtual ~B();
}Strictly speaking, B's destructor doesn't need to be virtual in this case. However, it's always safest to declare a destructor virtual, in case it's used as a base class in polymorphism.
I know that all this is slightly advanced for an introduction, and you all have done a good job of explaining things. I just don't want people to get the wrong idea about those little things.
Grant
#4
I always use protected instead of private for stuff I wanna derive, this can get a bit awry when using friend classes. But I hate those and keep away at all costs.
Thanx for the corrections though. ;-)
09/28/2001 (5:27 am)
Cool, I thought anything you want to override later should be virtual or runtime polymorphism goes kinda wierd. Could be wrong though, never tried it otherwise.I always use protected instead of private for stuff I wanna derive, this can get a bit awry when using friend classes. But I hate those and keep away at all costs.
Thanx for the corrections though. ;-)
Torque Owner MK
I find OOP a "beautiful" way of programming. Think in terms of Objects... everyday objects. Make a Class for that "class" of objects. E.g. a car. The Car Class will be used to make cars i.e. car objects.
Objects have properties, e.g. A car has wheels, has a colour etc. In OOP these properties are called members. For example, you might have a 'colour' member variable, which can be a colour. Or a carType member variable which can be something like, "cabriolet", or "coupe" or some other type of car.
These members can be of a private, protected or public scope. private members can only be accessed within the class it self. public ones can be accessed by code outside the actual class. I'll explain 'protected' later, I'll also try to elaborate on the explanations of the other two. In classes, everything by default, is private.
Unless absolutely necessary, Member variables are usually private, and thats how they should be. In order to allow outside code to access these variables, you make Member Functions that allow you to 'get' and 'set' the values of those variables. Obviously these functions should be public. You can also have private functions that are used internally.
Now something totally new, Constructors and Destructors. Constructors are called when an object is created. Destructors, when the object is destroyed. This can be automatically called by the system. They are not explicitly called by your code, but they do influence how you create your objects as you'll see later. They have to be of the same name as the class itself.
Here is a small example class:
class Car // A class called Car { // Anything from here on will automatically be private // as I have explained before. char *Colour; char *Type; int Price; public: // Anything from here on will be public // The following is the constructor. // It takes no parameters. Car() { // The Default values for the variables. Colour = "red"; Type = "Saloon"; price = 10000; } // The Destructor. // Can't remember what the virtual is for. Don't // have any books handy. The ~ identifies it as // the destructor. virtual ~Car() { // you can put clean up code here. // Not necessary for this one. } // Functions to set and get the colour. // void SetColour(const char *xColour) { Colour = xColour; } char *GetColour() { return Colour; } // Make the same for the other variables. };Now, How do we make (declare) objects of that class? Just like you declare any other variable...
void main() { // make an object of class Car... Car myCar(); // Set the car colour myCar.SetColour("green"); }If the constructor of the class needed parameters, they should be added to the line declaring the object. You access the public member functions using the dot(.) notation as shown in the SetColour line.
You can have more than one constructor or even functions that have the same name as each other. They must, however, have a different number of parameters or different types of parameters. This is called overloading.
// The following is the constructor. // It takes no parameters. Car() { // The Default values for the variables. Colour = "red"; Type = "Saloon"; price = 10000; } // The following is the overloaded constructor. // this one takes one parameter. Car(int xPrice) { // The Default values for the variables. Colour = "red"; Type = "Saloon"; Price = xPrice; }To use the overloaded constuctor above, you'll need to specify its price when declaring the object.
Now the 'protected' scope. Classes can be derived to make new classes that inherit all the features of that class. If you want to have some of theses features in a different way, you can override them. The class you are deriving from would be known as the base class. If you want to override a function, it has to be in a protected scope, and be declared as virtual.
protected: virtual DoSomething() { }I'm tired now and don't feel like explaing any more. But this should help you make a start. Sorry, I don't have any links. Feel free to ask questions though.