0% found this document useful (0 votes)
15 views29 pages

Lecture 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views29 pages

Lecture 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

OBJECT ORIENTED

PROGRAMMING
CSE 202. Winter 23-24

Lecture 1.1
INTRODUCTION

PRANAV BISHT
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
IIT(ISM) DHANBAD

*special thanks to Prof. Saurabh Srivastava for his original slides


RECAP
• Programming fundamentals from your first year.
• Distinction between algorithms and programming language.
• Similar to difference in communication skill and language fluency.
• Some of the concepts that you should know:
• Precedence and Associativity of operators
• Conditional constructs (e.g. if-else and switch-case)
• Iterative constructs (e.g. while, do-while and for)
• Arrays and Structures
• Functions
• Pointers
• Pre-processor directives (e.g. #def, #ifdef) etc.
• HW: Revise your first year programming notes/content.
Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad
Recap: Levels of Programming Languages
• What language do machines understand?
• What is Assembly Language?
• What language do we understand?
• How do we bridge the gap?
• Compilers.
• Source Code -> Intermediate/object code -> Machine code.
• In what programming language are C/C++ compilers written?
• Very often in C/C++ itself!
• HW1: Read about compilation process.
• HW2: Read about bootstrapping.

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Evolution of Programming Languages
• Low-level programming languages: Machine code and assembly code.

A hypothetical low-level program for adding


2+5, Source: Runestone Academy

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Evolution of Programming Languages
• High-level programming languages
• Like C, C++, Java, Python.
• More human understandable.

• Structured Programming
• Popularized in 1960s.
• Lack of reliance on GOTO statements.
• Before that, problem of spaghetti code due to indiscriminate use of GOTO statements.
• “GOTO Statement Considered Harmful” open letter in 1968 by Edsger W. Dijkstra
sparked a debate.
• "GOTO Considered Harmful' Considered Harmful" letter by opposition.

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Programming Paradigms
• Procedural languages
• Describe the step by step procedure to be followed to solve a specific problem.
• Passing arguments to functions and returning values from functions.
• Structured around its code (what is happening).
• E.g. – C, BASIC.
• Object-oriented programming (OOP) languages
• Data and methods to manipulate that data are kept as one unit.
• Limits direct access to data.
• More modular.
• Structured around its data (who is being affected).
• E.g. – C++, Java, Python.
• Other paradigms also, like functional programming.
• HW: Read about functional programming.

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Evolution of Programming Languages
• Underlying driving force for evolution of programming style:
• Increasing complexity (size) of codes.
• Better human readability.
• Data hiding.
• Large-scale application development.
• Locality or isolation of bugs.

• Example – Dumping everything in desktop vs. organizing folders.


• Long-term saving of time.
• Initial overhead but saves time in the long run.

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Why
C++?

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Why C++?
• To teach object-oriented programming.
• Extension of C to support OOP features.
• Easier to learn (especially for C programmers).
• Popular.
• Fast compilation and execution.

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


The TIOBE index for popularity, December 2023.
Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad
Digression – A recap of structures in C
• A structure is a collection of variables of different types
• It is a capsule to capture data of varying types
• An instance of a structure (let us call it a structure variable) holds data for all its members
• Thus, it can be used to collect an arbitrary amount of encapsulated information
• A particular member variable could be accessed by using the dot operator

• A common method to perform operations over the captured data, is through functions
• Relevant data must be supplied to and received from these functions appropriately
• For example, through parameters and returned values

• Remember, you can use nesting with structures to capture data hierarchically
• Structures, combined with functions to operate over them, is a legitimate way to build
applications
Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad
Structured Programming
• In this approach, you decouple the “data” from the “operations over the data”
• The data is stored in multiple variables, often collected together inside a wrapper like a
structure
• The operations are implemented as functions…
• … which take the required data as parameters, and return the results, if any

• There is no concept of “privacy” of data


• If a structure variable is passed to a function, all its member variables are accessible for reading
and writing

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


What is Object-Oriented Modelling?
• Object-Oriented Modelling refers to an approach towards designing software
• The idea is to represent the software in terms of collaborating objects
• We will discuss what objects are shortly…

• The design documents produced in the process follow some terminology and conventions
• Usually, these documents contain one or more diagrams, e.g. Class Diagrams
• These documents do not necessarily follow any standards…
• … but they usually communicate enough information to describe the software in context

• There are two major reasons for the popularity of Object Oriented Modelling approach
• First, it aligns well with the real world (we will see how)
• Second, Object Orientation support is fairly common with many general-purpose programming
languages

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad
You should be able to understand the
behaviour of the program on the left …

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


In particular, observe the
printLastName() function

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


While it may be doing what is expected from
it, i.e., printing the last name from the
structure, it is also manipulating the first
name…

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


You may feel that it is dishonesty – why would
a function that is supposed to print the last
name, changing the first name?

While it may be doing what is expected from


it, i.e., printing the first name from the
structure, it is also manipulating the last
name…

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


You may feel that it is dishonesty – why would It could be a programming mistake (a “bug” as
a function that is supposed to print the last it is often called), or that the guy who wrote
name, changing the first name? the printLastName() function, or did it
maliciously.

While it may be doing what is expected from


it, i.e., printing the first name from the
structure, it is also manipulating the last
name…

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Actually, the crux of the problem is that we
cannot have a tight control over what member
variables of a structure variable, are accessible
where !!

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


This is why, any function to which the
structure variable gets passed, can do
anything with any structure member 

Actually, the crux of the problem is that we


cannot have a tight control over what member
variables of a structure variable, are accessible
where !!

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Now check some similar C++ code on the right…

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


While the code on the right may not make
much sense to you right now, check out the
terms “private” and “public”

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


We will see what these terms mean later, but
the takeaway here is that a class – which may
look like an extension of a structure to you as of
now – has some mechanisms to control the
access of its member variables

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Classes and Objects
• A class is a collection of related data items, together with some operations over them
• In contrast, a structure only groups data items, it does not have any operations
• A class consists of two types of members
• The data items or variables, called fields
• The operations or functions that operate over the fields, called methods
• Keep in mind that a class is a “template”, declaring a class doesn’t occupy any memory
• An object is an “instantiation” of a class
• If class is a type, an object of the class is a variable of that type
• An object can be seen as a counterpart of the structure variables
• The operations are essentially functions, which “belong” to the class
• They can access any variable (private/public) of the same class.
• Any private variables of a class can be accessed or manipulated only through associated
functions of that class.

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Some Examples
• Class – Automobile
• Fields – Number of Wheels, Fuel Type, Fuel Capacity etc.
• Operations – drive, add fuel, inflate tyres etc.
• Objects – Volvo B8R, Rolls-Royce Phantom, Harley-Davidson Iron 883
etc.
• Class – Movie
• Fields – Name, Release Year, Viewer Certificate etc.
• Operations – stream, download to device, delete from device etc.
• Objects - The Shawshank Redemption, Interstellar, Baahubali: The
Beginning
• Remember that the object of one class, could be a field for another class
• For example, think about a class called Streaming Platform…
• … which could have a field called list of all movies…
• … which in turn, could be an array of Movie objects
Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad
Now, you can see that Person is a class, and
player is an object of the class Person.

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


Also, fname and lname are fields, whereas
setFname(), setLname() and
printLastName() are methods that represent
operations

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad


In the next Lecture …
• We will go a little deeper into basics of C++
• In particular, we will see that Classes are not the only wrappers that C++ provides
• Multiple Classes can also be put under wrappers called Namespaces

Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad

You might also like