OOPs with C++
Ms. Sudhriti Sengupta
&
Dr. Lavanya Sharma
OOPS
Object-Oriented Programming or OOPs refers to
languages that uses objects in programming.
Object-oriented programming aims to
implement real-world entities like inheritance,
hiding, polymorphism etc in programming. The
main aim of OOP is to bind together the data
and the functions that operate on them so that
no other part of the code can access this data
except that function.
Principle of OOPS
• Object
This is the basic unit of object oriented
programming. That is both data and function that
operate on data are bundled as a unit called as
object.
• Class
When you define a class, you define a blueprint for
an object. This doesn't actually define any data, but
it does define what the class name means, that is,
what an object of the class will consist of and what
operations can be performed on such an object.
• Abstraction
Data abstraction refers to, providing only essential
information to the outside world and hiding their
background details, i.e., to represent the needed
information in program without presenting the details.
For example, a database system hides certain details of
how data is stored and created and maintained. Similar
way, C++ classes provides different methods to the
outside world without giving internal detail about those
methods and data.
Encapsulation
Encapsulation is placing the data and the
functions that work on that data in the same
place. While working with procedural languages,
it is not always clear which functions work on
which variables but object-oriented
programming provides you framework to place
the data and the relevant functions together in
the same object.
Inheritance
One of the most useful aspects of object-
oriented programming is code reusability. As the
name suggests Inheritance is the process of
forming a new class from an existing class that is
from the existing class called as base class, new
class is formed called as derived class.
This is a very important concept of object-
oriented programming since this feature helps
to reduce the code size.
Important terminology:Inheritance
•
Super Class: The class whose features are inherited is
known as superclass(or a base class or a parent class).
• Sub Class: The class that inherits the other class is known
as subclass(or a derived class, extended class, or child
class). The subclass can add its own fields and methods in
addition to the superclass fields and methods.
• Reusability: Inheritance supports the concept of
“reusability”, i.e. when we want to create a new class and
there is already a class that includes some of the code that
we want, we can derive our new class from the existing
class. By doing this, we are reusing the fields and methods
of the existing class.
Polymorphism
The ability to use an operator or function in
different ways in other words giving different
meaning or functions to the operators or
functions is called polymorphism. Poly refers to
many. That is a single function or an operator
functioning in many ways different upon the
usage is called polymorphism.
Introduction to C++
C++ is a middle-level programming language
developed by Bjarne Stroustrup starting in 1979
at Bell Labs. C++ runs on a variety of platforms,
such as Windows, Mac OS, and the various
versions of UNIX.
Use of C++
• C++ is used by hundreds of thousands of programmers
in essentially every application domain.
• C++ is being highly used to write device drivers and
other software that rely on direct manipulation of
hardware under realtime constraints.
• C++ is widely used for teaching and research because it
is clean enough for successful teaching of basic
concepts.
• Anyone who has used either an Apple Macintosh or a
PC running Windows has indirectly used C++ because
the primary user interfaces of these systems are
written in C++.
Points to NOTE
• C++ is a statically typed, compiled, general-
purpose, case-sensitive, free-form programming
language that supports procedural, object-
oriented, and generic programming.
• C++ is regarded as a middle-level language, as it
comprises a combination of both high-level and
low-level language features.
• C++ is a superset of C, and that virtually any legal
C program is a legal C++ program.
Polymorhpism
The word polymorphism means having many
forms. In simple words, we can define polymorphism as
the ability of a message to be displayed in more than one
form.
Real life example of polymorphism, a person at the
same time can have different characteristic. Like a lady at
the same time is a mother, a daughter, a teacher etc . So
the same person posses different behavior in different
situations. This is called polymorphism.
Polymorphism is considered as one of the important
features of Object Oriented Programming.
• 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.
Compile time polymorphism: This type of
polymorphism is achieved by function
overloading or operator overloading.
Function Overloading: When there are multiple
functions with same name but different
parameters then these functions are said to
be overloaded. Functions can be overloaded
by change in number of
arguments or/and change in type of arguments.
Operator Overloading: C++ also provide option
to overload operators. For example, we can
make the operator (‘+’) for string class to
concatenate two strings. We know that this is
the addition operator whose task is to add two
operands. So a single operator ‘+’ when placed
between integer operands , adds them and
when placed between string operands,
concatenates them.
• 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.
Runtime polymorphism: This type of
polymorphism is achieved by Function
Overriding.Function overriding on the other
hand occurs when a derived class has a
definition for one of the member functions of
the base class. That base function is said to
be overridden.
Compile time polymorphism Run time polymorphism
The function to be invoked is
known at the compile time.
The function to be invoked is
known at the run time.
It is also known as overloading,
early binding and static binding.
It is also known as overriding,
Dynamic binding and late binding.
Overloading is a compile time
polymorphism where more than
one method is having the same
name but with the different
number of parameters or the type
of the parameters.
Overriding is a run time
polymorphism where more than
one method is having the same
name, number of parameters and
the type of the parameters.
It is achieved by function
overloading and operator
overloading.
It is achieved by virtual functions
and pointers.
It provides fast execution as it is
known at the compile time.
It provides slow execution as it is
known at the run time.
It is less flexible as mainly all the
things execute at the compile
time.
It is more flexible as all the things
execute at the run time.
Simple program for function
overloading
• // Program to compute absolute value
• // Works both for integer and float
• #include <iostream>
• using namespace std;
• int absolute(int);
• float absolute(float);
• int main() {
• int a = -5;
• float b = 5.5;
•
• cout << "Absolute value of " << a << " = " << absolute(a) << endl;
• cout << "Absolute value of " << b << " = " << absolute(b);
• return 0;
• }
• int absolute(int var) {
• if (var < 0)
• var = -var;
• return var;
• }
• float absolute(float var){
• if (var < 0.0)
• var = -var;
• return var;
• }

