IT 304 OOPM Unit III - 1693892203
IT 304 OOPM Unit III - 1693892203
Unit-3
Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the
special meaning to the user-defined data type.
Operator overloading is used to overload or redefine most of the operators available in C++. It is used to
perform the operation on the user-defined data type.
For example, C++ provides the ability to add the variables of the user-defined data type that is applied to
the built-in data types.
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is
one of the most important feature of Object-Oriented Programming.
Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
Super Class: The class whose properties are inherited by sub class is called Base Class or Super class.
Unary Operator overloading is a type of polymorphism in which an operator is overloaded to give user defined meaning
to it. It is used to perform operations on user-defined data type. Following program shows the concept of overloading
unary operator:
Example:
Class name Distance takes two members of object i.e. feet and inch, create a function by which Distance object should
decrement the value of feet and inch by 1.
#include <iostream>
using namespace std;
class Distance {
public:
// Member Object
int feet, inch;
// Constructor to initialize the object's value
Distance(int f, int i)
{
feet = f;
inch = i;
Chameli Devi Group of Institutions
Department of Information Technology
}
// Overloading(-) operator to perform decrement operation of Distance object
void operator-()
{
feet--;
inch--;
cout << "\n Feet & Inches (Decrement): " << feet << "'" << inch;
}
};
int main()
{
// declare and Initialize the constructor
Distance d1(8, 9);
A binary operator is an operator that operates on two operands and manipulates them to return a result. Operators are
represented by special characters or by keywords and provide an easy way to compare numerical values or character
strings.
Binary operators are presented in the form:
Operand1 Operator Operand2
In binary operator overloading, there should be one argument to be passed. It is overloading of an operator, operating
on two operands. Example of binary operator overloading is as follows:
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
// Overload the + operator
Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
Chameli Devi Group of Institutions
Department of Information Technology
void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};
int main() {
Complex complex1, complex2, result;
cout << "Enter first complex number:\n";
complex1.input();
cout << "Enter second complex number:\n";
complex2.input();
// complex1 calls the operator function
// complex2 is passed as an argument to the function
result = complex1 + complex2;
result.output();
return 0;
}
Output:
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i
Data conversion
Type conversion is the process that converts the predefined data type of one variable into an appropriate data type. The
main idea behind type conversion is to convert two different data type variables into a single data type to solve
mathematical and logical expressions easily without any data loss.
The different possible data conversion situations are:
1. Conversion between built-in data type and user-defined data type
a) Conversion from built-in to user defined data type
b) Conversion from user-defined data type to built-in data type
2. Conversion between user-defined data type to user-defined data type
a) Conversion in destination object
b) Conversion in source object
I. Conversion between built-in and user-defined data type
(a) Conversion from built-in to user defined data type
Conversion from built-in to user defined type is done by using the constructor with one argument of built-in type as
follows.
Syntax:
class class_name {
private:
//….
public:
class_name ( data_type) {
// conversion code
}
};
(b) Conversion from user-defined type to built-in data type
Chameli Devi Group of Institutions
Department of Information Technology
Conversion from user-defined data type to built-in data type is done by overloading the cast operator of built-in data
type as a member function. Operator function is defined as an overloaded built-in data type which takes no arguments.
Syntax:
class class_name {
...
public:
operator data_type() {
//Conversion code
}
};
II. Conversion between users defined data types to user defined data type
(a) Conversion in Destination Object
This is basically conversion of basic to user defined data type i.e., one argument constructor is used.
Conversion routine is defined as one argument constructor in destination class and takes the argument of the source
class type.
Eg.
classA objA;
classB objB;
objA=objB;
Here, objB is the source of the class classB and objA is the destination object of class classA.
For this expression to work, the conversion routine should exist in classA (destination class type)
Inheritance:
In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object
automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in
other class.
Chameli Devi Group of Institutions
Department of Information Technology
Advantage of inheritance is code reusability, programmer can reuse the members of parent class. So, there is no
need to define the members again. So, less code is required in the class.
When the base class is privately inherited by the derived class, public members of the base class becomes the
private members of the derived class. Therefore, the public members of the base class are not accessible by the
objects of the derived class only by the member functions of the derived class.
When the base class is publicly inherited by the derived class, public members of the base class becomes the
public members of the derived class. Therefore, the public members of the base class are accessible by the
objects of the derived class as well as by the member functions of the base class.
Implementing inheritance in C++: For creating a sub-class which is inherited from the base class we have to follow the
below syntax:
Syntax:
Here, subclass_name is the name of the sub class, access_mode is the mode in which you want to inherit this base class
for example: public, private etc. and base_class_name is the name of the base class from which you want to inherit the
sub class.
Base Class: A base class is a class in object-oriented programming language, from which other classes are derived. The
class which inherits the base class has all members of a base class as well as can also have some additional properties.
The base class members and member functions are inherited to object of the derived class.
Derived Class: A derived Class is a class created from an existing class. The derived class inherits all members and
member functions of a base class. The derived class can have more functionality with respect to the Base class and can
easily access the Base class.
class BaseClass{
// members....
// member function
};
Access modifiers:
Access modifiers are used to implement an important aspect of object-oriented programming known as data hiding.
There are 3 types of access modifiers available in C++:
1. Public
2. Private
Chameli Devi Group of Institutions
Department of Information Technology
3. Protected
If we do not specify any access modifiers for the members inside the class then by default the access modifier for the
members will be Private.
1. Public: All the class members declared under the public specifier will be available to everyone. The data members and
member functions declared as public can be accessed by other classes and functions too. The public members of a class
can be accessed from anywhere in the program using the direct member access operator (.) with the object of that
class.
2. Private: The class members declared as private can be accessed only by the member functions inside the class. They
are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the
friend functions are allowed to access the private data members of a class.
3. Protected: The protected members can be accessed within the class and from the derived class. Members cannot be
accessed from outside the class; however, they can be accessed in inherited classes.
Types of inheritance:
There are basically five types of inheritance in C++. The classification of inheritance is based on how the properties of the
base class are inherited by the derived classes.
1. Single Inheritance
This type of inheritance in C++ happens when the parent class has only one child class. In other words, this is only one
derived class formed from a base class.
Syntax-
class Base
{
// BODY OF THE BASE CLASS
};
class Derived : acess_specifier Base
{
// BODY OF THE DERIVED CLASS
};
2. Multiple Inheritance
This type of inheritance happens when the child class inherits its properties from more than one base class.
Multiple Inheritances is a feature of C++ where a class can inherit from more than one classes. The
constructors of inherited classes are called in the same order in which they are inherited.
Syntax
class A // Base class of B
{
// BODY OF THE CLASS A
};
class B // Derived class of A and Base class
{
// BODY OF THE CLASS B
};
class C : acess_specifier A, access_specifier A // Derived class of A and B
{
// BODY OF CLASS C
};
Chameli Devi Group of Institutions
Department of Information Technology
3. Hierarchical Inheritance
When multiple child classes inherit properties from just a single base class.
Syntax
class A // Base class of B
{
// BODY OF THE PROGRAM
};
class B : access_specifier A // Derived class of A
{
// BODY OF THE PROGRAM
};
class C : access_specifier A // Derived class of A
{
// BODY OF THE PROGRAM
};
class D : access_specifier A // Derived class of A
{
// BODY OF THE PROGRAM
};
4. Multilevel Inheritance
This type of inheritance is the best way to represent the transitive nature of inheritance. In multilevel inheritance, a
derived class inherits all its properties from a class that itself inherited from another class.
Syntax
class A // Base class
{
// BODY OF CLASS A
};
class B : acess_specifier A // Derived class of A
{
// BODY OF CLASS B
};
class C : access_specifier B // Derived from derived class B
{
// BODY OF CLASS C
};
5. Hybrid Inheritance
This type of inheritance essentially combines more than two forms of inheritance , when a child class inherits from
multiple base classes all of its parent classes and that child class itself serves as a base class.
Syntax
class A
{
// BODY OF THE CLASS A
};
class B : public A
Chameli Devi Group of Institutions
Department of Information Technology
{
// BODY OF THE CLASS A
};
class C
{
// BODY OF THE CLASS A
};
class D : public B, public C
{
// BODY OF THE CLASS A
};
Derived class constructors:
A constructor plays a vital role in initializing an object. When both the derived and base class contains constructor, the
base constructor is executed first and then the constructor in the derived class is executed. In case of multiple
inheritance, the base class is constructed in the same order in which they appear in the declaration of the derived class.
Similarly, in a multilevel inheritance, the constructor will be executed in the order of inheritance.
The derived class takes the responsibility of supplying the initial values to its base class. The constructor of the derived
class receives the entire list of required values as its argument and passes them on to the base constructor in the order
in which they are declared in the derived class. A base class constructor is called and executed before executing the
statements in the body of the derived class.
The public inheritance makes public members of the base class public in the derived class, and the protected members
of the base class remain protected in the derived class.
Example of public inheritance:
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
Output
Private = 1
Protected = 2
Public = 3
Private inheritance
Private inheritance makes the public and protected members of the base class private in the derived class.
Example
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
Chameli Devi Group of Institutions
Department of Information Technology
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PrivateDerived : private Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access private member
int getPub() {
return pub;
}
};
int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Output
Private cannot be accessed.
Protected = 2
Public = 3