0% found this document useful (0 votes)
8 views

Unit 5

Uploaded by

wofeda8503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit 5

Uploaded by

wofeda8503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Object Oriented System Design

Unit 5
C++ Overloading (Function and Operator)
If we create two or more members having the
same name but different in number or type of
parameter, it is known as C++ overloading. In
C++, we can overload:
• methods,
• constructors, and
• indexed properties
Types of overloading in C++ are
• Function overloading
• Operator overloading
C++ Function Overloading Example
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C;
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}
#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);

int mul(int a,int b)


{
return a*b;
}
float mul(double x, int y)
{
return x*y;
}
int main()
{
int r1 = mul(6,7);
float r2 = mul(0.2,3);
std::cout << "r1 is : " <<r1<< std::endl;
std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
}
C++ Operators Overloading
• 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 redefines
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.
Operator that cannot be overloaded are
as follows:
• Scope operator (::)
• Sizeof
• member selector(.)
• member pointer selector(*)
• ternary operator(?:)
Syntax of Operator Overloading
return_type class_name : : operator op(argument_list)
{
// body of the function.
}
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(int c)
{
num=c;
}

void operator ++()


{
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt(10);
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(int i)
{
x=i;
}
void operator+(A a)
{
int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;
}

void display();
};
int main()
{
A a1(5);
A a2(4);
//a1+a2;
a1.operator+(a2);
return 0;
}
C++ 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.
• In C++, the class which inherits the members of another class
is called derived class and the class whose members are
inherited is called base class.
• The derived class is the specialized class for the base class.
Types Of Inheritance
C++ supports five types of inheritance:
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance
Derived Classes
A Derived class is defined as the class derived from the
base class.
The Syntax of Derived class:
class derived_class_name :: visibility-
mode base_class_name
{
// body of the derived class.
}
derived_class_name: It is the name of the
derived class.
visibility mode: The visibility mode specifies
whether the features of the base class are
publicly inherited or privately inherited. It can be
public or private.
base_class_name: It is the name of the base 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 also become
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.
Note:
• In C++, the default mode of visibility is
private.
• The private members of the base class are
never inherited.
C++ Single Inheritance
• Single inheritance is defined as the inheritance in
which a derived class is inherited from the only one
base class.

