OOP in C++ - 3
OOP in C++ - 3
Dr. K. Veningston
Department of Computer Science and Engineering
National Institute of Technology Srinagar
[email protected]
Other methodologies
• Procedure Oriented Programming (POP)
– Developing software in terms of modules and functions (job to do).
– It gives less importance to data on which job (function) is applied.
– When the complexity of the problem is high, at some stage, the POP fails, as it cannot cope with the
complexity.
• It cannot sustain high complexity, where OOP comes in with the solution.
– Example:
• C, C++, Python
• Functional Programming (FP)
– A technique where most of the activities will be done directly with the help of functions
– Example:
• Python, LISP, Prolog
• Object Oriented Programming (OOP)
– It gives equal importance to both the activities/jobs/functions we do and the data on which these jobs are
performed.
– Examples:
• C++ is OOP & POP
• Java – Pure OOP
• Python is OOP, POP, and FP.
Object Oriented Programming (OOP)
• It is a powerful technique that reflects real-world entities.
• OOP’s Guru:
– Grady Booch (who developed Unified Modeling Language (UML))
defines it as a methodology in which programs are organized/built as
a cooperative collection of objects (interaction/communication
between the objects).
• The object is an instance of a class.
Key terms in OOP
• The class is taken from the hierarchy of classes. Cred
it
Card
User ATM
SB
Acco
unt
What is OOP?
• Object oriented programming (OOP) is the principle of design & development of programs
using modular approach.
– Define real-life problems in a more systematic way (using certain principles).
• The procedural programming focuses on processing of instructions in order to perform a
desired computation.
– It emphasises more on doing things like algorithms.
– Used in programming languages like C & Pascal.
• OOP combines both the data and the functions that operate on that data into a single unit
called the object.
– It follows bottom-up design technique, it is piecing together the smaller systems to give rise to
more complex systems.
• Another major component that plays a major role in OOP is the class.
– It is a template that represents a group of objects which share common properties and
relationships.
Why OOP?
• It is a programming paradigm
– A set of rules, ideas, and concepts.
– A standard in programming that we use to solve a specific type of
problem.
• Other programming paradigms - why?
– We have a lot of different types of problems.
• The main reason is that the traditional procedural
programming is unable to model real-world problems.
C++ as Object Oriented Language?
• It is an object-oriented, general purpose programming language, derived
from C.
• Existing code on C can be used with C++, hence mode compatible.
– Even existing pre-compiled libraries can be used with new C++ code.
– Also there is no additional cost for using C++, hence efficient.
• Major improvements over C
– Stream I/O
– Strong typing
– Default argument values
– Parameter passing by reference
– OOP principles
Idea behind OOP paradigm
• To represent real-time objects/entities.
– To get with their characteristics, attributes, and behaviors.
• To explain those objects to our computer.
– To represent those objects in our programs.
How does it work on a real-life example?
• Example: Consider “Car” as an entity.
– In real life, it has many attributes or characteristics.
• Manufacturer, color, price, max speed, etc.
– Behaviors of Car
• Drive, accelerate, stop, open door, fuel level, etc.
• Other examples:
– Student, Book, Animal, etc.
Objects
• The object represents a real-world entity.
– Entities are tangible that they can be seen, noticed, or at least felt.
– Examples: room, table, chair, book, paper, student, employee, vehicle, pen,
fan, animal, box, air, etc.
– What are not objects?
• Alien
– Entities are represented in programming as objects.
– Entity possesses some properties and behaviors.
• Properties describe the entity.
• Behaviors describe actions/functionality/operations performed on the object or by
the object.
Object – Example 1
ROOM
Properties: length,
width, height,
colour
Behaviours: area(),
getRent(), paint()
Objects – Example 2
• A neat approach of developing a software.
LIBRARY
BOOK
Properties: length,
width, height,
Properties: title,
colour, capacity,
author, publisher
#books
Behaviours: read(),
Behaviours: stock(),
copier(), getPrice(),
procureBooks(),
getQtyInStock()
issueBook(),
returnBook()
Objects – Example 3
• A neat approach of developing a software.
EMPLOYEE
COMPANY
Properties: empID,
Properties: name, name, department,
location contact, salary
Behaviours:
revenue(),
promoteEmp()
class
• Template where properties and behaviors of an entity/object are
kept.
– Class is an area/place holder/template/prototype which is holding all the
properties and behaviors of a particular entity.
– Object represents the entity itself.
• Example (class vs. object)
– Class: “Plan of a House” → class says how entity/house should be in the
form of a template/description.
– Object: “House” → entity itself/the real entity is object.
• From a class, we can create any # of objects. Class Object
• From a plan, we can construct any # of houses. Type of Variable Variable itself
Data type describes Every variable has
size, operations, etc. associated attributes
and operations
class
• The building block of OOP.
– Class is a user-defined datatype as opposed to a pre-defined datatype
(int, float, bool, double, etc.).
– It is a bit more complex datatype.
• To define a user, we need to store user characteristics including ‘name’,
‘gender’, ‘email’, etc.,
• All of the variables together are going to make a group called class.
• Applications that need to store users:
– FB: ‘name’, ‘gender’, ‘email’, ‘age’
– Fitness App: ‘name’, ‘gender’, ‘email’, ‘age’, ‘height’, ‘weight’
OOAD – Approaching a real-world problems
• (i) Identification of Entities (Noun phrase approach)
• (ii) Abstraction of each entity
– Identifying/finding the essential characteristics/behaviours of an
entity in the perspective of the user in the problem
space/application domain.
PERSON
– It is a way of looking/viewing towards an object/entity.
Properties: name,
ID, DoB, height, – Class is a good example. EMPLOYEE
weight, PATIENT
bloodGroup,BP, STUDENT CUSTOMER Properties: name,
bodyTemp, Properties: name, ID, DoB,
qualification, Properties: name, ID, DoB, height, Properties: name, qualification,
parentage, 10th ID, DoB, parentage, weight, BP, ID, DoB, parentage, 10th
mark, 12th mark, 10th mark, 12th bodyTemp, qualification, mark, 12th mark,
address, etc. mark, address parentage, address parentage, address address
GENERAL PERSON ENTITY COLLEGE HOSPITAL BANK COMPANY
OOAD – Approaching a real-world problems
• (iii) Encapsulation of entities
– Combining the properties and behaviours of an entity into a
capsule/template called class.
– A kind of hiding the details
– Class is a good example.
PERSON
Properties: name,
ID, DoB, height, EMPLOYEE
weight, PATIENT
bloodGroup,BP, STUDENT CUSTOMER Properties: name,
bodyTemp, Properties: name, ID, DoB,
qualification, Properties: name, ID, DoB, height, Properties: name, qualification,
parentage, 10th ID, DoB, parentage, weight, BP, ID, DoB, parentage, 10th
mark, 12th mark, 10th mark, 12th bodyTemp, qualification, mark, 12th mark,
address, etc. mark, address parentage, address parentage, address address
GENERAL PERSON ENTITY COLLEGE HOSPITAL BANK COMPANY
How to create class in C++?
• Members of a class = attributes + behavior
Class Method
Scope resolution operator ::
• The functions can be defined outside the class
– however they should be declared inside the class.
What if we have to create many Employee objects?
• We may need to repeat lines 19 to 21 for every Employee
object.
– This is not really optimal.
– There is a better approach to constructing objects.
• Constructors
– A constructor is a special type of method that is invoked each time an
object of a class is created.
Default constructor
• The output is the work of the
default constructor.
– Constructor that is
automatically generated by the
compiler, in case we do not
create constructors of our
own.
How we can create our own constructor?
• 3 Rules to create constructors
– (i) Despite it being a special method, the constructor does not have a
return type, unlike methods.
– (ii) The constructor has the same name as the class that it belongs to.
– (iii) The constructor must be public.
• Everything that is private is locked/hidden inside the class. We do not want to
make our constructor hidden.
• Note:
– When we decide to create our own constructors, the default
constructor will not be created.
Constructor - Example
• Code is reduced.
▪ Class member function definitions/ static variable initialization belong in the scope where the
class is defined. In this case, global scope.
▪ We cannot define them inside main().
Initializing static member
Initializing static member
static member functions (static methods)
• By declaring a function member as static, you make it independent of any
particular object of the class.
– static functions belong to all the objects.
• A static member function can be called even if no objects of the class exist
and the static functions are accessed using only the class name and the
scope resolution operator ::
• A static member function can only access static data member, other static
member functions and any other functions from outside the class.
– Non-static attributes of a class belong to the object. Is it okay that a static
method access non-static members? No.
• Static member functions have a class scope and they do not have access to
the ‘this’ pointer of the class.
NOTE
• Static method cannot access non-static attributes of an object.
• Static method can only access static attributes present inside
the class. call
other class)
Non-static variables
each object
objects
Non-static variables Non-static methods
obj3
Non-static variables
static member functions - Example
Passing and Returning Objects in C++
• In C++, we can pass class objects as arguments and also return
them from a function the same way we pass and return other
variables.
• Passing objects as arguments
– The address/reference of the object will be sent, if we pass using pointers.
– If we pass the object directly, the entire copy of that object will be sent.
– To pass an object as an argument, we write the object name as the
argument while calling the function the same way we do it for other
variables.
– Syntax: fun(object_name)
Passing an object as argument - Example