Type conversion in c++ | basic to class | class to basic | class to class

preview_player
Показать описание
Type conversion in c++
• When different types are mixed in an expression, then automatic type conversion is applied on operands according to certain rules. Similarly in assignment operation, the type of data on the right hand of assignment is automatically converted to the type of the variable on the left. For example:
int y;
float n=2.345;
y=n;
• In the above code snippet the value of n is converted into integer when it is stored into y. Thus the fractional part is truncated and only 2 is stored into the y.
• The automatic type conversion is performed only when only primitive types are involved in an expression. When user defined types are involved in an expression then automatic type conversion cannot be performed.
• We have to define operator functions (casting operator functions) for performing conversions with user defined types. We can have following three types of conversions where user defined types are involved:
i) Basic to class type : When we create objects using the variables of primary data types then it is called as basic to class type conversion. Generally we use single argument constructor to perform type conversion from basic type to class type.
#include<iostream>
using namespace std;
class Number
{ int n;
public:
void printDetails(){ cout<<n; }
Number(int n){ this->n=n;}
};
int main()
{ Number numb=100;
return 0;
}
ii)Class type to basic type: When we assign an object to a primitive data type’s variable, then it is called as class type to basic conversion. To perform class type to basic type conversion we have to define the casting operator function. The casting operator function must be a member of the class. The casting operator function cannot have any return datatype and it does not take any parameter. It have following syntax:
operator datatype()
{ ___________;
___________;
}
Example:
#include<iostream>
using namespace std;
class Number
{ int n;
public:
void printDetails(){ cout<<n; }
Number(int n) { this->n=n; }
operator int() { return n; }
};
int main()
{ Number numb=100;
cout<<"\nThe value is"<<x;
return 0;
}
iii) One class type to other class type : When we assign an object of a class into the object of another class then it is called as class to class conversion. The class to class conversion can be performed either by defining casting operator function in source class or using the constructor in the destination class.
Conversion using casting operator function
#include<iostream>
using namespace std;
class Rectangle
{ int width,length,area;
public:
Rectangle(int w,int l)
{ width=w; length=l; area=width*length;
}
void output()
{ cout<<"\nLength : "<<length<<"\nWidth : "<<width<<"\nArea of rectangle : "<<area;
}
};
class Triangle
{ int base,height;
float area;
public:
Triangle(int b,int h)
{ base =b; height=h; area=0.5*base*height;
}
void print()
{ cout<<"\nBase : "<<base<<"\nHeight : "<<height<<"\nArea of triangle : "<<area;
}
operator Rectangle()
{ Rectangle temp(base,height);
return temp;
}
};
int main()
{ Triangle t(10,20);
Rectangle r=t;//Triangle to Rectangle
return 0;
}

Conversion using constructor
#include<iostream>
using namespace std;
class Triangle
{ int base,height;
float area;
public:
Triangle(int b,int h)
{ base =b; height=h; area=0.5*base*height;
}
void print()
{ cout<<"\nBase : "<<base<<"\nHeight : "<<height<<"\nArea of triangle : "<<area;
}
int getBase() { return base; }
int getHeight() { return height; }
};
class Rectangle
{ int width,length,area;
public:
void output()
{ cout<<"\nLength : "<<length<<"\nWidth : "<<width<<"\nArea of rectangle : "<<area;
}
Rectangle(Triangle t)
}
};
int main()
{ Triangle t(10,20);
Rectangle r=t;//Rectangle(t)
return 0;
}
Note: Replace all < with less than and all > with greater than.
#tarunsir #c++ #programming #c++inhindi
Рекомендации по теме
Комментарии
Автор

Very very good explanation. appreciated 👍

DextralPrime
Автор

Sir in basic to class type we should always do initialize with only this pointer ha sir Or our wish? Any other method

agtechchannel
Автор

Basic to class type 5:03 to 7:01
Class to basic type 8:09 to 11:00
One class to other class : 12:38 to 20:29

Niharikabsharma
Автор

sir isme konsa software use kiya he coding ke liye ...

amnnma
Автор

Hello,
So you said for class type to basic type "The casting operator function cannot have any return datatype" but in the operator int function it is returning n.

Oposquash
Автор

you said the typecasting operator function does not have return data type so how did you return a object from the function ?

hekermen
Автор

Sir in cast operator function can we pass void as argument????

tradertrader
Автор

Syntax for basic to class type conversion?

rishita
Автор

Sir please teach in english also or add some subtitles i cant understand the language you are speaking

ahamedshathelegend
Автор

Sir ye types of converstion hai
To implict aur explicit bhi types
Hai mujhe smjh nhi aaya 😅

thakursahab