C++ Single Level Inheritance Example: Inheriting Fields
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
How to make a Private Member Inheritable
• The private member is not inheritable.
• If we modify the visibility mode by making it
public, but this takes away the advantage of
data hiding.
• C++ introduces a third visibility modifier,
i.e., protected.
• The member which is declared as protected will
be accessible to all the member functions within
the class as well as the class immediately
derived from it.
Visibility modes can be classified into
three categories
• Public: When the member is declared as public,
it is accessible to all the functions of the
program.
• Private: When the member is declared as
private, it is accessible within the class only.
• Protected: When the member is declared as
protected, it is accessible within its own class as
well as the class immediately derived from it.
Multilevel Inheritance
Multilevel inheritance is a process of deriving a class
from another derived class.
When one class inherits another class which is further inherited by
another class, it is known as multi level inheritance in C++. Inheritance
is transitive so the last derived class acquires all the members of all its
base classes.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Multiple Inheritance
• Multiple inheritance is the process of deriving a new
class that inherits the attributes from two or more
classes.
C++ Hybrid Inheritance
• Hybrid inheritance is a combination of more than one
type of inheritance.
C++ Hierarchical Inheritance
• Hierarchical inheritance is defined as the process of
deriving more than one class from a base class.
C++ Function Overriding
• If derived class defines same function as defined
in its base class, it is known as function
overriding in C++.
• It is used to achieve runtime polymorphism.
• It enables you to provide specific
implementation of the function which is already
provided by its base class.
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{
cout<<"Eating bread...";
}
};
• int main()
• {
• Dog d = Dog();
• d.eat();
• return 0;
•}
C++ virtual function
• A C++ virtual function is a member function in the base class
that you redefine in a derived class. It is declared using the
virtual keyword.
• It is used to tell the compiler to perform dynamic linkage or late
binding on the function.
• There is a necessity to use the single pointer to refer to all the
objects of the different classes. So, we create the pointer to the
base class that refers to all the derived objects.
• But, when base class pointer contains the address of the derived
class object, always executes the base class function. This issue
can only be resolved by using the 'virtual' function.
• A 'virtual' is a keyword preceding the normal declaration of a
function.
• When the function is made virtual, C++ determines which
function is to be invoked at the runtime based on the type of the
object pointed by the base class pointer.
Late binding or Dynamic linkage
In late binding function call is resolved during runtime.
Therefore compiler determines the type of object at
runtime, and then binds the function call.
Rules of Virtual Function
• Virtual functions must be members of some class.
• Virtual functions cannot be static members.
• They are accessed through object pointers.
• A virtual function must be defined in the base class,
even though it is not used.
• The prototypes of a virtual function of the base class
and all the derived classes must be identical.
• If the two functions with the same name but different
prototypes, C++ will consider them as the overloaded
functions.
Pure Virtual Function
• A virtual function is not used for performing any task. It
only serves as a placeholder.
• When the function has no definition, such function is known
as "do-nothing" function.
• The "do-nothing" function is known as a pure virtual
function. A pure virtual function is a function declared in
the base class that has no definition relative to the base
class.
• A class containing the pure virtual function cannot be used
to declare the objects of its own, such classes are known as
abstract base classes.
• The main objective of the base class is to provide the traits
to the derived classes and to create the base pointer used
for achieving the runtime polymorphism.
virtual void display() = 0;
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class.";
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
In the above example, the base class contains the pure virtual function.
Therefore, the base class is an abstract base class. We cannot create
the object of the base class.
POINTER TO OBJECTS
• A Variable That Holds an Address value is called a
Pointer variable or simply pointer.
• We already know about pointer that's point to Simple
data types likes int, char, float etc.
• So similar to these type of data type, Objects can also
have an address, so there is also a pointer that can
point to the address of an Object, This Pointer is
Known as This Pointer.
• Every Object in C++ has access to its own address
through an important pointer This Pointer.
• The This Pointer is an implicit parameter to all
member functions
• Therefore, inside a member function, this may be
used to refer to the invoking object.
• Whenever a member function is called, it is
automatically passed an implicit arguments that is
This pointer to the invoking object (i.e. The object on
which the function is invoked).
• The This pointer is passed as a hidden argument to all
Nonstatic member function calls and is available as a
local variable within the body of all Nonstatic
functions.
• This Pointer is a constant pointer that holds the
memory address of the current object.
• This pointer is not available in static member
functions as static member functions can be called
without any object (with class name).
#include<iostream>
using namespace std;
class Point
{
private:
int x;
public:
void setx(int a) //simple function.
{
this->x=a;
}
};
main()
{
Point obj1; //1st Object of Class Point.
Point obj2; //2nd Object of Class point.
obj1.setx(10); //function calling using 1st object.
obj2.setx(30); //function calling using 2nd object.
C++ Declaration and Uses of Object Pointers
• Just like Other Pointers, the object pointers are declared by placing in
front of a object pointer's name.
• The Syntax is:
• class_name * Object_pointer_name;
• In above Syntax, class_Name is the name of an already defined class
and object_pointer_name is the pointer to an object of this class
type.
• For example: To declared an Pointer ptr as an object pointer of class
Date, We shall write:
• Date * ptr;
#include<iostream>
using namespace std;
class Date
{
private:
short int dd, mm, yy;
public:
Date() //constrctor:
{
dd = mm = yy = 0;
}
void getdata(int i, int j, int k)
{
dd = i;
mm = j;
yy = k;
}
void prndata(void)
{
cout<<"\nData is "<<dd<<"/"<<mm<<"/"<<yy<<"\n";
}
};
main()
{
Date D1; //simple object having type Data:
Date *dptr; //Pointer Object having type Date:
cout<<"Initializing data members using the object, with values 19, 10, 2016"<<endl;
D1.getdata(19,10,2016);
cout<<"Printing members using the object ";
D1.prndata();
dptr = &D1;
cout<<"Printing members using the object pointer ";
dptr->prndata();
cout<<"\nInitializing data members using the object pointer, with values 20, 10, 2016"<<endl;
dptr->getdata(20, 10, 2016);
cout<<"printing members using the object ";
D1.prndata();
cout<<"Printing members using the object pointer ";
dptr->prndata();
return 0;
}
Note
To Access public members using an object the
Operator is used, and to access public members
using an Object pointer, the Arrow -> operator is
used.
Static Keyword in C++
• Static keyword has different meanings when used with
different types. We can use static keyword with:
• Static Variables : Variables in a function, Variables in a
class
Static Members of Class : Class objects and Functions
in a class
Static variables in a Function
When a variable is declared as static, space for it gets
allocated for the lifetime of the program.
Even if the function is called multiple times, space for
the static variable is allocated only once and the value
of variable in the previous call gets carried through the
next function call.
#include <iostream>
#include <string>
using namespace std;
void demo()
{
// static variable
static int count = 0;
cout++;
cout << count << " ";
}
int main()
{
for (int i=0; i<5; i++)
demo();
return 0;
}
Static variables in a class:
• As the variables declared as static are initialized only
once as they are allocated space in separate static
storage so, the static variables in a class are shared by
the objects.
• There can not be multiple copies of same static
variables for different objects.
• Also because of this reason static variables can not be
initialized using constructors.
#include<iostream>
using namespace std;
class GfG
{
public:
static int i;
GfG()
{

}
};
int GfG::i = 1;
int main()
{
GfG obj;
cout <<obj.i;
return 0;
}
Class objects as static:
• Just like variables, objects also when declared as static
have a scope till the lifetime of program.
• If the object is declared inside the if block as non-
static.
• So, the scope of variable is inside the if block only.
• So when the object is created the constructor is
invoked and soon as the control of if block gets over
the destructor is invoked as the scope of object is
inside the if block only where it is declared.
#include<iostream>
using namespace std;
class GfG
{
int i = 0;
public:
GfG()
{
i = 0;
cout << "Inside Constructor\n";
}
~GfG()
{
cout << "Inside Destructor\n";
}
};
int main()
{
int x = 0;
if (x==0)
{
static GfG obj;
}
cout << "End of main\n";
}
output
Inside Constructor
End of main
Inside Destructor
Static functions in a class
• Just like the static data members or static variables inside the
class, static member functions also does not depend on
object of class.
• It is recommended to invoke the static members using the
class name and the scope resolution operator.
• Static member functions are allowed to access only the static
data members or other static member functions, they can
not access the non-static data members or member
functions of the class.
#include<iostream>
using namespace std;
class GfG
{
public:
static void printMsg()
{
cout<<"Welcome to GfG!";
}
};
int main()
{
// invoking a static member function
GfG::printMsg();
}
Output
#include<iostream>
using namespace std;
class Test
{
static int i;
int j;
};
int main()
{
cout << sizeof(Test);
return 0;
}
Output: 4 (size of integer)
static data members do not contribute in size of
an object. So ‘i’ is not considered in size of Test.
Also, all functions (static and non-static both) do
not contribute in size.
C++ Polymorphism
• The term "Polymorphism" is the combination of "poly" +
"morphs" which means many forms.
• It is a greek word. In object-oriented programming, we use 3
main concepts: inheritance, encapsulation, and
polymorphism
There are two types of polymorphism in C++
Compile time polymorphism
• The overloaded functions are invoked by matching the type
and number of arguments.
• This information is available at the compile time and,
therefore, compiler selects the appropriate function at the
compile time.
• It is achieved by function overloading and operator
overloading which is also known as static binding or early
binding.
• Now, let's consider the case where function name and
prototype is same.
Run time polymorphism
• Run time polymorphism is achieved when the object's
method is invoked at the run time instead of compile
time.
• It is achieved by method overriding which is also
known as dynamic binding or late binding.
Differences b/w compile time and run time polymorphism
C++ Runtime Polymorphism Example
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
Runtime Polymorphism with Data Members
#include <iostream>
using namespace std;
class Animal { // base class declaration.
public:
string color = "Black";
};
class Dog: public Animal // inheriting Animal class.
{
public:
string color = "Grey";
};
int main(void) {
Animal d= Dog();
cout<<d.color;
}

You might also like