[go: up one dir, main page]

0% found this document useful (0 votes)
26 views69 pages

Unit 1

The document outlines a course on Introduction to Object Oriented Concepts, detailing the vision and mission of the department, program outcomes, and course outcomes. It includes a syllabus covering various topics such as OOP principles, classes, inheritance, polymorphism, and exception handling, along with textbooks and reference materials. Additionally, it provides an overview of C and C++ programming languages, highlighting their features, differences, and the fundamentals of object-oriented programming.

Uploaded by

yernekrish1247
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)
26 views69 pages

Unit 1

The document outlines a course on Introduction to Object Oriented Concepts, detailing the vision and mission of the department, program outcomes, and course outcomes. It includes a syllabus covering various topics such as OOP principles, classes, inheritance, polymorphism, and exception handling, along with textbooks and reference materials. Additionally, it provides an overview of C and C++ programming languages, highlighting their features, differences, and the fundamentals of object-oriented programming.

Uploaded by

yernekrish1247
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/ 69

II SEMESTER

23ADS1206: Introduction to Object


Oriented Concepts

Shubhangi S. Shambharkar
Vision & Mission
Vision of the Department
• To be a well-known center for pursuing computer
education through innovative pedagogy, value-
based education and industry collaboration.
Mission of the Department
• To establish learning ambience for ushering in
computer engineering professionals in core and
multidisciplinary arena by developing problem-
solving skills through emerging technologies.
Program Outcome
• PO 1 – Engineering knowledge
• PO 2 – Problem analysis
• PO 3 -Design/development of solutions
• PO 4 – Conduct investigations of complex problems
• PO 5 – Modern tool usage
• PO 6 – The engineer and society
• PO 7 – Ethics
• PO 8 – Individual and team work
• PO 9 – Communication
• PO 10 – Project management and finance
• PO 11 – Life-long learning
Scheme
Course Outcomes
• Upon successful completion of the course the
students will be able to
• 1. Understand the concept of object-oriented
programming and modelling
• 2. Apply the knowledge of object-oriented
programming to solve the given problem
• 3. Analyze the problem to provide the object
oriented solution using advanced
programming concepts.
Syllabus
Unit I: 6 Hrs.
Introduction to OOP ,History of OOP, structured and procedural programming, switching from
conventional to OO programming, Basic concepts of OOP, benefits and applications.
Unit II: 7 Hrs.
Programming with OOP Classes, objects, access specifiers, constructors, destructors, token
expressions and control structures functions: inline, friend.
Unit III: 7 Hrs.
Inheritance Types of inheritance: single, multiple, multilevel, hierarchical, hybrid, virtual base class,
abstract base class
Unit IV: 7 Hrs.
Polymorphism Introduction to polymorphism, virtual function, pure virtual function, pointers,
function and operator overloading
Unit V: 7 Hrs.
Exception and File Handling Throw and catch mechanism, rethrowing exception, specifying
exception, working with Files
6 Hrs.
Unit VI: Templates and STL ,Class template, function template, overloading template
function, non-type template organization, STL, components and types of STL:
• Textbooks:
• 1. Object Oriented Programming with C++, 5th edition , Bal
Gurusamy, McGraw Hill Publications.
• 2. Object Oriented Programming in C++ , 3rd edition ,
Lafore,R , Sams Publication

• Reference Books:
• 1. The C++ Programming Language, 6th edition ,
Stroustrup.B , Pearson Education
• 2. C++ The Complete Reference , 6th edition , Schildt, H ,
McGraw Hill Publications

• MOOCs Links and additional reading, learning, video


material
• 1. https://nptel.ac.in/courses/106105153
Google Classroom Code
What is C?
• C is a structural or procedural oriented programming
language which is machine-independent and extensively
used in various applications.
• C is the basic programming language that can be used to
develop from the operating systems (like Windows) to
complex programs like Oracle database, Git, Python
interpreter, and many more.
• C programming language can be called a god's
programming language as it forms the base for other
programming languages. If we know the C language, then
we can easily learn other programming languages.
• C language was developed by the great computer scientist
Dennis Ritchie at the Bell Laboratories.
What is C++?

• C++ is a special-purpose programming


