Question about Polymorphism.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Question about Polymorphism.

Postby corey__cakes on Sun May 11, 2014 5:04 pm

I've recently learned about polymorphism in C++. I get the jest of virtual members, abstract classes, and pointers to a base class. What I don't understand is the purpose of have a pointer to a base class. The resources I've read on polymorphism have all shown that you have an object from a derived class and a pointer of a base class pointing to that object, then calling some function from the pointer. What do I gain from doing that? I thought the point of inheritance was so I could call those same functions from an object of the derived class.

//basic includes and stuff
using namespace std;

//Base class
class Base {
int x, y; //variables and stuff in the Base class
void SetXandY(int a, int b) { //generic function
x = a;
y = b;
int GetX() { return x; } //accessor functions
int GetY() { return y; }

//Derived Class
class Derived : public Base { //Derived class inherits from Base class
int w, h; //other members you'd add in derived classes
void SetWandH(int a, int b) { //generic derived class function
w = a;
h = b;
int GetW() { return w; } //accessor functions
int GetH() { return h; }

//main function
int main() {

Derived obj; //declares an object from the derived class
Base* objpointer = &obj;
/* We can do this because the object of the Derived class
is inheriting from the Base Class. */
//now we can do this
objpointer->SetXandY(5, 20); //this will make the x and y values for obj 5 and 20 respectively

//shows us the data
cout << obj.GetX() << endl;
cout << obj.GetY() << endl;

//holds the screen

//end of main
return 0;

This works and I know why it works. I understand all of it. But why do I need a pointer at all? I can call obj.SetXandY() without making a pointer and it still preforms the same. So there isn't any need for objpointer.
What do you call a cow with no legs?

Ground beef.
ES Beta Backer
ES Beta Backer
Posts: 23
Joined: Mon Dec 23, 2013 9:56 pm
Location: Your VCR.

Re: Question about Polymorphism.

Postby blueshackstudios on Mon May 12, 2014 2:23 pm

You are correct in that it is useless in your situation, but that is a bad example.

Mainly polymorphism is useful when you need a base class pointer for general use.

Consider the following:
Code: Select all
class Enemy
     virtual void Update()
class Goblin : public Enemy
     void Update() // Overrides virtual function in base class
int main()
     Enemy* enemy;
     enemy = new Goblin();

     return 0;

In the above situation you can inherit from enemy to make goblins, ogres, bandits, etc, and then only call enemy->Update() after setting the enemy to whatever type you need. This is especially useful for having a list like List<Enemy*> and adding all different sorts of enemies to it, and then looping the list and calling ALL of their Update()'s.
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon May 12, 2014 2:15 pm

Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 1 guest