Unit 1
Unit 1
Unit 1
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
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