Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 86

Object Oriented Programming[210243]

SE Computer Engineering

UNIT-I
Fundamentals of Object Oriented Programming
Unit Contents
Introduction to object-oriented programming, Need of object-oriented
programming, Fundamentals of object-oriented programming:
Namespaces, objects, classes, data members, methods, messages,
data encapsulation, data abstraction and information hiding,
inheritance, polymorphism. Benefits of OOP, C++ as object oriented
programming language.
C++ Programming- C++ programming Basics, Data Types, Structures,
Enumerations, control structures, Arrays and Strings, Class, Object,
class and data abstraction, Access specifiers, separating interface from
implementation.
Functions- Function, function prototype, accessing function and utility
function, Constructors and destructor, Types of constructor, Objects
and Memory requirements, Static members: variable and functions,
inline function, friend function.
Introduction to OOP paradigm
• Object-oriented programming (OOP) is a
programming paradigm based on the concept
of "objects", which can contain data, in the
form of fields (often known as attributes or
properties), and code, in the form of
procedures (often known as methods or
functions).
Introduction to Procedure-Oriented
Programming
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs
known as functions.
• Most of the functions share global data.
• Data move openly around the system from
function to function.
• Functions transform data from one form to
another.
• Employs top-down approach in program design.
Procedure-Oriented Programming

Relationship of data and functions in POP


Limitations of Procedural
Programming
• The program code is harder to write
• The Procedural code is often not reusable, which
may pose the need to recreate the code if is
needed to use in another application
• Difficult to relate with real-world objects
• The importance is given to the operation rather
than the data, which might pose issues in some
data-sensitive cases
• The data is exposed to the whole program,
making it not so much security friendly
Object-Oriented Programming Benefits
• OOP mimics the real world, making it easier to
understand
• OOP codes are reusable in other programs
• Security is offered due to the use of data
hiding and abstraction mechanism
• Due to modularity and encapsulation, OOP
offers ease of management
OOP Paradigms

Organization of Data and Functions in OOP


Fundamentals of object oriented
Programming
Namespaces:
• A namespace is a declarative region that provides
a scope to the identifiers (the names of types,
functions, variables, etc) inside it.
• Namespaces are used to organize code into
logical groups and to prevent name collisions that
can occur especially when your code base
includes multiple libraries.
• eg. using namespace std;
Here std is the namespace where ANSI C++
standard class libraries are defined.
Fundamentals of object oriented
Programming
• Defining namespace
syntax-
namespace namespace_name
{
// code declarations
}
Fundamentals of object oriented
Programming
#include <iostream> int main ()
using namespace std; {
// first name space // Calls function from first name space.
namespace first_space
first_space::func();
{
void func() {
// Calls function from second name space.
cout << "Inside first_space" << endl;
} second_space::func();
}
// second name space return 0;
namespace second_space }
{
void func() {
cout << "Inside second_space" << endl;
}
}
Fundamentals of object oriented
Programming
Objects:
• Objects are the basic run time entities in an
object-oriented system.
• When a program is executed, the objects
interact by sending messages to one another.
• Objects take up space in the memory.
• We can create ‘n’ number of objects belonging
to particular class
Object Example
class example
{
-------
-------
};
int main()
{
example obj; //object of class example
}
Fundamentals of object oriented
Programming
Classes:
• A class in C++ is the building block, that leads
to Object-Oriented programming.
• It is a user-defined data type, which holds its
own data members and member functions,
which can be accessed and used by creating
an instance of that class.
• A C++ class is like a blueprint for an object.
Fundamentals of object oriented
Programming
Classes:
Fundamentals of object oriented
Programming
Data Members:
• Data members include members that are
declared with any of the fundamental types,
as well as other types, including pointer,
reference, array types, bit fields, and user-
defined types.
Fundamentals of object oriented
Programming
• Data Members:
Fundamentals of object oriented
Programming
Methods:
• Methods are functions that belongs to the
class.
• There are two ways to define functions that
belongs to a class:
– Inside class definition.
– Outside class definition.
Inside class definition
class example
{
public:
void add() //method defined inside class
{
------
------
}
};

