I recently started learning OOP. Forgive me if this is a noob question. My question is, I have thought new keyword is used with only same datatypes such as:
char* p = new char; // OR
int* myArr = new int[i] //etc...
While studying inheritance and virtual functions I've come across this:
#include <iostream>
using namespace std;
class Human {
public:
virtual void className() {
cout << "Human" << endl;
}
};
class Asian : public Human {
public:
void className() {
cout << "Asian" << endl;
}
};
int main() {
Human* h1 = new Asian();
h1->className();
}
In the main function, we initialize the pointer with the base class and then there is the derived class after new keyword? What do those 2 datatypes represent, how am I supposed to use them?
CodePudding user response:
This is the advantage of using OOP. Suppose you have a car factory and you're making sedan, hatchback and etc. All of them are cars, so they have four wheels, engines, and so on. In order to don't have the same data many times, you can use inheritance. Both the parent and the child class can cast to each other but an example for you to understand is that suppose you have a class that you modify a car. As I didn't specify the type of car, it can be a sedan or a hatchback, instead of having 2 different functions you can have one function that takes the parent object(Car).
virtual
keyword specifies that the child version of the function should run instead of the parent. So in your example, since we are making an Asian
object, we will call the child(Asian) version of the className
. If there was no virtual
keyword then the parent version would run.
CodePudding user response:
Note that in your case, the class Human
is accessible class of Asian
and
If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class (4.10, 4.11). [ Note: it follows that members and friends of a class X can implicitly convert an X* to a pointer to a private or protected immediate base class of X. —end note ]
According to the above quoted statement, you can convert a pointer to the derived class Asian
to a pointer to the base class Human
(which means from Asian*
to Human*
), which is what you did when you wrote:
Human* h1 = new Asian();
In the above statement, on the left hand side we have a pointer named h1
of type Human
and on the right hand side by using the keyword new
we are creating an object of type Asian
on the heap. But note that using new
has a second effect which is that after the object of type Asian
is created on the heap then a pointer to that object is returned. So essentially,
on the right hand side we get a pointer to Asian
(Asian*).
Now as i have already explained(quoted) at the beginning of the answer, you can convert a pointer to Asian
to a pointer to Human
which is what has happened here.
Now when you wrote:
h1->className();
There are 3 important facts that you have to consider:
className
is a virtual function/methodh1
is a pointerAsian
inheritHuman
publicly
and according to virtual function documentation
a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.
And so the result is that the derive class' function named className
is called at run-time and we see Asian
printed on the console.