C++ first s lide

  • 1.
    OOPs with C++ Ms.Sudhriti Sengupta & Dr. Lavanya Sharma
  • 2.
    OOPS Object-Oriented Programming orOOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
  • 4.
    Principle of OOPS •Object This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object. • Class When you define a class, you define a blueprint for an object. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
  • 5.
    • Abstraction Data abstractionrefers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details. For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data.
  • 7.
    Encapsulation Encapsulation is placingthe data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.
  • 9.
    Inheritance One of themost useful aspects of object- oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class. This is a very important concept of object- oriented programming since this feature helps to reduce the code size.
  • 10.
    Important terminology:Inheritance • Super Class:The class whose features are inherited is known as superclass(or a base class or a parent class). • Sub Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods. • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
  • 12.
    Polymorphism The ability touse an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.
  • 14.
    Introduction to C++ C++is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
  • 15.
    Use of C++ •C++ is used by hundreds of thousands of programmers in essentially every application domain. • C++ is being highly used to write device drivers and other software that rely on direct manipulation of hardware under realtime constraints. • C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts. • Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++.
  • 16.
    Points to NOTE •C++ is a statically typed, compiled, general- purpose, case-sensitive, free-form programming language that supports procedural, object- oriented, and generic programming. • C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. • C++ is a superset of C, and that virtually any legal C program is a legal C++ program.
  • 17.
  • 18.
    The word polymorphismmeans having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Real life example of polymorphism, a person at the same time can have different characteristic. Like a lady at the same time is a mother, a daughter, a teacher etc . So the same person posses different behavior in different situations. This is called polymorphism. Polymorphism is considered as one of the important features of Object Oriented Programming.
  • 20.
    • Compile timepolymorphism: 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.
  • 21.
    Compile time polymorphism:This type of polymorphism is achieved by function overloading or operator overloading. Function Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
  • 22.
    Operator Overloading: C++also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands , adds them and when placed between string operands, concatenates them.
  • 23.
    • Run timepolymorphism: 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.
  • 24.
    Runtime polymorphism: Thistype of polymorphism is achieved by Function Overriding.Function overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
  • 25.
    Compile time polymorphismRun time polymorphism The function to be invoked is known at the compile time. The function to be invoked is known at the run time. It is also known as overloading, early binding and static binding. It is also known as overriding, Dynamic binding and late binding. Overloading is a compile time polymorphism where more than one method is having the same name but with the different number of parameters or the type of the parameters. Overriding is a run time polymorphism where more than one method is having the same name, number of parameters and the type of the parameters. It is achieved by function overloading and operator overloading. It is achieved by virtual functions and pointers. It provides fast execution as it is known at the compile time. It provides slow execution as it is known at the run time. It is less flexible as mainly all the things execute at the compile time. It is more flexible as all the things execute at the run time.
  • 26.
    Simple program forfunction overloading • // Program to compute absolute value • // Works both for integer and float • #include <iostream> • using namespace std; • int absolute(int); • float absolute(float); • int main() { • int a = -5; • float b = 5.5; • • cout << "Absolute value of " << a << " = " << absolute(a) << endl; • cout << "Absolute value of " << b << " = " << absolute(b); • return 0; • } • int absolute(int var) { • if (var < 0) • var = -var; • return var; • } • float absolute(float var){ • if (var < 0.0) • var = -var; • return var; • }