Int main()
{
example obj;
obj.add();
return 0;
}
Outside class definition
class example
{
public:
void add() //method declared inside class
};
void example::add() //method defined outside class
{
------
------
}
Int main()
{
example obj;
obj.add();
return 0;
}
Fundamentals of object oriented
Programming
Messages:
• Objects communicate with one another by
sending and receiving information to each other.
• A message for an object is a request for execution
of a procedure and therefore will invoke a
function in the receiving object that generates
the desired results.
e.g. employee.salary(name)
(object) (Message) (information)
Fundamentals of object oriented
Programming
Data Encapsulation:
• Encapsulation is an Object Oriented
Programming concept that binds together
the data and functions that manipulate
the data, and that keeps both safe from
outside interference and misuse.
Fundamentals of object oriented
Programming
• 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.
• Data hiding is an object-oriented
programming technique of hiding internal
object details i.e. data members.
Fundamentals of object oriented
Programming
Fundamentals of object oriented
Programming
• Inheritance is a process in which one object
acquires all the properties and behaviors of its
parent object automatically.
• 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.
Types Of Inheritance
Fundamentals of object oriented
Programming
• Inheritance Example // Derived class
class Car: public Vehicle
{
// Base class public:
class Vehicle string model = “Altroz";
{ };
public: int main()
string brand = “Tata"; {
void honk() Car myCar;
{ myCar.honk();
cout << "Tuut, tuut! \n" ; cout << myCar.brand + " " + myCar.model;
} return 0;
}; }
Fundamentals of object oriented
Programming
Polymorphism:
• 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.
• A person at the same time can have different
characteristics. Like a man at the same time is a father,
a husband, an employee. So the same person posses
different behavior in different situations. This is called
polymorphism.
• It takes place when there is a hierarchy of classes and
they are related by inheritance.
Fundamentals of object oriented
Programming
Types of Polymorphism
Fundamentals of object oriented
Programming: Polymorphism
class Shape {
protected:
int width, height;

public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
Benefits of OOP
• Modularity for easier troubleshooting
• Reuse of code
• Flexibility through polymorphism
• Effective problem solving
• Security
• Code maintenance
C++ As OOP Language
• C++ is called object oriented programming (OOP)
language because C++ language views a problem in
terms of objects involved rather than the procedure
for doing it.
• Object oriented programming is always good for
writing large business logics and large applications or
games.
• OOPs is also very much desired for maintenance and
long term support.
C++ Program structure
C++ Programming Basics
C++ Data Types
More about Built-in Data Types
Derived Type
• These data types are derived from fundamental data
types.
• Variables of derived data type allows us to store multiple
values of same type in one variable but never allows to
store multiple values of different types.
• e.g. Array of integer values
int string[10];
• Function
• Pointer
• Reference
User-defined data types
Union
Structure Vs Union
Example of Union
User-defined data types
Enumerations
Enumerations
Control Structures
Control structures
Arrays and Strings

22 47 12 21 33 60

0 1 2 3 4 5
Strings
Class
Object
• Everything in C++ is associated with classes
and objects, along with its attributes and methods.
• For example: in real life, a car is an object. The car
has attributes, such as weight and color, and
methods, such as drive and brake.
Class & Object
Data Abstraction
Class & Data Abstraction
Advantages of Abstraction
• The programmer need not write low-level code.
• It protects the internal implementation from
malicious use and errors.
• The Programmer can change the internal details of
the class implementation without the knowledge of
end-user thereby without affecting the outer layer
operations.
Accessing Class Members
• The data members and member functions
of class can be accessed using the dot('. ') operator
with the object.
Accessing Class Members
Access Specifiers

class PrivateAccess
{
private:
int x;
void
display();
}
Access Specifiers
Separating Interface from
Implementation
• Class declaration is the abstract definition of
interface.
• The functions include the details of the
implementation.
• It is possible to separate interface from the
implementation by placing them in separate files.
• The class declaration goes in a header file with a
.h extension whereas implementation goes in the
.cpp file which must include the user defined
header file.
Interface Vs Implementation
Functions
Functions
Function Prototype
Function Prototype
Constructors
• A constructor is a special method that is
automatically called when an object of a class
is created.
• To create a constructor, use the same name as
the class, followed by parentheses ().
• Constructor doesn’t have a return type.
• One can pass parameters to the constructors.
• Constructors are of different types: default,
parameterized, copy.
Constructors
Destructors

• A destructor is a member function that is invoked


automatically when the object goes out of scope or is
explicitly destroyed.
• A destructor has the same name as the class,
preceded by a tilde ( ~ ).
• For example, the destructor for class String is
declared: ~String() .
Destructors
Types of Constructor
Default Constructor
Parameterized Constructor
• It accepts a specific number of parameters. To
initialize data members of a class with distinct values.
• With a parameterized constructor for a class, one
must provide initial values as arguments, otherwise,
the compiler reports an error.
Parameterized Constructor
Copy Constructor
• Used to create new object as a copy of existing
object.
• A copy constructor has the following general
function prototype:
ClassName (const ClassName &old_obj);
Copy Constructor
Objects & Memory Requirements
Static Members: Variables & Functions
Static Members
Inline Function
Inline Function
Inline Function
Friend Function
• A friend function of a class is defined outside
that class' scope but it has the right to access
all private and protected members of
the class.
• Even though the prototypes for friend
functions appear in
the class definition, friends are not
member functions of the class in which they
are declared.
Friend Function
Modular Programming
• Modular programming is the process of subdividing
a computer program into separate sub-programs.
• A module is a separate software component. It can
often be used in a variety of applications and
functions with other components of the system.
Generic Programming
Difference Between Procedure Oriented Programming (POP) & Object
Oriented Programming (OOP) 4M
Story of C++ Invention by Bjarne Stroustrup:
Case Study

You might also like