• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Constructor Overloading in C++
Next →
← Prev

Constructor Overloading in C++

By Dinesh Thakur

The constructor is key for object initialization. The mechanism of the constructor is made considerably more powerful by combining with the feature of overloading. It is made possible by providing more than one constructor in a class. It is called constructor overloading.

Constructor Overloading in C++ allows a class to have more than one constructors that have the same name as that of the class but differs only in terms of a number of parameters or parameter’s datatype or both. Constructors often use this facility of constructor overloading to provide more than one style of initialization. By overloading a constructor of a class, we make the class more versatile as it allows you to-construct objects in a variety of ways. To understand the concept of constructor overloading, let us consider the following program.

#fnclude<iostream.h>
#include<conio.h>
class rectangle {
 private:
  int length,breadth;
 public:
  rectangle () {
   length = breadth = 0;
   cout<<"constructor with zero parameter called\n";
  }
  rectangle (int a) {
   length = breadth = a;
   cout<<"constructor with one parameter called\n";
  }
  rectangle(int a,int b) {
   length = a; 
   breadth = b;
   cout<<"constructor with two parameters called\n";
 }
 int area() {
  return(length*breadth);
 }
};
int main() {
 clrscr();
 rectangle r1; //constructor with zero parameter called
 rectangle r2(5); //constructor with one parameter called
 rectangle r3(7,8); //constructor with two parameters called
 cout<<"\nArea of first rectangle = "<<r1.area() ;
 cout<<"\nArea of square = "<<r2.area() ;
 cout<<"\nArea of second rectangle = "<<r3.area():
 getch();
 return 0;
}
Output :
constructor with zero parameter called
constructor with one parameter called
constructor with two parameter called
Area of first rectangle = 0
Area of square = 25
Area of second rectangle = 56

Explanation: In this program, the class rectangle contains three constructors with the same name rectangle, which differs only in the number of parameters and hence overloaded.

The first constructor rectangle() doesn’t take any parameter and initialize both data members to 0. The second constructor rectangle (int a) takes a parameter of int type and initialize both the data member’s length and breadth with a value of an (i.e., 5). The third constructor rectangle(int a, int b) takes two parameters and b of int types used to initialize the data member’s length and breadth, respectively.

Now we have defined three constructors associated with the class rectangle. These instructors will be executed when an object of rectangle class is created upon the number of arguments specified in the definition of an object.

On execution of the first statement rectangle r1; in main (), an object r1 of type rectangle is created, and constructor with no parameter is called as no arguments are specified after r1 object definition. It initializes both of its data members to a value 0 and displays the message constructor with a zero parameter called.

The statement rectangle r2(5); creates an object r2 of type rectangle and invokes a constructor with one parameter as only one argument is specified after r2 object definition. The data member’s length and breadth of object r2 are initialized with the value 5 passed to a single parameter constructor. It also displays the message constructor with one parameter called on the console indicating the type of constructor being called.

The statement, rectangle r3 (7, 8), creates an object r3 of type rectangle and invokes a constructor with two parameters as two arguments are specified after r3 object definition. This constructor initializes the data member’s length and breadth with values of parameters a and b (i.e., 7 and 8), respectively. It also displays the message constructor with two parameters called on the console indicating the type of constructor being called. Finally, these values are used to calculate the area of three rectangles resented by objects r1, r2 and r3.

We can specify the arguments to a constructor in the following three equivalent forms.

rectangle r2(5);//Implicit call
rectangle r2 = rectangle(5) ;//Explicit call
rectangle r2 = 5; //Implicit call 

The first and the third forms are the implicit constructor call. These representations of passing an argument to a parameterized constructor are a short-hand for the second form of constructor call. In the second form, we are explicitly calling the constructor for initializing the object r2.

The third form can only be used when specifying a single argument after object definition, which is quite similar to basic variable initialization. However, only the first and second forms of the constructor can be used for two or more arguments. In general, we recommend using the first form as it is shorter and easy to implement. Some of the valid and invalid constructor calls are shown below :

rectangle r3 = rectangle(7,8);//Valid
rectangle r3 = (7,8); //Invalid
rectangle r1(); //Invalid

You’ll also like:

  1. Constructor Overloading in Java with Example
  2. Calculate Area of Rectangle using Constructor Overloading
  3. Parameterized Constructor in Java Example
  4. Copy Constructor in Java Example
  5. Function Overloading and Method Overloading in Java
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW