C++Builder Learn about Polymorphism :: Pointers to Base Classes in C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn about Polymorphism :: Pointers to Base Classes in C++
By Yilmaz Yoru May 21, 2021

Generally we try to prepare our posts for a first time C++ readers or we assume reader read some posts about that topic before. At least we try to simplify and clarify the post as much as possible. This post requires a good knowledge about Functions, Classes, Objects and Pointers. These topics are the main stones of C++. So If you feel hard to understand all below, or if you are not really sure of the meaning of any of the following expressions below this is very normal. Please read our previous posts about Functions, Classes, Objects and Pointers. Reading also is not enough, try to run some codes make your own specific codes for specific variables, functions. So you may be ready to understand all below.

Let’s remember that, Для просмотра ссылки Войди или Зарегистрируйся is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methods, functions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. In C++, classes have members (attributes, methods) and we should protect each member inside this class.

One of the main features of class inheritance is that a pointer to a derived class is type-compatible with a pointer to its base class. It is the best way to do this professionally with Polymorphism which is a powerful and versatile feature of C++ programming language.

Polymorphism in C++ uses inherit attributes and methods from another class methods to perform different tasks. Polymorphism means multiple forms, and this method is used when we have many classes that are related to each other by inheritance. We explained Inheritance in C++ posts before.

For example, a human at the same time can have different skills. For example a worker is a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism. Polymorphism is considered as one of the important features of Object Oriented Programming.

Base Classes, Derived Classes and Pointers to Base Class

The Base Class, also known as the Parent Class or the Super Class is a class, from which other classes are derived. In other term it is a base class for other derived classes. That means if a derived class which inherits the base class has all members of a base class as well as can also have some additional properties. The Base class members and member functions are inherited to object of the derived class.

The Derived Class, also known as Child Class or SubClass, is a class that is created from an existing class. The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class.

Let’s explain this with an example, here we will create a 2D shape class that holds protected width and height dimensions of any shape, so this class and these protected parameters can be defined as below,
C++:
class shape
{
  protected:
 int width, height;
};
Now, let’s extend this with a new set_dimension() method which sets width and height of any shape. We should modify this shape class as below,
C++:
class shape
{
  protected:
 int width, height;
 
  public:
 void set_dimension(float w, float h)
 {
 width = w;
 height = h;
 }
};
In C++, we can define Derived Class with a Base Class. To do this we should use : after the name of class and we should add The type of inheritance is specified by the access-specifier and the name of base class. In general, we can define a public derived class as below,
C++:
class derived_class: public base_class
{
};
As same in that form, we can define a new rectangle and ellipse classes (Derived Class) including our main shape class (Base Class) as below,
C++:
class rectangle: public shape
{
  public:
 float area()
 {
 return width*height;         // A = a*b
 }
};

class ellipse: public shape
{
  public:
 float area()
 {
 return 3.14*width*height/4;   // A = pi*D1*D2/4
 }
};
If you want you can add more shapes too. Now let’s see how we use all these, Now let’s combine all together and use them in a main form.
C++:
#include <iostream>
 
using namespace std;
 
class shape
{
  protected:
 int width, height;
 
  public:
 void set_dimension(float w, float h)
 {
 width = w;
 height = h;
 }
};
 
class ellipse: public shape
{
  public:
 float area()
 {
 return 3.14*width*height/4;   // A = pi*D1*D2/4
 }
};
 
class rectangle: public shape
{
  public:
 float area()
 {
 return width*height;         // A = a*b
 }
};
 
int main ()
{
  ellipse   ellps;
  rectangle rect;
 
  shape *shape1 = &rect;
  shape *shape2 = &ellps;
 
  shape1->set_dimension(5.0, 8.0);
  shape2->set_dimension(5.0, 8.0);
 
  cout << "Area of Rectangle:" << rect.area() << '\n';
  cout << "Area of Ellipse:" << ellps.area() << '\n';
 
  getchar();
 
  return 0;
}
In C++ polymorphism is mainly divided into two types:
  • Compile time Polymorphism
  • Runtime Polymorphism