×

Parameterize Constructor

C++ Parameterized Constructor

A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax:
className(data-type argument){  
// Constructor definition  
}  
className(data-type argument, data-type argument){  
// Constructor definition  
}
A parameterized constructor can be passed values to constructor function in two ways: 1) Calling constructor implicitly
Employee emp(101);
2) Calling constructor explicitly
Employee emp= Employee(101);

C++ Parameterize Constructor Example 1

Creating a class with the name 'Employee' and a parameterize constructor 'Employee(int)' which initialize data member 'id'.
#include <iostream>  
using namespace std;  
class Employee  
{  
    int id;  
    public:  
    Employee(int x){  
        id=x;  
    }  
    void display(){  
        cout<<"id="<<id;  
    }  
};  
int main(){  
    Employee emp(101);  
    emp.display();  
    return 0;  
}
Output:
Id=101

C++ Parameterize Constructor Example 2

We can create multiple parameterize constructor in a single class and called constructor as implicit or explicit.
#include <iostream>  
using namespace std;  
class Employee  
{  
    int id;  
    string dept;  
    public:  
    Employee(int x){  
        id=x;  
    }  
    Employee(string dep){  
        dept=dep;  
    }  
    void displayId(){  
        cout<<"id="<<id<<endl;  
    }  
    void displayDept(){  
        cout<<"Dept="<<dept<<endl;  
    }  
};  
int main(){  
    Employee emp1(101); //implicit  
    Employee emp2=Employee("Programmer"); //explicit  
    emp1.displayId();  
    emp2.displayDept();  
    return 0;  
}
Output:
id=101
Dept=Programmer

Related Topics

How to Setup Environment for C++ Programming on Mac

Mac OS X code Installation There are so many environments available for C++. We are going to install jGrasp and Xcode in our mac operating system. Instruction for installation of jGrasp and...

2 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

Input Iterators in C++

What are input iterators? Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented. All the iterators...

4 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.