I'm trying to understand the basics of how Object Oriented Programming works with C++. I'm not really trying to learn the language per-say (although I might be interested in learning it gradually). I brought two books on C++ and read through them once, and while some things seem to be easy to understand, setting of variable types, creating statements and function ex. when the books start getting into Object Oriented details it becomes a little confusing. I want to see if my basic understand of C++ if correct:

You basically start with Classes. A simple class contains variables and functions that allow the class to specialize in a specific operation. A class also allows only those variables and functions (i guess you can call them inputs and outputs in a sense) that let it interact with other data to be public. Other variables and functions that are just the inner workings of the Class are keep private so that other classes and data can't mess around with the Class' operation. For example you have a Class called "Fish". This class contains all the functions and variables that it needs to simulates any type of fish in 3-D. Certain functions and variables, like how the geometry is built and rendered in 3-D, is kept private, because it not important for the outside world to know. The only thing the outside world needs to know is those variables that make a fish a certain type, like "number of fins, weight of body, carnivore or vegetarian, color and so on.

Next comes Objects. Objects are a variable with a class type. For example, you just finished making your "Fish" class and now you want to use it to make a specific type of fish. You want to make a "Gold Fish", this Gold Fish references the fish class with it set of values. For instance a gold fish can have fish values of ""number of fins = 3, weight of body = 1.5, vegetarian, Color = Gold and so on. These values are sort of sent through the Fish Class' functions, and the Fish Class creates the Gold Fish based on those values. More Objects give more fish and different values for each object gives me different breeds of fish.

Now on to Inherence. Inherence allows you to assemble a bunch of objects together to form a bigger picture. For instance you want to make a 3-D fish again, but you want this fish to have behavior and movement. There are a lot of different aspects in a fish's behavior and movements that vary heavily from fish to fish. So rather then make one huge class, you break up the fish into a number of specialized Classes. Lets say you make one class that specializes in how the fins are shaped and how they behave. Another class that looks at how the mouth of the fish is shaped and how that behaves, another class that specializes in the eyes, another for drawing the geometry in 3-D...and so on. Now that you have all you different classes, you want to make objects (that contain the values for the type of fish you want to simulate) of those classes and assemble it. To allow different objects to combined you need to let higher objects pass (or inherent) their functions to lower objects. For instance you have this fish, and every part of the fish needs to be drawn out as geometry. The "Draw Geometry" Object needs to pass its capabilities to all the different fish parts. The Draw Geometry acts as "the parent object" and it passes it attributes to its "child objects" which are the mouth, fins, eyes. Above this parent object there might be another parent object "XYZ". This object tells the location of where we want our fish to be in 3-D. Since all the parts need to move together this peace needs to pass its function to every object. You can say that this "XYZ" object is the parent of the "Draw Geometry" object and the grandparent of the "mouth, fins and eyes" objects.

After Inherence comes polymorphism (the one I totally don't understand). How it works is a mystery to me, and I hear that there are eight different types of polymorphism. From my understanding, polymorphism is when you pass different types of value types between objects, and the object changes the type passed to it or the object receiving the values adapts to the data type being sent to it. Illustrating the idea:

Float Dog.Bark ----> Int Soud.Speak (Float Sound.Speak)

Here, we have a Dog Object with a "Bark" variable. The Bark variable is of type Float. The output of this variable is sent to the input of the Object Sound. Speak. The "speak variable is of type Int. The Sound Object decides to adapt the using floating point numbers so that it can use the outputs of Float Dog.Bark

For some reason this doesn't sound correct to me. My other idea of how it works is that an objects function is override by another objects function. But i can't understand how that would work.


My last thing is on what is a C++ (or sometime there called API) library. Is it just a bunch of class made by different programmers so that other programmers can use them in their code?

I think what you may be looking for is more general computer science theory than anything specific to C++ (especially if you have no interest in learning the language at this time). You should google around for OO design theory, since it applies to many other languages, not just C++ (in fact, if you are looking to learn a language purely for its OO features, you could choose to learn Smalltalk or Python instead).

Polymorphism in C++ might sound a little odd to you. it doesn't work on objects as such, but with pointers-to-objects (and if you don't know what a pointer is, then this explanation maybe won't be much help to you)

When a pointer is created, it's given a "pointed to" type. eg, for a non-polymorphic example, if a pointer has an int as its "pointed to" type, then you may point it to any object within your program which is of the type int.

If, on the other hand, the"pointed to" type is a class, then it may point to any object within your program which is of that class.
here's where polymorphism can happen, because the pointer may also point to objects derived from that class.

Let me reiterate - OO Polymorphism in C++ is not about changing objects, it's about changing pointers. You cannot change the data type of variables within an object. and you cannot even change an object from one derived type to another (Not safely anyway.. it's possible to use some really ugly hacks to do this, but you'll probably end up breaking something pretty badly. so just don't do it).

You're probably wondering exactly what use that is to anyone, if you cant' even change the object.. Well.. it allows a programmer to create a common link (an interface) between objects which would otherwise be unrelated..

here's an analogy.. (I know.. Sorry! I hate analogies too..). Imagine you learn to drive in a Ford Fiesta 1.0 Litre. Your driving instructor tells you that for that specific car-object, using your foot against the 2nd pedal will be invoking the brake() function for that car-object.

but what if you pass your test and buy a Lamborghini Diablo to whizz about in? You knew which function to use for braking in your fiesta.. but now you're driving a different car-object.

Luckily, your middle pedal does exactly the same for every car in the world. in fact, the whole driving interface is the same, even though your car objects are of a completely different type

Chances are, the mechanics of the car objects are rather different under the hood, but you don't care about that, all you care about is that your interface behaves in the way which you expect, that you always have a brake() function, and that invoking this function always stops the car.

What you have, as the driver, is a polymorphic interface to every car you could want to drive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.