You are on page 1of 12

SNS COLLEGE OF TECHNOLOGY

Coimbatore-35.
An Autonomous Institution

• COURSE NAME : 16CS451 OBJECT ORIENTED PROGRAMMING USING C++

• III YEAR/ V SEMESTER

• UNIT – III OPERATOR OVERLOADING AND INHERITANCE

• Topic: Data conversion

Mrs.S.R.Janani
Assistant Professor
Department of Computer Science and Engineering
Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING
08/23/2021 1
USING C++/Janani.S.R/CSE/SNSCT
Unit 3 - Syllabus
• Need of operator overloading
• Overloading Unary Operators- Overloading binary Operators
• Overloading Special Operators
• Data Conversion
• Inheritance: Derived Class and Base Class
• Derived Class Constructors-Overriding Member Functions
• Class Hierarchies-Public and Private Inheritance
• Levels of Inheritance
• Multiple Inheritances

Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING


08/23/2021 2/10
USING C++/Janani.S.R/CSE/SNSCT
C++ Data Conversion
• Data conversion in C++ includes conversions between basic types and
user-defined types, and conversions between different user-defined types.
• The assignments between types, whether they are basic types or user-
defined types, are handled by the compiler with no effort on our part,
provided that the same data type is used on both sides of the equal sign.
• The different possible data conversion situations are:
1. Conversion between basic and user defined
a) Conversion from basic to user defined data type
b) Conversion from user-defined data type to basic data type
2. Conversion between user defined data types
a) Conversion in destination object
b) Conversion in source object

Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING


08/23/2021 3
USING C++/Janani.S.R/CSE/SNSCT
Conversion from basic to user defined data type

Conversion from basic to user defined type is done by using the


constructor function with one argument of basic type as follows.
Syntax:
class class_name
{
private:
//….
public:
class_name ( data_type)
{ // conversion code }
};

Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING


08/23/2021 4
USING C++/Janani.S.R/CSE/SNSCT
//example  int main()
//conversion from basic type to object {  
celsius cel;   //cel is user defined    
#include<iostream>
float fer;             //fer is basic type    
using namespace std; cout<<"\nEnter temperature in Fahrenheit measurement: ";    
class celsius cin>>fer;    
{ cel=fer;  
private:     //convert from basic to user-defined;
float temper; //eqvt to à cel = celsius(fer);    
cel.showtemper();    
public:    
return 0;
celsius()    
}
{         temper=0;     }  
celsius(float ftmp)    
{         temper=(ftmp-32)* 5/9;     }     Output:
void showtemper()     Enter temperature in Fahrenheit measurement: -40
{         cout<<"Temperature in Celsius: "<<temper;     Temperature in Celsius: -40
}
};

Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING


08/23/2021 5
USING C++/Janani.S.R/CSE/SNSCT
Conversion from user-defined type to basic data type

• Conversion from user-defined type of basic data type is done by


overloading the cast operator of basic type as a member function. 
• Return type of operator is not specified because the cast operator
function itself specifies the return type.
Syntax:
class class_name
{ ...  
public:
operator data_type()
{ //Conversion code }
};

Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING


08/23/2021 6
USING C++/Janani.S.R/CSE/SNSCT
//conversion from user to basic type
#include<iostream>
using namespace std; int main()
class celsius {
{ celsius cel; //cel is user defined
private: float fer; //fer is basic type
float temper; cel.gettemper();
public:
fer=cel; //convert from user-defined to basic;
celsius()
{ //eqvt to fer= float(cel);
temper=0; cout<<"\nTemperature in Fahrenheit measurement: "<<fer;
} }
operator float()
{
float fer; Output:
fer=temper *9/5 + 32; Enter Temperature in Celsius: -40
return (fer);
Temperature in Fahrenheit measurement: -40
}
void gettemper()
{
cout<<"\n Enter Temperature in Celsius:";
cin>>temper;
}
};
Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING
08/23/2021 7
USING C++/Janani.S.R/CSE/SNSCT
Conversion in Destination Object
For this expression to work, the conversion routine
• This conversion is exactly like conversion of should exist in classA (destination class type) as a one
basic to user defined data type i.e. one argument constructor as
argument constructor is used.
//source object class
• Conversion routine is defined as a one class classB   
argument constructor in destination class {     //body of classB }
and takes the argument of the source class
type. //destination object class
class classA
• Eg. {
private:    
classA objA;
//….
classB objB; public:    
classA (classB objB) //object of source class    
objA=objB; {         //code for conversion        
Here, objB is the source of the //from classB        
class classB and objA is the destination //to classA    
object of class classA. }
}; 
Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING
08/23/2021 8
USING C++/Janani.S.R/CSE/SNSCT
Conversion in source object
Syntax:
• This conversion is exactly like conversion //destination object class
of user-defined type to basic type i.e. class classA   
{  
overloading the cast operator is used. //body of classA
• Eg. }
classA objA; //source object class
classB objB; class classB
{
objA=objB; private:     
• Here, objB is the source of the //….
public:    
class classB and objA is the destination
operator classA () //cast operator destination
object of class classA. Conversion types    
routine is specified as conversion (cast) {    
operator overloading for the type of //code for conversion from classB        
destination class. //to classA    
}
};
Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING
08/23/2021 9
USING C++/Janani.S.R/CSE/SNSCT
class Polar int main()
{ {
//example
//conversion from user defined to private: Polar pol(10.0, 0.78);
user defined float radius, angle; Cartesian cart;
//conversion routine in source class public: cart=pol;
#include<iostream> Polar() cout<<"\nGiven Polar: ";
#include<cmath> { pol.display();
using namespace std; radius=0; cout<<"\nEquivalent cartesian: ";
class Cartesian angle =0; cart.display();
{ }
private: return 0;
Polar(float rad, float ang) }
float xco, yco;
public: {
Cartesian() radius =rad;
{ angle=ang;
xco=0; }
yco=0; operator Cartesian() Output:
} { Give Polar : (10.0, 0.78)
Cartesian(float x, float y) float x=static_cast<int>(radius * cos(angle));
{
Equivalent cartesian: (7,7)
float y=static_cast<int>(radius * sin(angle));
xco=x; return Cartesian(x,y);
yco=y;
} }
void display() void display()
{ {
cout<<"("<<xco<<","<<yco<<")"; cout<<"("<<radius<<","<<angle<<")";
} }
}; };
Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING
08/23/2021 10
USING C++/Janani.S.R/CSE/SNSCT
Assessment 1
• List out the possible data conversion situations

Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING


08/23/2021 11
USING C++/Janani.S.R/CSE/SNSCT
References
• Larry L. Peterson, Bruce S. Davie, “Computer Networks: A systems approach”, Fifth Edition, Morgan Kaufmann
Publishers, 2011.
• Behrouz A. Forouzan, “Data communication and Networking”, Fourth Edition, Tata McGraw – Hill, 2011.
• James F. Kurose, Keith W. Ross, “Computer Networking - A Top-Down Approach Featuring the Internet”, Fifth Edition,
Pearson Education, 2009
• Nader. F. Mir, “Computer and Communication Networks”, Pearson Prentice Hall Publishers, 2010.

Data conversion /16CS451 OBJECT ORIENTED PROGRAMMING


08/23/2021 12
USING C++/Janani.S.R/CSE/SNSCT

You might also like