0% found this document useful (0 votes)
29 views25 pages

File 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)
29 views25 pages

File 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/ 25

Object Oriented Programming

and Design (CS 204)

CO1
Programming Paradigm – to – String Class

Prepared by –
Dr. Soumen Moulik
Department of Computer Science & Engineering
National Institute of Technology Meghalaya
Programming Paradigms
 What is Programming Paradigm ?
◦ Fundamental style of programming that defines how the structure and
basic elements of a computer program will be built.
 Classification:
◦ Monolithic programming, Ex – Assembly, BASIC
 Emphasizes on finding a solution
 Consist of only global data and sequential code
 Change of sequence of instructions need „goto‟ statements
 No support of the subroutine concept
◦ Procedural programming, Ex – FORTRAN, COBOL
 Lay stress on algorithms
 Better abstraction, global data items, concept of subroutines
 Program controls are through jumps (gotos) and calls to subroutines
 Global data is shared and therefore may get altered

2
Programming Paradigms
 Classification (contd.):
◦ Structured programming, Ex – Pascal, C
 Focuses on modules – may contain more than one subroutine
 Controlled scope of data, concept of user-defined data types
 Emphasis on algorithm rather than data; top-down approach
 Global data is shared and therefore may get altered
◦ Object-oriented programming, Ex – C++, Java
 Encapsulation of data and functions – Objects
 Data is hidden and not accessible by external functions
 Emphasis on data rather than algorithm; bottom-up approach
◦ Logic-oriented programming, Ex – Prolog
 Focusing on goals usually expressed in predicate calculus
◦ Rule-oriented programming, Ex – ELAN
 Makes use of „if-then-else‟ rules for computation

3
Timeline

4
Procedural Programming

 Many important data items are placed as global so that they may be
accessed by all the functions
 But, global data are more vulnerable to inadvertent changes
 In large program it is hard to find what data is used by which function
 Procedural approach does not model real world problems very well
5
Object-Oriented Programming

6
Properties of OOP
 Data and associated operations are unified into a single unit,
which is known as an object
◦ An object is a partitioned area of computer memory that stores data,
and set of operations that can access the data
 In Function Oriented Programming (FOP) problem is divided
into functions
 In Object Oriented Programming (OOP) problem is divided
into objects
 OOP can be referred to as the superset of FOP
 Data is hidden and cannot be accessed by external functions
 OOP protects data from accidental modification from
outside functions
 Objects communicate with each other through functions
7
Objects

 An object can be any entity – a person, a place, or a thing


 Decomposition of a problem into objects depends on
judgement and nature of the problem

8
Classes
 The objects with the same data structure (attributes) and
behaviour (operations) are grouped into a class

9
Classes
 Every object is associated with data and functions which
define meaningful operations on that object

 The declaration is similar to


the structure declaration in C
 It enables the creation of class
variables called objects
 Ex –
account savings_account
account current_account
account FD_account
 What about class student ?

10
Classes
 A class is a template that unites data and
operations
 A class is an abstraction of the real world
entities with similar properties
 A class identifies a set of similar objects
 Ideally, the class is an implementation of
abstract data type

11
Inheritance
 Inheritance is the process by which objects of one class
acquire the properties of objects of another class
 Ex – Rectangle gets all the features of polygon, and all
the features of closed figure

12
Polymorphism
 Polymorphism, a Greek term, means the ability to take
more than one form
 An operation may exhibit different behaviours in
different instances
 Different types ways of achieving polymorphism –
 Operator overloading –
◦ When operands are numbers, then result is sum
◦ When operands are strings, then result is concatenated string
 Function overloading –
◦ Using a single function name to perform different types of tasks

13
Dynamic / Late Binding
 Binding refers to the linking of a procedure call to the
code to be executed in response to the call
 In case of dynamic / late binding –
◦ The code associated with a given procedure call is not known
until the time of the call at run-time
 Ex – Class: Shape; Function: Draw ()
◦ Object: Circle; Function: Draw (circle)
◦ Object: Box; Function: Draw (box)
◦ Object: Triangle; Function: Draw (triangle)