language developed by Bjarne Stroustrup at
Bell Labs circa 1980. C++ language is very
similar to C language, and it is so compatible
with C that it can run 99% of C programs
without changing any source of code though
C++ is an object-oriented programming
language, so it is safer and well-structured
programming language than C.
C++ Program
• // Header file for input output functions
• #include <iostream>
• using namespace std;

• // main() function: where the execution of


• // C++ program begins
• int main() {

• // This statement prints "Hello World"
• cout << "Hello World";

• return 0;
• }
• The #include is a preprocessor directive tells the
compiler to include the content of a file in the
source code. For
example, #include<iostream> tells the compiler
to include the input-output library which contains
all C++’s input/output library functions.
• This is used to import the entity of
the stdnamespace into the current namespace of
the program. It is basically the space where all
the inbuilt features of C++ are declared. For
example, std::cout.
Standard Output Stream – cout

• The data needed to be displayed on the


screen is inserted in the standard output
stream (cout) using the insertion
operator(<<).
• cout << value/variable;
Standard Input Stream – cin
• The extraction operator (>>) is used along
with the object cin for extracting the data
from the input stream and store it in some
variable in the program.
• cin >> variable;
Features of C++
• It provides a lot of features that are given below.
• Simple
• Abstract Data types
• Machine Independent or Portable
• Mid-level programming language
• Structured programming language
• Rich Library
• Memory Management
• Quicker Compilation
• Pointers
• Recursion
• Extensible
• Object-Oriented
• Compiler based
• Reusability
• National Standards
• Errors are easily detected
• Power and Flexibility
• Strongly typed language
• Redefine Existing Operators
• Modeling Real-World Problems
• Clarity
• 1) Simple
• C++ is a simple language because it provides a
structured approach (to break the problem
into parts), a rich set of library functions, data
types, etc.
• 2) Abstract Data types
• In C++, complex data types called Abstract
Data Types (ADT) can be created using classes.
• 3) Portable
• C++ is a portable language and programs
made in it can be run on different machines.
• 4) Mid-level / Intermediate programming language
• C++ includes both low-level programming and high-level language
so it is known as a mid-level and intermediate programming
language. It is used to develop system applications such as kernel,
driver, etc.

• 5) Structured programming language


• C++ is a structured programming language. In this we can divide the
program into several parts using functions.

• 6) Rich Library
• C++ provides a lot of inbuilt functions that make the development
fast. Following are the libraries used in C++ programming are:
• <iostream>
• <cmath>
• <cstdlib>
• <fstream>
• 7) Memory Management
• C++ provides very efficient management techniques. The
various memory management operators help save the
memory and improve the program's efficiency. These
operators allocate and deallocate memory at run time.
Some common memory management operators available
C++ are new, delete etc.

• 8) Quicker Compilation
• C++ programs tend to be compact and run quickly. Hence
the compilation and execution time of the C++ language is
fast.
• 9) Pointer
• C++ provides the feature of pointers. We can use pointers
for memory, structures, functions, array, etc. We can
directly interact with the memory by using the pointers.
• 10) Recursion
• In C++, we can call the function within the
function. It provides code reusability for every
function.
• 11) Extensible
• C++ programs can easily be extended as it is very
easy to add new features into the existing
program.
• 12) Object-Oriented
• In C++, object-oriented concepts like data hiding,
encapsulation, and data abstraction can easily be
implemented using keyword class, private, public,
and protected access specifiers. Object-oriented
makes development and maintenance easier
• 13) Compiler based
• C++ is a compiler-based programming language, which
means no C++ program can be executed without
compilation. C++ compiler is easily available, and it requires
very little space for storage. First, we need to compile our
program using a compiler, and then we can execute our
program.
• 14) Reusability
• With the use of inheritance of functions programs written
in C++ can be reused in any other program of C++. You can
save program parts into library files and invoke them in
your next programming projects simply by including the
library files. New programs can be developed in lesser time
as the existing code can be reused. It is also possible to
define several functions with same name that perform
different task. For Example: abs () is used to calculate the
absolute value of integer, float and long integer.
• 15) Errors are easily detected
• It is easier to maintain a C++ programs as errors can be
easily located and rectified. It also provides a feature
called exception handling to support error handling in
your program.
• 16) Power and Flexibility
• C++ is a powerful and flexible language because of
most of the powerful flexible and modern UNIX
operating system is written in C++. Many compilers and
interpreters for other languages such as FORTRAN,
PERL, Python, PASCAL, BASIC, LISP, etc., have been
written in C++. C++ programs have been used for
solving physics and engineering problems and even for
animated special effects for movies.
• 17) Strongly typed language
• The list of arguments of every function call is typed checked during
compilation. If there is a type mismatch between actual and formal
arguments, implicit conversion is applied if possible. A compile-time
occurs if an implicit conversion is not possible or if the number of
arguments is incorrect.

