Unit 3 Half
Unit 3 Half
Object Oriented
Programming Using C++
• Create a class that defines the data type that is to be used in the overloading operation.
• Declare the operator function operator op( ) in the public part of the class.
• Define the operator function to implement the required operations.
Operator overloading
• Rules for Overloading Operators
• Although it looks simple to redefine the operators, there are certain restrictions and
limitations in overloading them. Some of them are listed below:
• Only existing operators can be overloaded. New operators cannot be created.
• The overloaded operator must have at least one operand that is of user-defined type.
• We cannot change the basic meaning of an operation That is to say, we cannot redefine
the plus (+) operator to Subtract one value from the other.
• Overloaded operators follow the syntax rules of the original operators. They cannot
overridden.
• There are some operators that cannot be overloaded.
• We cannot use friend functions to overload certain operators, However, member
functions can be used to overload them.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class Distance
Operator overloading
{ void displayDistance()
private: {
int feet; cout<<"Entered feet value is: “;
int inches; cout << feet<<”\n”;
public: cout<<"Entered inches value is: “;\
Distance() cout << inches<<”\n\n”;
{ } Distance D1(11, 10), D2(-5,
feet = 0; Distance operator-() 11);
inches = 0; { clrscr();
} feet = -feet; -D1;
Distance(int f, int i) inches = -inches; D1.displayDistance();
{ return Distance(feet, inches); -D2;
feet = f; } D2.displayDistance();
inches = i; }; getch();
} void main() }
{
Function overriding
• A function is a block of statements that together performs a specific task
by taking some input and producing a particular output.
• So, when we call the function using its name for the parent object, the
parent class function is executed. But when we call the function using the
child object, the child class version will be executed.
Function overriding
• Types of Function Overriding in C++ - Unlike other languages such as
Java where function overriding is strictly done at compile time, C++
supports two types of function overriding:
1. Compile Time Overriding 2. Runtime Function Overriding
• Compile Time Function Overriding - In compile time function overriding,
the function call and the definition is binded at the compilation of the
program. Due to this, it is also called early binding or static binding..
• Syntax
class Parent
{
access_modifier :
return_type name_of_the_function() { } // overridden function
};
Function overriding
class child : public Parent
{
access_modifier :
return_type name_of_the_function() { } // overriding function
};
• Example:
#include <iostream.h>
class Parent
{
public:
void Test()
{ cout << "Base Function" << endl; }
Function overriding
};
class Child : public Parent
{
public:
void Test()
{ cout << "Derived Function" << endl; }
};
int main()
{
Child Child_Derived; or Parent p;
Child_Derived.Test(); or p.Test()
return 0;
}
Function overriding
• Runtime Function Overriding using Virtual Function -Function
overriding can also be performed at the runtime, which means that
function call will be binded to its definition during runtime (also known as
late binding or dynamic binding). This can be done with the help of virtual
functions.
int main()
{
Base *basePtr;
Base b_obj;
Derived derivedObj;
basePtr = &derivedObj; or basePtr = &b_obj;
basePtr->display();
return 0;
}