
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Conversion constructor in C++?
In this section we will see what is the conversion constructor in C++ class. A constructor is a special type of function of class. It has some unique property like, its name will be same as class name, it will not return any value etc. The constructors are used to construct objects of a class. Sometimes constructors may take some arguments, or sometimes it does not take arguments.
When a constructor takes only one argument then this type of constructors becomes conversion constructor. This type of constructor allows automatic conversion to the class being constructed.
Example
#include<iostream> using namespace std; class my_class{ private: int my_var; public: my_class(int x) { this->my_var = x; //set the value of my_var using parameterized constructor } void display() { cout << "The value of my_var is: " << my_var <<endl; } }; int main() { my_class my_obj(10); my_obj.display(); my_obj = 50; //here the conversion constructor is called my_obj.display(); }
Output
The value of my_var is: 10 The value of my_var is: 50
- Related Articles
- Difference between Static Constructor and Instance Constructor in C#
- Type Conversion in Python
- Narrowing Conversion in Java
- Conversion Operators in C++
- ZigZag Conversion in C++
- Conversion Functions in SQL
- Type Conversion in C++
- Conversion Disorder
- Energy Conversion
- Enum constructor in Java
- Copy Constructor in C++
- Constructor Delegation in C++
- Virtual Constructor in C++
- Constructor overloading in Java
- Constructor Overloading in C#

Advertisements