• 18) Redefine Existing Operators


• C++ allows the programmer to redefine the meaning of existing
operators such as +, -. For Example, The "+" operator can be used
for adding two numbers and concatenating two strings.

• 19) Modelling real-world problems


• The programs written in C++ are well suited for real-world modeling
problems as close as possible to the user perspective.

• 20) Clarity
• The keywords and library functions used in C++ resemble common
English words
No. C C++
1) C follows the procedural style C++ is multi-paradigm. It supports both procedural and
programming. object oriented.
2) Data is less secured in C. In C++, you can use modifiers for class members to
make it inaccessible for outside users.
3) C follows the top-down approach. C++ follows the bottom-up approach.

4) C does not support function overloading. C++ supports function overloading.

5) In C, you can't use functions in structure. In C++, you can use functions in structure.

6) C does not support reference variables. C++ supports reference variables.

7) In C, scanf() and printf() are mainly used for C++ mainly uses stream cin and cout to perform input
input/output. and output operations.
8) Operator overloading is not possible in C. Operator overloading is possible in C++.

9) C programs are divided into procedures C++ programs are divided into functions and classes.
and modules
10) C does not provide the feature of C++ supports the feature of namespace.
namespace.
11) Exception handling is not easy in C. It has to C++ provides exception handling using Try and Catch
perform using other functions. block.
12) C does not support the inheritance. C++ supports inheritance.
Object-oriented programming
• As the name suggests uses objects in programming.
• Object-oriented programming aims to implement real-world entities like
inheritance, hiding, polymorphism, etc. in programming.
• The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function.
• There are some basic concepts that act as the building blocks of OOPs i.e.
• Class
• Object
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
Characteristics of an Object-Oriented
Programming Language
Important features of Object Oriented
Programming
The important features of Object
Oriented programming are:
• Abstraction
• Polymorphism
• Inheritance
• Encapsulation

Terminologies used in Object


