-4

Why do I need to refer an object of an inherited class through a pointer to its base class, when I am aware that the call to a function, exclusive to the inherited class, will produce a compilation time error?

Why Polymorphism?

Edit:

Here's a small piece of code as an example:

enum Suit { Spade, Heart, Club, Diamond };
enum Val { Ace=1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };

class Card {
private:
    Val val;
    Suit suit;
public:
    Card(Val val, Suit suit) : val(val), suit(suit) {
        cout << "Card constructor called" << endl;
    }
    int get_val() { return val; }
    Suit get_suit() { return suit; }
};

class BlackJackCard : public Card {
public:
    int garbage;
    BlackJackCard(Val val, Suit suit) : Card(val, suit), garbage(9) {}
    int get_val() {
        Val tmpVal = (Val)Card::get_val();
        if(tmpVal == 1) return 11;
        if(tmpVal < 10) return tmpVal;
        return 10;
    }
    int exclusive_to_BJC() {
        cout << "I do nothing!! and the garbage value my object holds is " << garbage << endl;
    }
};

int main() {
    Card *newCard = new BlackJackCard(King,Spade);
    cout << newCard->get_val() << endl;     // 13
    cout << newCard->get_suit() << endl;    // 0

/*Why should I use a base class referencing if I can't access all the*/
/*members of the object I have constructed(except for the virtual functions).*/
//  cout << newCard->exclusive_to_BJC() << endl;
//  cout << newCard->garbage << endl;

    BlackJackCard *new_bjCard = new BlackJackCard(King,Spade);
    cout << new_bjCard->get_val() << endl;  // 10
    cout << new_bjCard->get_suit() << endl; // 0
    cout << new_bjCard->exclusive_to_BJC() << endl;
}
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Marcus
  • 25
  • 3
  • I don't understand the question. Code example? –  Feb 17 '15 at 13:21
  • Your caps-lock is broken. – DevSolar Feb 17 '15 at 13:49
  • possible duplicate of [What is polymorphism, what is it for, and how is it used?](http://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used) – ivan_pozdeev Feb 17 '15 at 16:27
  • @ivan_pozdeev actually no, I was not asking what polymorphism is. I have read all about it from http://www.cplusplus.com/doc/tutorial/polymorphism/ . I was just not able to appreciate the use of it. But now I do, as I have mentioned in my comment below to Gnucki's answer. – Marcus Feb 18 '15 at 07:04

2 Answers2

0

Mainly for that reason (follow the link): low coupling.

If you talk about pointers, I understand C++, so you can take look at this explained example too.

Gnucki
  • 5,043
  • 2
  • 29
  • 44
  • The idea about design refactoring to increase cohesion provides a very different perspective to me. Until now I believed that inheritance is mostly about code reuse and hence I was not able to appreciate the need for Polymorphism, but now that I think about inheritance as implementation of an interface it makes complete sense. Thank you. – Marcus Feb 18 '15 at 06:57
  • Exactly! As you seem to understand it well, you may take a look at my response [here](http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo/27313969#27313969) to understand the OOP design differences (in my opinion) between abstract classes and interfaces (do not focus on score, it is a really late answer on the topic). This may help you to achieve your understanding. – Gnucki Feb 18 '15 at 08:54
0

You don't need to, you can. In fact, the vast majority of the time, it is better to take advantage of this possibility, to obtain better code. Consider:

class Vehicle
{
};

class Car : public Vehicle
{
};

int f(Vehicle *)
{
   // code written here will be able to work on any type of vehicle.
}

OOP allows you to write f() in way that all vehicles are treated the same way, from the point of view of f(). In fact, a Car can have specialized functionality for Vehicle's function, without f() even needing to know.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42