Okay, and so i got two classes.

class a{
public:
a(){};
void print(){cout << "hello"};
}

class b : public a{
public:
void print(){cout << "hello world";}
}

Along with a variety of parents having a child

a blah[10];
blah[5] = b();

Than I call print, and would like it to say hello world.

blah[5].print();

However it calls parents. How do you fix this?

This is often fixed by proclaiming the function virtual, a la:

class a{
public:
    virtual void print(){
        cout << "hello";
    }
}

class b : public a{
public:
    virtual void print() {
        cout << "hello world";
    }
}

This is the way one implements polymorphism in C++. More here: http://en.wikipedia.org/wiki/Virtual_function

However, it ought to be noted that inside your example, it'll never call the kid function, since you are utilizing object values, not pointers/references to things. To treat this,

a * blah[10];
blah[5] = new b();

Then:

blah[5]->print();

What you are searching for operates-time polymorphism, meaning to achieve the object take "great shapeInch (i.e. a or b), and act accordingly, because the program runs. In C++, you need to do this by looking into making the function virtual within the base class a:

virtual void print() {cout << "hello"}; 

Then, you have to keep elements by pointer or reference, and - as with general the derived classes can introduce new data people and want more storage - it's normal to produce the objects around the heap with new:

a* blah[10];
blah[5] = new b();

You'll be able to call:

blah[5]->print();  

And it'll call the b implementation of print().

You need to later delete blah[5] (and then any other peoples you've pointed at memory came back by new).

Used, it's wise to utilize a container that may remove the objects it consists of when it's itself destructed, whether because of departing scope or just being erased. std::vector<> is a such container. You may also use wise-pointers to automate the deletion from the a and b objects. This helps to make the code correct if exceptions are tossed before your delete claims execute, and also you would like your program to help keep running without seeping memory. The boost library may be the simplest/best spot to obtain a wise pointer implementation from. Together:

#include <vector>
#include <boost/shared_ptr.hpp>

std::vector<boost::shared_pointer<a> > blah(10);

blah[5] = new b();

(It's more normal to make use of vectors with push_back(), because it instantly develops the vector to suit in most the sun and rain you've added, using the new total available by calling vector::size().)

It will that since you told the compiler that the instance was of type a. It's in a wide array of a objects, right? Therefore it is of type a!

Obviously, you would like the technique in b to overwrite the main one in a, despite getting a reference from the parent type. You will get that behavior while using virutal keyword on function declaration within the parent class.

virtual void print(){cout << "hello"};

Why do you use it like this?

Because whenever you cast your resist parents class, you introduced an ambiguity. If this object's print() is known as, how can we address it? It's of type b, however the reference is of type a, therefore the sorrounding code may expect it some thing like a, not b!

To disambiguate, the virtual keyword is introduced. virtual functions will always be overridden, when the object is of the child class that contains a technique with similar signature.

Cheers!

In a you have to declare print as virtual.