Hi guys,
I have a question regarding C++ classes and polymorphism. I understand the concept of pointers and that polymorphism with respect to operators(+, -, etc...) means that the operators can have different meanings based on "context" (how they are used). My question is regarding class pointers. For example, if in the following example code:

class ClassName
{
     AnyMembers
};

class DerivedClassName : public ClassName
{
   AnyMembers
} ;

ClassName* classPointerObject = new DerivedClassName;

How does "classPointerObject" relate to the derived class object?
Is this just a matter of a simple "is-a-type-of" relationship?

Your input would be greatly appreciated.
Thanks,
Navid

Recommended Answers

All 5 Replies

What are you asking? Are you confused about the operational semantics of C++, or are you asking about terminology?

In C++ only a class with virtual functions is a polymorphic class and only these classes objects may have polymorphic behavior implemented via virtual function calls.
In common case the classPointerObject pointer refers to the base class subobject of the derived class object - that's all. It's a simple is_a relation (inheritance) but not a polymorphism.

Thanks ArkM,
The concept of class pointers and polymorphism make more sense now.

Thanks,
Navid

It is so polymorphism. The derived class can override the base class's behavior in other ways than just virtual functions.

It is so polymorphism. The derived class can override the base class's behavior in other ways than just virtual functions.

Don't muddle inheritance and polymorphism. If a derived class constructor changes base class data members then we get the base class object with unusual state (it's a very bad practice) and only this base class member functions are directly accessible via base class pointers or references.
The C++ Standard, 10.3:

Virtual functions support dynamic binding and object-oriented programming. A class that declares or inherits a virtual function is called a polymorphic class.

Well, let's redefine a polymorphism definition (for example, if we have at least one if statement in a member function then this class have polymorphic behaviour ;))...

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.