C plus plus header banner

#Previous question #Index of C++ Questions #Next question


Overloading is a design of object oriented programming where a member function or operator can take different arguments. Overloading is also known as polymorphism. Here a function or operator is seen in different forms or prototypes keeping the name as same.

Overloading needs

Overloading is often needed when functions or operators can take various parameters. Constructor and member functions are often overloaded to ease the design and interface.

Constructor overloading

Compiler makes a default constructior and default destructor which does not take any argument (default constructior or destructor are argument less). Now an objectioct might require various attributes to be assigned during construction. So Constructors are overloaded very often and it is very common programming practice.

enum shapes
class Shape
  shapes type;
  int m_ForeColor;
  int m_BackColor;
class Circle
  int m_radius;
  Circle(int radius);
  Circle(int radius, int ForeColor, int BackColor);
class Rectangle
  int m_width,m_height;
  Rectangle(int width, int height);
  Rectangle(int width, int height, int ForeColor, int BackColor);
int main()
  return 0;

Destructor overloading

Destructor is designed to free allocated memory and do clean up operations. There is no need to take arguments or rather there is no need for overloading. An overloaded destructor would mean that the destructor has taken arguments. Since a destructor does not take arguments, it can never be overloaded. Overloading of destructor can never be done and compiler will produce errors.

Destructor overloading error

We have added a line with a overloaded destructor which is having two arguments and it produces a compiler error. The error "destructor cannot have any parameters" will be seen during the compilation process of g++/gcc.

class Rectangle
  ~Rectangle(int width, int height); 
int main()
  return 0;
$ g++ dest.cpp 
dest.cpp:32:3: error: destructor cannot have any parameters
  ~Rectangle(int width, int height);
1 error generated.

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students