Cahpter 5 Operator Overloading
Cahpter 5 Operator Overloading
#include <iostream.h>
class Team
{
private:
int players;
public:
Team()
{
players=0;
}
void show_players()
{
cout<<"Number of players= "<<players<<endl;
}
// for postfix notation
void operator ++(int)
{
players= players+1;
}
};
int main()
{
Team t1;
t1.show_players();
t1++;
t1.show_players();
return 0;
Example of Overloading both increment notations in a same program
#include <iostream.h> }
class Team // for postfix notation
{ void operator ++(int)
private: {
int players; players= players+1;
public: }
Team() };
{ int main()
players=0; {
} Team t1;
void show_players() t1.show_players();
{ ++t1;
cout<<"Number of players= "<<players<<endl; t1.show_players();
} t1++;
// For prefix notation t1.show_players();
void operator ++() return 0;
{ }
Overloading comparison operator in C++
#include <iostream.h> }
class Team // for postfix notation
{ void operator ++(int)
private: {
int players; players= players+1;
public: }
Team() };
{ int main()
players=0; {
} Team t1;
void show_players() t1.show_players();
{ ++t1;
cout<<"Number of players= "<<players<<endl; t1.show_players();
} t1++;
// For prefix notation t1.show_players();
void operator ++() return 0;
{ }
Assignment Operators Overloading
• You can overload the assignment operator (=) just
as you can other operators and it can be used to
create an object just like the copy constructor.
Cont’d…
#include <iostream> inches = D.inches;
using namespace std; }
class Distance // method to display distance
{ void displayDistance()
private: {
int feet; // 0 to infinite cout << "F: " << feet << " I:" << inches << endl;
int inches; // 0 to 12 }
public: // required constructors };
Distance() int main()
{ {
feet = 0; Distance D1(11, 10), D2(5, 11);
inches = 0; cout << "First Distance : ";
} D1.displayDistance();
Distance(int f, int i){ cout << "Second Distance :";
feet = f; D2.displayDistance();
inches = i; // use assignment operator
} D1 = D2;
void operator=(const Distance &D ) cout << "First Distance :";
{ D1.displayDistance();
Type cast operator
• A cast is a special operator that forces one data type
to be converted into another.
• As an operator, a cast is unary and has the same
precedence as any other unary operator.
• The most general cast supported by most of the C+
+ compilers is as follows:
(type) expression
Where type is the desired data type. There are other
casting operators supported by C++, they are listed
below:
Cont’d…
1. const_cast<type> (expr): The const_cast operator is used to
explicitly override const and/or volatile in a cast. The target type
must be the same as the source type except for the alteration of its
const or volatile attributes. This type of casting manipulates the
const attribute of the passed object, either to be set or removed.
2. dynamic_cast<type> (expr): The dynamic_cast performs a runtime
cast that verifies the validity of the cast. If the cast cannot be made,
the cast fails and the expression evaluates to null. A dynamic_cast
performs casts on polymorphic types and can cast a A* pointer into
a B* pointer only if the object being pointed to actually is a B object.
3. reinterpret_cast<type> (expr): The reinterpret_cast operator
changes a pointer to any other type of pointer. It also allows casting
from pointer to an integer type and vice versa.
4. static_cast<type> (expr): The static_cast operator performs a
nonpolymorphic cast. For example, it can be used to cast a base
class pointer into a derived class pointer.
Cont’d…
#include <iostream.h>
main()
{
double a = 21.09399;
float b = 10.20;
int c ;
c = (int) a;
cout << "Line 1 - Value of (int)a is :" << c << endl ;
c = (int) b;
cout << "Line 2 - Value of (int)b is :" << c << endl ; return
0;
Subscripting [] operator overloading in C++
int main()
{
Employee a;
a.name=new char[10];
strcpy(a.name, "spiderman");
strcpy(a.dept,"IT");