by Muskan-1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

OBJECT ORIENTED PROGRAMMING

DEVELOPMENT

 By:
Muskan Jain
Roll no :1221256
Branch : Computer Science
Engineering

1
WHY WE SHOULD STUDY C++

 Why do we need C++ when we already have an efficient


language: C.
 The reason behind inventing C++ was an increasing
complexity.
 When we talk of real life projects, it becomes difficult to write
an effective code with limited lines of code, and as soon as the
loc exceeds25000 to 100000 lines of code, it becomes complex
to handle and manage the project with C language.

2
C++, as the name suggests, is a superset of C. As a matter
of fact, C++ can run most of C code while C cannot run C+
+ code
FUNCTIONS
 Function is a sub program which performs logically isolated
task
 Creation of function is must for easy readability & easy
maintenance of program

4
What is Procedural Programming? [Definition]
Procedural Programming may be the first programming paradigm that
a new developer will learn. Fundamentally, the procedural code is the
one that directly instructs a device on how to finish a task in logical
steps Procedural Programming divides the program into procedures,
which are also known as routines or functions, simply containing a
series of steps to be carried out.
OBJECT ORIENTED PROGRAMMING

 OOP took the best ideas of structured program & combine them
with several new concepts.

 Program can be organized in one of the two ways:

 Around its code (what is happening)= STRUCTURED APPROACH

 Around its data (who is being affected)= OOP APPROACH

6
STRIKING FEATURES OF OOP
 Programs are divided into what are known as objects.
 Data is hidden & cannot be accessed by external functions.

 Objects may communicate with each other through functions.

 New data & functions can be easily added whenever


necessary.
 Follows bottom up approach in program design.

 OOP makes it possible to create full reusable applications with


less code and shorter development time

7
EXAMPLE
 In any company one has to deal with number of entities at a time
such as:
 Employees [25 functions]
 Customers [20 functions]
 Vendors [15 functions]
 Sales [10 functions]
 Purchases [20 functions]
 Therefore total of 90 functions, which are supposed to be
designed.
 If we use C, we will have to form all the different sub programs
for all the 90 functions.
 This drawback is removed by OOP approach (in SPA, there is
great redundancy).
8
OOPS FEATURES ?
 Class
 Object

 Encapsulation

 Abstraction

 Polymorphism

 Inheritance

9
CLASS ?
 Classes are user-defined (programmer-defined) types.
 A class is a collection of objects that have identical properties,
common behaviour and shared relationship.
 Once the class is defined any number of objects of that class is
created.
 For example planets, sun and moon are the members of the
solar system class.

10
 A Class : is a way to bind the data and its associated functions
together. All the elements of a class are private by default, even
elements can be declared as public or protected. An object is an
instance of a class.
 Syntax:
class class-name
{
access:specifier
private data and functions
}
In the above syntax the every class has a unique name, the
"access:specifier" can either private, public, protected. The
"protected" is used when inhertinace is applied.
OBJECT Objects are the basic runtime entities in an object-oriented
system. They may represent a person, a place, a bank
account, a table of data or any item that the program has to
handle.

Fruits

12
Object Class
 Teacher : Faculty
 Table : furniture
 Bikes : Vehicle

 For example: mango, apple, orange are members of class fruit.


If fruit has been defined as a class, then the statement
 fruit mango

 will create an object mango belonging to the class fruit.

13
3. ENCAPSULATION
 Wrapping of data and functions together as a single unit is
known as encapsulation. By default data is not accessible to
outside world and they are only accessible through the
functions which are wrapped in a class.

 The major benefit of data encapsulation is the security of the


data. It protects the data from unauthorized users that we
inferred from the above stated real-real problem.
4. DATA ABSTRACTION IN C++
 Data Abstraction is a process of providing only the essential
details to the outside world and hiding the internal details, i.e.,
representing only the essential details in the program.

 Let's take a real life example of AC, which can be turned ON or


OFF, change the temperature, change the mode, and other external
components such as fan, swing. But, we don't know the internal
details of the AC, i.e., how it works internally. Thus, we can say
that AC seperates the implementation details from the external
interface.
ADVANTAGES OF ABSTRACTION:
 Implementation details of the class are protected from the
inadvertent user level errors.

 The main aim of the data abstraction is to reuse the code


and the proper partitioning of the code across the classes.

 Internal implementation can be changed without affecting


the user level code.
5. POLYMORPHISM IN C++

 In C++, polymorphism causes a member function to


behave differently based on the object that calls/invokes
it. Polymorphism is a Greek word that means to have
many forms. It occurs when you have a hierarchy of
classes related through inheritance.
 For example, suppose we have the function