14
Introduction to C++
 C++ developed by Bjarne Stroustrup in 1979
 It supports all features of C and adds other new
features such as classes, objects etc.
 A C++ program is composed of the following elements
◦ Preprocessor‟s directive section
◦ Global declaration section
◦ Class declaration and method definition section
◦ Main function
◦ Method definition section
 Example C++ program:

15
Compilation of a C++ program

16
Tokens
 Tokens are smallest individual units in a program
 Most of the C++ tokens are basically similar to the C tokens
with the exception of some additions and minor modifications

17
Tokens
 Keywords –
◦ Reserved words that cannot be used as identifiers / variable names
◦ 32 keywords from ANSI C + Additional keywords

18
Tokens
 Identifiers –
◦ Refer to the names of variables, functions, arrays, classes etc.
◦ Only alphabetic characters, digits and underscores are permitted
◦ The name cannot start with a digit
◦ Uppercase and lowercase letters are distinct
◦ A declared keyword cannot be used as variable name
◦ C++ allows unlimited length for variable name
 ANSI C recognizes only 32 characters in a name
◦ Example – abc, my_name, a_123, retVal
 Constants –
◦ Refer to fixed values that do not change during the execution
◦ Example – 123 (decimal), 12.34 (float), “C++” (string), „A‟ (character),
037 (octal), 0X2 (hexadecimal)

19
Data Types
C++ Data
Types

User-
Built-in Derived
defined

structure Integral Void Floating array

union int float function

class char double pointer

enum reference
SIZE:
20
Implicit Type Conversion
 Implicit –
◦ When compiler encounters an expression, it divides it into sub-
expressions consisting of one or two operands
◦ In case of a binary operator, the „smaller‟ type is converted to the
„wider‟ type
◦ If one operand is an int and the other is a float, then the int is
converted into a float because a float is wider than int
◦ Integral widening conversion
◦ EXAMPLE:

 Explicit – (type) expression; (type) variable_name


◦ EXAMPLE:

21
Inline Functions
 When a program executes general function call instructions –
◦ CPU stores the memory address of the instruction following the function call
◦ Copies the arguments of the function on the stack
◦ Then transfers control to the specified function
◦ Executes the function code
◦ Stores the function return value in a predefined memory location
◦ Then returns control to the calling function
 This is overhead if the execution time of function is less than the
switching time from the caller function to the called function
 C++ provides the feature of inline function to reduce this overhead
◦ Inline functions are expanded in line, i.e., gets inserted or substituted, at the time
of inline function call EXAMPLE:
◦ This is only a request to the compiler, not a command
◦ Compiler may not perform inlining if a function contains loop, static variables,
recursion, switch or goto statements, or returns something other than void

22
String Class
 length() / size()  no. of characters in a string, including
white space
 at(index)  character at a particular index
 append(str)  adds the argument string at the end
 replace(a, b, str)  replaces b characters from index a by str
 erase(a, b)  deletes b characters at index a
 find(str)  returns index where pattern is found. If pattern is
not found then returns predefined constant npos (= -1)
 substr(a, b)  returns a substring of b length starting from
index a

EXAMPLE:

23
Moving from C to C++
 Scope resolution operator  ::
 Variable definition at any point of use
 Reference variable  enjoys the simplicity of value variable and
power of the pointer variable
 Function overloading
 Default arguments are allowed
 Functions are allowed as a part of a structure
 Argument passing by reference is allowed
 Dynamic / Run-time memory management

Some C programs that won‟t compile in C++


[https://www.geeksforgeeks.org/write-c-program-wont-compiler-c/]

24
Reference Variable
 References vs. Pointers:
◦ Pointer is a variable that holds memory address of another
variable, while Reference is an alias or another name for an
already existing variable
◦ A pointer can be declared as void but a reference can never be.
◦ Pointer variable has multiple levels of indirection, i.e. single-
pointer, double-pointer etc., but reference variable has only one
level of indirection.
◦ C and C++ support pointers; whereas C++, Java, Python, Ruby,
Perl and PHP support references
 Applications:
◦ Modify the passed parameters in a function
◦ Avoiding a copy of large structures

25

You might also like