0% found this document useful (0 votes)
9 views5 pages

Type Conversion 2

The document provides examples of type conversion in C++, including conversion from built-in types to class types, class types to built-in types, and between different class types. It explains the use of constructors and overloaded casting operators for these conversions. Additionally, it includes code snippets demonstrating these concepts in practice.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Type Conversion 2

The document provides examples of type conversion in C++, including conversion from built-in types to class types, class types to built-in types, and between different class types. It explains the use of constructors and overloaded casting operators for these conversions. Additionally, it includes code snippets demonstrating these concepts in practice.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

channel link : h ps://www.youtube.

com/c/ComputerScienceAcademy7

#include<iostream.h>
#include<conio.h>
/*
1. Conversion from built in type to class type.
The constructors can be used for default type conversion from
argument's type to the constructor's class type.
*/
/* class number
{
int a;

public:
void getdata(int x)
{
a = x;
}

void display()
{
cout<<a<<endl;
}

number(){}
number(int x)
{
a = x;

}
};

void main()
{
clrscr();

int n = 15;
cout<<"Int n = "<<n<<endl;
number N1;

N1 = n; //N1 = number(n);

cout<<"Value of object N1 = ";


N1.display();

getch();

*/

/*

2. Conversion from class type to built in type


*/
/*
class number
{
int a;

public:
void getdata(int x)
{
a = x;
}

void display()
{
cout<<a<<endl;
}

operator int()
{
return a;

};
void main()
{
clrscr();

number N1;

N1.getdata(26);
cout<<"Value of object N1 = ";
N1.display();

int n = N1; //N1.operator int();

cout<<"Value of int n = "<<n;

getch();

*/

/*
C++ allows us to define a overloaded cas ng operator
(also called conversion func on) that could be used to convert
/* a class type data to basic type.

The general form of an overloaded cas ng operator func on is


operator typename ()
{

operator int ()
{

The conversion func on must sa sty the following condi ons:


1. It must be a class member
2. It must not specify a return value.
3. It must not have any arguments.

/*

3. Conversion from one class type to another class type.

Such conversions can be carried out by constructor or


a convers on func on.

*/

class number1
{
int a;

public:
void getdata(int x)
{
a = x;
}

void display()
{
cout<<a<<endl;
}

int getnum()
{
return a;
}

};

class number2
{
int a;

public:
void getdata(int x)
{
a = x;
}

void display()
{
cout<<a<<endl;
}

number2(){}

number2(number1 N1)
{
a = N1.getnum();
}

};

void main()
{
clrscr();

number1 N1;
N1.getdata(25);
cout<<"Value of N1 from number1: ";
N1.display();

number2 N2;

N2 = N1; //N2 = number2(N1);

cout<<"Value of N2 from number2: ";


N2.display();

getch();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy