C++ and
object
oriented
programmin
PREPARED BY :
g BRIJESH KHODA
Prepared by
Brijesh Khoda
Unit - 3
Operator Overloading & inheritance
Operator Overloading
“Operator overloading means giving special meaning to an existing
operator in C++ without changing its original meaning”.
Operator overloading is a compile-time polymorphism.
We know that, operators are used with built in data types like int,
float etc. if we use these operators with user defined data types,
compiler will generates an error.
By using operator overloading, we can apply operators with user
defined data types like objects.
For example, we can overload an operator ‘+’ in a class like String
so that we can concatenate two strings by just using +.
So the main idea behind “Operator overloading” is to use C++
operators with class variables or class objects.
Operator functions are the same as normal functions. The only
differences are, that the name of an operator function is always
the operator keyword followed by the symbol of the operator,
and operator functions are called when the operator is used.
Syntax: return_type operator op(arguments)
{ ………….. }
Rules for Operator
Overloading
• Only built-in operators can be overloaded. we cannot
overload newly added operators.
• The arity (no of argu) of the operators cannot be changed
• The precedence of the operators remains same.
• The overloaded operator cannot hold the default
parameters except function call operator “()”.
• We cannot overload operators for built-in data types. At
least one user defined data types must be there.
• The assignment “=”, subscript “[]”, function call “()” and
arrow operator “->” these operators must be defined as
member functions, not the friend functions.
• Some operators like assignment “=”, address “&” and
comma “,” are by default overloaded.
Types of operator
overloading
Operator Overloading is a special function to
change the current functionality of some
operators.
The following four operators can not be
overloaded.
(1) sizeof operator
(2) conditional operator
(3) pointer to member operator
(4) scope resolution operator
Following are two types of operators which can be
overloaded.
Unary operators
Binary operators
OVERLOADING UNARY OPERATOR
In the unary operator function, no arguments should be passed. It
works only with one class object. It is the overloading of an
operator operating on a single operand.
Syntax: op object;
Example: ++ob1;
Function: void operator ++()
++, -- and – are 3 unary operators which can be overloaded.
These are used with single object only.
OVERLOADING BINARY OPERATOR:
Binary operators works with two values. So, in binary operator
function only one object is passed as an argument and this
function is called by second object.
Syntax: object1 op object2;
Example: ob1 + ob2;
Function: classname operator + (ob2)
Operator overloading using
friend function
If we want to define operator function outside the
class, we have to define that function as a “friend
function” of that class.
This overloaded friend function allows us to
access private members of class, and we can
apply operator on them.
But we know that friend function is not a member
of class. So it can not be called by any object.
Soin this friend function, we have to pass one
object for unary operator and two arguments for
binary operator overloading.
Syntax: ret_type operator op(parameters);
Inheritance
“Acquiring the properties of one class into another class is called
inheritance.”
OR
The ability of one class to derive the properties of another class is
called inheritance.”
Inheritance is one of the most important features of Object
Oriented Programming in C++.
Syntax:
Class derived_class_name : access_specifier base_class_name
{ //body
};
Followings are 5 types of inheritance:
(1)single inheritance
(2) multi level inheritance
(3) multiple inheritance
(4) hierarchical inheritance
(5) hybrid inheritance
•Single inheritance
A derived class inherits from a single base class. This is the simplest
type of inheritance.
•Multilevel inheritance
A derived class inherits from another derived class, forming a direct
line of inheritance from the base class.
For example, if the "mammal" class inherits from the "animal" class,
and the "cow" class inherits from the "mammal“ class, then the
"mammal" class can implement the functionality of the "animal" class,
and the "cow" class can implement the functionality of the "mammal"
class.
•Multiple inheritance
A class inherits from more than one base class, combining their
features into a single class. For example, if class C inherits from both
classes A and B, then class C will have all the properties and methods
of both classes A and B.
•Hierarchical inheritance
More than one derived class inherits from a single base class. All the
derived classes inherit all the properties of the base class.
•Hybrid inheritance
A combination of multiple inheritance and multilevel inheritance. A
class is derived from two classes, but one of the parent classes is a
derived class, not a base class.
How to define Derived
class
To define a derived class in C++, you can use the following
syntax:
Class derived_class : access_specifier base_classname
{ //body
}
• class: keyword to create a new class
• derived_class_name: name of the new class, which will inherit
the base class
• access-specifier: Specifies the access mode which can be either
of private, public or protected. If neither is specified, private is
taken as default.
• base-class-name: name of the base class.
A derived class, also known as a subclass or child class, is a class that
inherits properties and behaviors from a base class
. A derived class can:
•Inherit all the instance variables and methods of the base class
•Have its own methods and variables in addition to the base class
•Restrict the visibility of base class members
•Access the base class members without restriction unless the
members are private
Constructor in derived
class
• We can use constructors in derived classes in C++
• If the base class constructor does not have any arguments, there is no
need for any constructor in the derived class
• But if there are one or more arguments in the base class constructor,
derived class need to pass argument to the base class constructor
• If both base and derived classes have constructors, base class
constructor is executed first.
• Syntax:
• Derived_constructor (argu) : base1_const(argu) , base2_const(argu)
• { //body
• }
• C++ supports a special syntax for passing arguments to multiple base
classes
• The constructor of the derived class receives all the arguments at once
and then will pass the call to the respective base classes
• The body is called after the constructors is finished executing
Virtual base class
Virtual base classes are used in virtual inheritance in a way of
preventing multiple “instances” of a given class appearing in an
inheritance hierarchy when using multiple inheritances.
For example : Consider the situation where we have one
class A . This class A is inherited by two other classes B and C.
Both these class are inherited into another in a new class D as
shown in figure below.
As we can see from the figure that data members/function of class A are
inherited twice to class D. One through class B and second through
class C. When any data / function member of class A is accessed by an
object of class D, ambiguity arises as to which data/function member
would be called? One inherited through B or the other inherited
through C. This confuses compiler and it displays error.
For this reason, we have to inherit class A virtually in B and C.
Class B : virtual public A
{ //body
};
Class C : virtual public A
{ //body
};
virtual can be written before or after the public. Now only one copy of
data/function member will be copied to class C and class B and
class A becomes the virtual base class. When a base class is specified as
a virtual base, it can act as an indirect base more than once without
duplication of its data members. A single copy of its data members is
shared by all the base classes that use virtual base.
Containership
We can create an object of one class into another and that object
will be a member of the class. This type of relationship between
classes is known as containership or has_a relationship as one
class contain the object of another class. And the class which
contains the object and members of another class in this kind of
relationship is called a container class.
The object that is part of another object is called contained
object, whereas object that contains another object as its
part or attribute is called container object.