makeSound(). When a cat calls this function, it will
produce the meow sound. When a cow invokes the same
function, it will provide the moow sound.
Types of Polymorphism
•Compile-time polymorphism, and
•Runtime polymorphism.

Compile Time Polymorphism


You invoke the overloaded functions by matching the number and type
of arguments. The information is present during compile-time. This
means the C++ compiler will select the right function at compile time.
Compile-time polymorphism is achieved through function overloading
and operator overloading.
1. Function Overloading
Function overloading occurs when we have many functions with similar
names but different arguments. The arguments may differ in terms of
number or type.
2. Operator Overloading
In Operator Overloading, we define a new meaning for a C++ operator. It also
changes how the operator works. For example, we can define the + operator to
concatenate two strings. We know it as the addition operator for adding
numerical values. After our definition, when placed between integers, it will add
them. When placed between strings, it will concatenate them.
 Runtime polymorphism
 In a Runtime polymorphism, functions are called at the time the program
execution. Hence, it is known as late binding or dynamic binding.
 1. Function overriding
 Function overriding is a part of runtime polymorphism. In function overriding,
more than one method has the same name with different types of the parameter
list.
 It is achieved by using virtual functions and pointers. It provides slow execution
as it is known at the run time. Thus, It is more flexible as all the things executed
at the run time.
 In function overriding, we give the new definition to base class function in the
derived class. At that time, we can say the base function has been overridden. It
can be only possible in the ‘derived class’. In function overriding, we have two
definitions of the same function, one in the superclass and one in the derived
class. The decision about which function definition requires calling happens at
runtime. That is the reason we call it ‘Runtime polymorphism’.
6. 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.
 #include<iostream>
 using namespace std;

 class person //declare a class


 { int main()
 char name[30]; //class data {
 int age; //class data person p; //definition object of
 public:
class
 void getdata (void); //member function
p.getdata();
 void display (void); //member function
p.display(); //call number
 };
function
 void person::getdata(void) //definition of a function
 {
return 0;
 cout<< "enter name:"; }
 cin>> name;
 cout<< "enter age";
 cin>>age;
 }
 void person :: display (void) //definition of a function
 {
 cout<< "In name:" <<name;
 cout<< "In age:" << age;
 }
ACCESS
SPECIFIERS
 Public specifier: When the members are declared as
public, members can be accessed anywhere from the
program.
 Private specifier: When the members are declared as
private, members can only be accessed only by the
member functions of the class.
 protected - members cannot be accessed from outside
the class, however, they can be accessed in inherited
classes
C++ FUNCTION OVERLOADING

 Function Overloading is defined as the process of having


two or more function with the same name, but different
in parameters is known as function overloading in C++.
In function overloading, the function is redefined by
using either different types of arguments or a different
number of arguments. It is only through these differences
compiler can differentiate between the functions.
 The advantage of Function overloading is that it
increases the readability of the program because you
don't need to use different names for the same action.
C++ NAMESPACES

 Namespaces in C++ are used to organize too many classes so that it


can be easy to handle the application.
 For accessing the class of a namespace, we need to use
namespacename::classname. We can use using keyword so that we
don't have to use complete name all the time.
 In C++, global namespace is the root namespace. The global::std
will always refer to the namespace "std" of C++ Framework.
THE NEED FOR NAMESPACE
 Consider a situation, we are writing some code that has a
function called abc() and there is another predefined library
available which is also having same function abc(). Now at the
time of compilation, the compiler has no clue which version of
abc() function we are referring to within our code.
 To overcome this difficulty namespace is designed and is used
as additional information to differentiate similar functions,
variables, classes, etc. with the same name available in different
libraries. Using namespace, we can define the context in which
names are defined. In essence, a namespace defines a scope.
 All C++ standard library types and functions are declared in the
std namespace or namespaces nested inside std thus it is widely
used in most of the programs.
 #include <iostream>
 using namespace std;
 namespace first_function {
 void func() {
 cout << "Inside first_function" << endl;
 }}
 namespace second_function {
 void func() {
 cout << "Inside second_function" << endl;
 }}

 int main () {
 first_function::func();
 second_function::func();
 return 0;
 }
HEADER FILES
 Header files contain definitions of Functions and
Variables, which is imported or used into any C++
program by using the pre-processor #include statement.
Header file have an extension ".h" which contains C++
function declaration and macro definition.
 Each header file contains information (or declarations) for a particular group
of functions. Like stdio.h header file contains declarations of standard input
and output functions available in C++ which is used for get the input and
print the output. Similarly, the header file math.h contains declarations of
mathematical functions available in C++.
 Types of Header Files in C++
 System header files: It is comes with compiler.
 User header files: It is written by programmer.
 Why need of header files

You might also like