Professional Documents
Culture Documents
OOP in C++
OOP in C++
Dr. K. Veningston
Department of Computer Science and Engineering
National Institute of Technology Srinagar
veningstonk@nitsri.ac.in
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.