Transition between PP and OOP
I am pretty comfortable with functions , loops and decision making
aspects of c++ and have written some very basic programs
using these techniques.
Now i am trying to get the hang of OOP and I am struggling to
make programs which use classes effectively.
I understand constructors, destructors , pointers to objects,
arrays of objects but as yet cant put my knowledge into
decent examples.
1. Does everyone struggle like this at first?
I havent got to sections on inheritance , virtual functions
or class templates.
2. Should i still be able to complete any program i set my mind
without inheritance virtual functions and templates but with
just different program design?
Re: Transition between PP and OOP
Quote:
Originally posted by The Gweech
1. Does everyone struggle like this at first?
I think so. Unless its the first thing you learned. Don't worry too much about that. It'll click.
Quote:
Originally posted by The Gweech
2. Should i still be able to complete any program i set my mind
without inheritance virtual functions and templates but with
just different program design?
well sort of. inheritance is pretty darn good for eliminating duplicate code in similar objects. (some think its bad design to inherit functionality) You don't really need to use templates but there will come a day when you find them useful. Don't worry about them for now. As far as the whole virtual function thing goes. Think about all the situations in C where you might have used a switch of some kind, based on what "type" of thing you were working on. those are where you would use a virtual functions. the cat and dog examples are pretty good for that. dog.Speak() cat.Speak() virtual functions called very simply
animal->Speak();
In C you would have written something like this:
Code:
Speak(animalptr, type)
{
switch(type)
{
case DOG:
-----do dog stuff----
break;
case CAT:
-----do cat stuff----
break;
}
}
So I don't think you should try to get away from virtual function inheritance.