Oriented programming
• Objects
• Classes
Classes
• The building block of C++ that leads to Object-Oriented programming is a Class.
• It is a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class
• A class is like a blueprint for an object.
• For Example: Consider the Class of Cars. There may be many cars with different
names and brands but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range, etc. So here, the Car is the
class, and wheels, speed limits, and mileage are their properties.
• A Class is a user-defined data type that has data members and member functions.
• Data members are the data variables and member functions are the functions used
to manipulate these variables together these data members and member functions
define the properties and behavior of the objects in a Class.
• In the above example of class Car, the data member will be speed limit, mileage, etc
and member functions can apply brakes, increase speed, etc.
Classes
• These contain data and Class classname
functions bundled together {
under a unit. Data
• In other words class is a Functions
collection of similar objects. };
• When we define a class it just main ( )
{
creates template or Skelton. classname objectname1,objectn
• So no memory is created }
when class is created.
• Memory is occupied only by
object.
Objects
• Object is an instance of a Class classname
class {
• Classes acts as data types Data
Functions
for objects };
• The functions defined main ( )
inside the class are called {
member functions classname objectname1,objectn
}
• The variables defined
inside the class are called
data member
Encapsulation
• Combining data and functions
together as a bundle is called
as encapsulation.
• Provides data hiding.
• This concept is the main heart
of an Object oriented
programming.
• Data is hidden inside the class
by declaring it as private
inside the class.
• When data or functions are
defined as private it can be
accessed only by the class in
which it is defined.
• Consider a real-life example of encapsulation, in a company,
there are different sections like the accounts section, finance
section, sales section, etc.
• The finance section handles all the financial transactions and
keeps records of all the data related to finance.
• Similarly, the sales section handles all the sales-related
activities and keeps records of all the sales.
• Now there may arise a situation when for some reason an
official from the finance section needs all the data about sales
in a particular month.
• In this case, he is not allowed to directly access the data of the
sales section. He will first have to contact some other officer in
the sales section and then request him to give the particular
data.
• This is what encapsulation is. Here the data of the sales section
and the employees that can manipulate them are wrapped
Encapsulation (Cont..)
• When data or functions are defined as public
then it can be accessed anywhere outside the
class.
• Object Oriented programming gives importance
to protecting data which in any system.
• This is done by declaring data as private and
making it accessible only to the class in which it is
defined.
• This concept is called data hiding.
• We must keep at least one member functions as
public.
Inheritance
• Inheritance as the name suggests is
the concept of inheriting or deriving
properties of an exiting class to get
new class or classes.
• In other words we may have common
features or characteristics that may be
needed by number of classes.
• So those features can be placed in a
common tree class called base class
and the other classes which have
these characteristics can take the tree
class and define only the new things
that they have on their own in their
classes.
• These classes are called derived class.
Inheritance (Cont..)
• The main advantage of using
this concept of inheritance in
Object oriented
programming is it helps in
reducing the code size since
the common characteristic is
placed separately called as
base class and it is just
referred in the derived class.
• This provide the users the
important usage of
terminology called as
reusability.
Example
Polymorphism
• The word polymorphism means having many
forms.
• In simple words, we can define
polymorphism as the ability of a message to
be displayed in more than one form.
• A person at the same time can have different
characteristics.
• A man at the same time is a father, a
husband, and an employee. So the same
person possesses different behavior in
different situations. This is called
polymorphism.
• An operation may exhibit different behaviors
in different instances. The behavior depends
upon the types of data used in the operation.
C++ supports operator overloading and
function overloading.
Example
Abstraction
• Abstraction means displaying only essential information
and hiding the details.
• Data abstraction refers to providing only essential
information about the data to the outside world, hiding the
background details or implementation.
• Consider a real-life example of a man driving a car. The man
only knows that pressing the accelerator will increase the
speed of the car or applying brakes will stop the car
• but he does not know how on pressing the accelerator the
speed is actually increasing, he does not know about the
inner mechanism of the car or the implementation of an
accelerator, brakes, etc. in the car. This is what abstraction
is.
Example
• Abstraction using Classes: We can implement Abstraction
in C++ using classes. The class helps us to group data
members and member functions using available access
specifiers. A Class can decide which data member will be
visible to the outside world and which is not.
• Abstraction in Header files: One more type of abstraction
in C++ can be header files. For example, consider the pow()
method present in math.h header file. Whenever we need
to calculate the power of a number, we simply call the
function pow() present in the math.h header file and pass
the numbers as arguments without knowing the underlying
algorithm according to which the function is actually
calculating the power of numbers.
Scope Rules of Functions and
Variables
• The scope of an identifier is that part of the
C++ program in which it is accessible.
• In C++ there are four kinds of scope:
1. Local Scope
2. Function Scope
3. File Scope
4. Class Scope
Local Scope
• A block in C++ is enclosed by a pair of curly
braces i.e., ‘{‘ and ‘}’.
• The variables declared within the body of the
block are called local variables and can be used
only within the block.
• These come into existence when the control
enters the block and get destroyed when the
control leaves the closing brace.
• Note the variable(s) is/are available to all the
enclosed blocks within a block.
Function Scope
• A label (used in goto statements) has function
scope. It is visible everywhere inside the
function.
• It pertains to the labels declared in a function
i.e., a label can be used inside the function in
which it is declared.
• So we can use the same name labels in
different functions.
File Scope
• If the declaration of an identifier appears
outside all functions, it is available to all the
functions in the program and its scope
becomes file scope.
• A variable declared outside any function has
file scope (global scope).
• It can be accessed from anywhere in the file.
Class Scope
• Variables inside a class have class scope.They can
be accessed using objects of the class.
• In C++, every class maintains its won associated
scope.
• The class members are said to have local scope
within the class.
• If the name of a variable is reused by a class
member, which already has a file scope, then the
variable will be hidden inside the class.
• Member functions also have class scope.
Definition and Declaration of a Class
• A class in C++ combines related data and
functions together.
• It makes a data type which is used for creating
objects of this type.
• Classes represent real world entities that have
both data type properties (characteristics) and
associated operations (behavior).
Syntax of a Class Definition
• The body of the class can have three keywords
namely:
1. private
2. public
3. protected

