0% found this document useful (0 votes)
34 views30 pages

Lecture 3

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)
34 views30 pages

Lecture 3

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/ 30

OBJECT ORIENTED

PROGRAMMING
CSE 202. Winter 23-24

Lecture 1.2
OOP Basics

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

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


Let us go back to C … for a while !!

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


Let us go back to C … for a while !!

What can you infer from this C code?

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


Let us go back to C … for a while !!

There are two methods related to addition:


1. add()
2. add_to_result()
These may be declared in the header file add.h

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


Let us go back to C … for a while !!

Similarly, there are two methods related to multiplication:


1. multiply()
2. multiply_with_result()
These may be declared in the header file multiply.h

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


Let us go back to C … for a while !!

All the above observations are correct, so this looks good to


execute …

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


Let us go back to C … for a while !!

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


Let us go back to C … for a while !!

Oops... there is an error regarding multiple declarations for the


identifier result

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


Let us go back to C … for a while !!

That does seem to be the case !!

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


Let us go back to C … for a while !!

But if we assume that the two files


came from different sources (e.g.,
from two different libraries), it is
certainly plausible to see such
coincidences.

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


C++ to the rescue …
• The problem with the C code we saw is that an extern variable is accessible “everywhere”.
• In essence, the space in which it lives is the global space.

• C++ introduced a mechanism to restrict even global variables to a “smaller space”.


• This mechanism is known as a namespace.
• A namespace is a collection of C++ statements – declarations as well as definitions.
• All identifiers within a namespace must be unique, but there can be duplicates outside.
• Actually, Namespaces help achieve a top-level encapsulation – a concept we will discuss in the
coming lectures.

• To access any variable or function within a namespace, prefix the namespace name followed
by ::
• :: is known as the scope resolution operator; we will revisit this guy again in later lectures !!

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


C++ Namespaces

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


C++ Namespaces

Adding code to a namespace is no


different than creating a code
block, and adding the code inside
it

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


C++ Namespaces

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


C++ Namespaces

The way to access the functions become a little tedious


though, since you need to provide their fully qualified names

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


C++ Namespaces

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


C++ Namespaces

This time we compiled successfully and see some output …

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


Hello World!

The customary Hello World program.

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


Some more C++ basics
• Modern-style headers defined by Standard C++ do not have the extension “.h”.
• The stdio.h equivalent in C++ is iostream.
• The printf() equivalent in C++ is cout.
• The scanf() equivalent in C++ is cin.
• Unlike printf() and scanf(), cout and cin are not functions, but streams.
• A stream is an entity where a program can either insert characters to or extract characters
from.
• We need not worry about the internal specifications of the stream.
• Some streams defined in the standard library:
• cin
• cout
• cerr
• clog

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


Some more C++ basics
• The insertion operator (<<) is used with cout, i.e. inserting into the stream.
• The extraction operator (>>) is used with cin, i.e. extracting from the stream.
• For simplicity, you can think of cout and cin to represent the screen.
• The << and >> operators are also left and right shift operators (operator overloading).
• Two major differences from C
• You don’t need to provide format specifiers to cin or cout.
• To take multiple inputs or print multiple pieces of output, you have to use the << and >>
operators multiple times.
• Let’s see few example codes to illustrate.

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


• “Hello” is a string while Hello is an integer variable.
• No need for format specifier.

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


We can use either “\n” or endl for breaking the line.

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


We can chain multiple << in a single statement also.
Similarly we can chain multiple >> in a single statement also.

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


Gojo gets cut from the sentence

HW: Save Gojo from getting cut

Suppose you enter “Gojo Satoru”. What will be the output?

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


Inheritance example
Paneer and Capsicum
Onion and Tomato
Pizza
Pizza

Onion Tomato Paneer Capsicum

Cheeze Cheeze

Pizza Sauce Pizza Sauce

Pizza Base Pizza Base

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


Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad
Pranav Bisht | Dept. of CSE | IIT (ISM) Dhanbad
What if I add this line?

We get an error!

Why?

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


Protected members are private except for
the derived class.

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

You might also like