0% found this document useful (0 votes)
44 views3 pages

Assignment Type Conversion C++

The document presents two C++ programs demonstrating type conversion. The first program shows how a class can be converted to a basic type (int) using operator overloading, while the second program illustrates conversion from one class type to another using constructor overloading. Key concepts include type conversion, operator overloading, and implicit conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

Assignment Type Conversion C++

The document presents two C++ programs demonstrating type conversion. The first program shows how a class can be converted to a basic type (int) using operator overloading, while the second program illustrates conversion from one class type to another using constructor overloading. Key concepts include type conversion, operator overloading, and implicit conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment: Type Conversion in C++

Program 1: Class to Basic Type Conversion


Heading: Type Conversion from Class to Basic Type (int)
Key Concepts: Type Conversion, Operator Overloading, Implicit Conversion
Objective: Demonstrate how a class type can be converted to a basic data type (int)

#include <iostream>
using namespace std;

class A {
int value;

public:
A(int v = 0) {
value = v;
}

// Class to Basic type conversion function (class to int)


operator int() {
return value;
}

void display() {
cout << "Value inside class A: " << value << endl;
}
};

int main() {
A a(100); // Create object with value
a.display(); // Display value from object

int x = a; // Class to Basic conversion (int)


cout << "Converted to int: " << x << endl;

return 0;
}

/*
--------- Output ---------
Value inside class A: 100
Converted to int: 100
*/

Key Concepts for Program 1


1. Type Conversion: Demonstrates converting an object of a class type to a basic type (int).
2. Operator Overloading: The `operator int()` function is used to convert the class object
into an `int`.
3. Implicit Conversion: The class object `a` is automatically converted to an integer without
explicit casting in the `main()` function.

Program 2: Class to Class Type Conversion


Heading: Type Conversion from Class to Class
Key Concepts: Constructor Overloading, Type Conversion, Class to Class Conversion
Objective: Demonstrate the conversion from one class type (A) to another class type (B)
using a constructor

#include <iostream>
using namespace std;

class A {
int value;

public:
A(int v = 0) {
value = v;
}

// Conversion to basic type (int) for class-to-class conversion


operator int() {
return value;
}

void display() {
cout << "Value in class A: " << value << endl;
}
};

class B {
int data;
public:
B() {}

// Constructor to convert from class A to B


B(A a) {
data = a; // Uses Class to Basic conversion inside constructor
}

void show() {
cout << "Value in class B: " << data << endl;
}
};

int main() {
A a(200); // Create object of class A
a.display(); // Show class A's value

B b = a; // Class to Class conversion


b.show(); // Show class B's value

return 0;
}

/*
--------- Output ---------
Value in class A: 200
Value in class B: 200
*/

Key Concepts for Program 2


1. Constructor Overloading: The constructor in class `B` is used to convert an object of class
`A` into an object of class `B`.
2. Class to Class Conversion: This program demonstrates how to transfer data from one
class to another by utilizing implicit type conversion via a constructor.
3. Type Conversion: The conversion from class `A` to class `B` is done through the `int`
conversion operator and the constructor in class `B`.

You might also like