• In C++, the keywords private, public and


protected are called access specifiers.
• The data hiding concept in C++ is achieved by
using the keyword private.
• Private data and functions can only be
accessed from within the class itself.
• Public data and functions are accessible
outside the class also.
• Protected data and functions behaves as
private data only but can be inherited by
deriving class.
Concept of Inheritance
• Inheritance is a concept which is the result of
commonality between classes.
• Due to this mechanism, we need not repeat the
declaration as well as member functions in a class if
they are already present in another class.
• For example, consider the classes namely “minister”
and “prime minister”.
• Whatever information is present in minister, the same
will be present in prime minister also.
• Apart from that there will be some extra information in
class prime minister due to the extra privileges enjoyed
by him.
Concept of Inheritance (Cont..)
• Due to the mechanism of inheritance, it is
enough only to indicate that information
which is a specific to prime minister in its
class.
• In addition, the class prime minister will
inherit the information of class minister.
Base Class and Derived Class
Types of Inheritance
Types of Inheritance (Cont..)
Single Class Inheritance
• Consider the following classes
class A { ......};
class C: private A • All the public parts of class
A and all the protected
{ parts of class A, become
private members/parts of
. the derived class C.
. • No private member of class
A can be accessed by class
. C.

};
Multiple Class Inheritance
• Consider the following classes
class A { ......}; • All the public and protected
parts of class A, become
class B {......}; private members / parts of the
derived class C.
class C: private A, public B• All the public parts of class B,
{ become public members /
parts of the derived class C.
. • All the protected parts of class
B, become protected
. members / parts of the
derived class C.
.
• No private member of class A
}; and class B can be accessed by
class C.
Ways of Deriving a Class
Polymorphism
• Polymorphism means ‘one name multiple
forms’.
• Polymorphism implementation in C++ can be
shown as in figure.
structured and procedural
programming
• 1. Structured Programming :
Structured Programming, as name suggests, is a technique that is
considered as precursor to OOP and usually consists of well-structured
and separated modules. In this programming, user can create its own
user-defined functions as well as this methodology tries to resolve issues
that are associated with unconditional transfers to allow programmers
follow logic of programs. It also requires more discipline at the design and
logical structuring stage.
• Example : Pascal, ALGOL, C, Modula-2, etc.
• 2. Object-Oriented Programming :
Object-Oriented Programming, as name suggests, is a different approach
to programming that brings together data and functions that execute on
them. It basically supports encapsulation, abstraction, inheritance,
polymorphism, etc. It also includes data hiding feature therefore it is more
secure. This model is based on real life entities that focuses on by whom
task is to be done rather than focusing on what to do.
• Example : JAVA, C#, C++, etc.
Structured Programming Object-Oriented Programming

It is a subset of procedural programming. It relies on concept of objects that contain data and code.

Programs are divided into small programs or functions. Programs are divided into objects or entities.

It is all about facilitating creation of programs with It is all about creating objects that usually contain both
readable code and reusable components. functions and data.

Its main aim is to improve and increase quality, clarity, and Its main aim is to improve and increase both quality and
development time of computer program. productivity of system analysis and design.

It simply focuses on representing both structure and


It simply focuses on functions and processes that usually
behavior of information system into tiny or small modules
work on data.
that generally combines data and process both.

It is a method of organizing, managing and coding


It is a method in which set of objects can vary dynamically
programs that can give or provide much easier modification
and can execute just by acting and reading to each other.
and understanding.

In this, methods are written globally and code lines are In this, method works dynamically, make calls as per need
processed one by one i.e., Run sequentially. of code for certain time.

It generally follows “Top-Down Approach”. It generally follows “Bottom-Up Approach”.

It provides less flexibility and abstraction as compared to It provides more flexibility and abstraction as compared to
object-oriented programming. structured programming.

It is more difficult to modify structured program and reuse It is less difficult to modify object-oriented programs and
code as compared to object-oriented programs. reuse code as compared to structured programs.

It gives more importance of code. It gives more importance to data.


Application of OOP
• Building complex systems and applications is
made possible by OOPs, it enables modular,
reusable, and maintainable programming.
• Software development,
• GUI development,
• game development,
• mobile app development,
• web development,
• database systems,
• and AI/ML are some of the main applications of
OOPs.

You might also like