
- 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
How to convert a class to another class type in C++?
In this tutorial, we will be discussing a program to understand how to convert a class to another class type in C/C++.
Class conversion can be done with the help of operator overloading. This allows data of one class type to be assigned to the object of another class type.
Example
#include <bits/stdc++.h> using namespace std; //type to which it will be converted class Class_type_one { string a = "TutorialsPoint"; public: string get_string(){ return (a); } void display(){ cout << a << endl; } }; //class to be converted class Class_type_two { string b; public: void operator=(Class_type_one a){ b = a.get_string(); } void display(){ cout << b << endl; } }; int main(){ //type one Class_type_one a; //type two Class_type_two b; //type conversion b = a; a.display(); b.display(); return 0; }
Output
TutorialsPoint TutorialsPoint
- Related Articles
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
- How to access the members of a class from another class in Java?
- How to convert a string to a Python class object?
- Convert Class in C#
- Convert variables from one type to another in Arduino
- How to use the SimpleDateFormat class to convert a Java Date to a formatted String?
- How to convert primitive data into wrapper class using Java?
- Java Program to create String to super class type mapping
- How to declare a class in Java?
- How to instantiate a class in C#?
- How to inherit a class in C#?
- How to define a class in Arduino?
- How to call a parent class function from derived class function in C++?
- How to ensure that child class overrides a super class method in java?

Advertisements