0% found this document useful (0 votes)
9 views50 pages

1stand2nd Class UNIT 1 OOP.pptx

The document provides a comprehensive overview of Object Oriented Programming (OOP) with C++, covering key concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It discusses the advantages and disadvantages of OOP, compares it with procedural programming, and outlines the importance of UML in software design. Additionally, it includes references to preferred textbooks and various topics related to OOP, including exception handling, templates, and file I/O operations.
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)
9 views50 pages

1stand2nd Class UNIT 1 OOP.pptx

The document provides a comprehensive overview of Object Oriented Programming (OOP) with C++, covering key concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It discusses the advantages and disadvantages of OOP, compares it with procedural programming, and outlines the importance of UML in software design. Additionally, it includes references to preferred textbooks and various topics related to OOP, including exception handling, templates, and file I/O operations.
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/ 50

Object Oriented Programming with C++

Unit No. Unit Content


Introduction to object oriented approach: Why object oriented programming?- Characteristics of
object oriented language: classes and objects - encapsulation-data abstraction- inheritance -
1 polymorphism – Merits and Demerits of object oriented programming. UML- class diagram of OOP -
Inline function – default argument function- Exception handling(Standard) - reference: independent
reference – function returning reference – pass by Reference
Classes and objects: Definition of classes – access specifier – class versus structure – constructor –
2 destructor – copy constructor and its importance – array of objects – dynamic objects- friend
function-friend class – container Class
Polymorphism and Inheritance :
Polymorphism-compile time polymorphism – function overloading – operator overloading - .
3 Inheritance-types of inheritance- constructors and destructors in inheritance – constraints of multiple
inheritance-Abstract base class – pure virtual functions- run time polymorphism-function overriding.
Exception handling and Templates
4 Exception handling(user-defined exception)- Function template , Class template – Template with
inheritance , STL – Container, Algorithm, Iterator - vector, list, stack, map
IOstreams and Fi les
5 IOstreams, Manipulators- overloading Inserters(<<) and Extractors(>>)- Sequential and Random files
– writing and reading objects into/from files – binary files
Preferred Text Books

► Text Books
► Stanley B Lippman, Josee Lajoie, Barbara E, Moo, “C++ primer”, Fifth edition, Addison-Wesley,
2012
► Bjarne Stroustrup, The C++ programming Language, Addison Wesley, 4th edition, 2013
► Harvey M. Deitel and Paul J. Deitel, C++ How to Program, 7th edition, Prentice Hall, 2010
► Maureen Sprankle and Jim Hubbard, Problem solving and Programming concepts, 9th
edition,Pearson Eduction, 2014
Outline

► Introduction to OOP

► Why Object Oriented Programming ► Inline function


► Characteristics of OOP languages ► Default argument function
► Exception handling
► Classes and Objects
► Encapsulation, Data Abstraction, Inheritance, ► Reference
Polymorphism ► Independent reference
► Merits and Demerits of Object Oriented Programming ► Function returning reference
► UML –Class diagram of OOP ► Pass by reference
Introduction to OOP

► Computers are everywhere (Cars, Phones, laptops, cameras, televisions etc.)


► Used to do various activities (search the internet, book tickets, listening music, watching videos
etc.)
► What is a computer- (receive data from the external world, perform calculation, send the result
back)
► What calculation are performed? – Determined by the program loaded in a computer
► Program are the set of instructions – a precise description of the calculations we want the
computer to perform.
► Programs are written in special notations: “Programming Language”
Introduction to OOP

► Programming Languages
► Evolution : Low level languages (Machine programming using 0’s and 1’s)
Middle level language (Assembly programming using “mnemonics”)
High level language (Procedure-Oriented or Structured Programming Languages
and Object – Oriented Programming Languages using general
English words with set of grammar rules)
► Are all functional…!
Introduction to OOP

► Programming Languages
► Evolution : Low level languages (Machine programming using 0’s and 1’s)
Middle level language (Assembly programming using “mnemonics”)
High level language (Procedure-Oriented or Structured Programming Languages
and Object – Oriented Programming Languages using general
English words with set of grammar rules)
► Are all functional…!
“YES”

► Why Object Oriented Programming?


► Limitation with Procedure-Oriented or Structured Programming
Introduction to OOP

► Basic differences between Procedure Oriented and Object Oriented Programming


Procedural Oriented Programming Object Oriented Programming
It follows top down approach, emphasis Emphasis on Data rather than procedure
is on functions and operations over
actual data
Instructions are given to the computer Programs are divided into Objects
using algorithmic approach
Group of similar tasks are organized as Data structures are designed such that they
functions characterize the Objects
Most functions shared global data Functions that operate on the data of an
object are tied together in that same data
structure
Data is publicly available Data is hidden and cannot be directly
accessed
Introduction to OOP
Introduction to OOP

► Major limitations with Procedure-Oriented Programming


are
► Program becomes complex with the use of lots of functions and the
inter-dependability among them

► Though parts of procedural language are reusable within the same


project, developers can't use the code they write in one project for
another project: have to rewrite same code many times if required,
i.e. Inability to reuse code through the program
► This adds development cost and time of a project

To overcome the limitation: Object Oriented Paradigm is designed


Introduction to OOP

► Major limitations with Procedure-Oriented Programming


are
► Difficulty in error checking : Program is continuously broken down
into smaller manageable pieces with the use of structural blocks
(Decision making blocks, Repetitive blocks and functions).
► Finding error or debugging becomes more and more difficult as the code
gets longer

► Scalability is also difficult

To overcome the limitation: Object Oriented Paradigm is designed


Introduction to OOP

► Object Oriented Programming paradigm


► Object-oriented programming is a style of programming that rely on classes, objects, encapsulation,
inheritance, and polymorphism
OR
► Object Oriented programming (OOP) is a programming paradigm that relies on the concept
of classes and objects.
► It is used to structure a software program into simple, reusable pieces of code blueprints (usually called
classes), which are used to create individual instances of objects.
► There are many object-oriented programming languages including JavaScript, C++, Java, and Python.
Introduction to OOP

► Object Oriented Programming paradigm


► Object-oriented programming is a style of programming that rely on classes, objects, encapsulation,
inheritance, and polymorphism
OR
► Object Oriented programming (OOP) is a programming paradigm that relies on the concept
of classes and objects.
► It is used to structure a software program into simple, reusable pieces of code blueprints (usually called
classes), which are used to create individual instances of objects.
► There are many object-oriented programming languages including JavaScript, C++, Java, and Python.

What is so great about classes?


Introduction to OOP

► Class
► A class is the representation of an idea, a concept, in the code
► Classes significantly helps maintenance by allowing us to avoid making mistakes and finding
bugs easily if mistake happens
► In a nutshell,
► Classes are essentially user defined data types. Classes are where we create a layout for the structure of
methods and attributes
► Individual objects are instantiated, or created from this blueprint.
► Classes contain fields for attributes, and methods for behaviors
Introduction to OOP

► Objects
► Objects are the basic run-time entities in an OO system, means problems are decomposed into
number of entities called objects and then builds data and functions around these objects
Solving the problem is mainly done by focusing on entities rather than the functions to be performed by the
entities
► Objects can be a person, a place, a bank account, a table of data etc.
► Objects are instances of classes containing data and code to manipulate the data.
► Data means variables (properties) and code means methods (functions).
► In a simple term, objects are the variable of type Class.
► As they are variable, they take space in memory and have an associated address
Introduction to OOP

► Examples
Introduction to OOP

► Examples
Introduction to OOP

► Building Blocks of Object Oriented Programming


► Classes
► In our Dog class example, attributes can be name & birthday, while methods
include bark() and updateAttendance().
► In our Car class example, attributes include steering, break, tyres etc., while methods include forward(),
backward(), accelerate(), deaccelerate().

► Objects
► In our examples, Rufus is an instance of the Dog class, Fluffy & Maisel are the instances of Herding
Dog class, Ford and Toyota are the instances of Car class
Introduction to OOP

► In programming context
Introduction to OOP

► Data encapsulation and Data abstraction

► Data encapsulation is a mechanism of bundling the data and the functions that use them OR grouping of data
and functions into a single unit called class.
► The data is not accessible to the outside world (other functions of the program).
► Only those functions which are wrapped in the class can access it.
► Functions provide an interface between the objects data and the program
► This insulation of data from direct access by the program is called data hiding or information hiding

► Data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from
the user.
OR
It refers to the act of representing essential features without including the background details or explanation.
Introduction to OOP

► Inheritance
► Inheritance supports reusability, it allows classes to inherit features of other classes.
► Class whose properties gets inherited is called Parent class and the class which inherits the properties of other
class is called Child Class
► Put another way, parent classes extend attributes and behaviors to child classes.
► If basic attributes and behaviors are defined in a parent class, child classes can be created extending the
functionality of the parent class, and adding additional attributes and behaviors.
► For example, herding dogs have the unique ability to herd animals. In other words, all herding dogs are dogs, but
not all dogs are herding dogs. We represent this difference by creating a child class Herding Dog from the parent
class Dog, and then add the uninique herd() behavior.
► The benefits of inheritance are programs can create a generic parent class, and then create more specific child
classes as needed. This simplifies overall programming, because instead of recreating the structure of
the Dog class multiple times, child classes automatically gain access to functionalities within their parent
class.
Introduction to OOP

► Polymorphism
► Polymorphism is extensively used in implementing inheritance
► In Greek term, Polymorphism means the ability to take more than one form.
► Operator Overloading
► An operation may exhibit different behaviors in different instances. The operation depends on the type of data
used in the operation.
► For eg. : “+” for integers is addition of two numbers [2 + 3 = 5]
for strings is concatenation of two strings [Ram + Eats = RamEats]
► Function Overloading
► Similarly, using a single function name to perform different types of tasks within a class with different
function parameters, like Draw() function name can be defined differently for circle, square, rectangle with
the same name Draw().
Introduction to OOP

► Another example programming context


Introduction to OOP

► Polymorphism

► Function Overriding
► Using a single function name to perform
different types of tasks in Parent and child
classes with same function parameters.
Introduction to OOP

► Building Blocks of Object Oriented Programming


► Classes
► Objects
► Methods ► Principles of Object Oriented Programming
► Attributes ► Data Encapsulation
► Data abstraction
► Inheritance
► Polymorphism
Introduction to OOP

► Promising areas for Application of OOP


► Simulation and modeling
► Real-time systems
► Object-oriented databases
► AI and expert systems
► Neural networks and parallel programming
► Hypertext, hypermedia and expertext
► And many more……
Topics Covered till now

► Why Object Oriented Programming


► Characteristics of OOP languages
► Classes and Objects
► Encapsulation, Data Abstraction, Inheritance, Polymorphism
Class Contents

► UML – Class diagram of OOP


► Beginning with C++
UML Class Diagram

► UML: The Unified Modeling Language (UML) is an industry-standard language for


specifying, visualizing, constructing, and documenting the artifacts of software
systems

► The UML definition was led by Grady Booch, Ivar Jacobson, and Jim Rumbaugh

► UML simplifies the process of software design, making a "blueprint" for construction

► In Essence: a tool used through the analysis and design phases of system development
for expressing the constructs and relationships of complex systems
UML Class Diagram

Class representation
• Describe the structure of the system in terms of classes and objects

• Primary purpose during analysis workflow: to create a vocabulary that is used by both
the analyst and users

ClassName A class is a description of a set of objects that share the same


attributes, operations, relationships, and semantics.
attributes Graphically, a class is rendered as a rectangle, usually
including its name, attributes, and operations in separate,
designated compartments.
operations
UML Class Diagram

The name of the class is the only required tag in


ClassName the graphical representation of a class. It always
appears in the top-most compartment.

attributes

operations
UML Class Diagram

Person Class Attributes

An attribute is a named property of a


name : String class that describes the object being modeled.
address : Address
birthdate : Date In the class diagram, attributes appear in
ssn : Id the second compartment just below the
name-compartment.
Classes Attributes (Cont’d)

Attributes are usually listed in the form:


Person
attributeName : Type

name : String A derived attribute is one that can be


address : Address computed from other attributes, but
birthdate : Date doesn’t actually exist. For example,
/ age : Date a Person’s age can be computed from
ssn : Id his birth date. A derived attribute is
designated by a preceding ‘/’ as in:

/ age : Date
Classes Attributes (Cont’d)

Attributes can be:


Person + public
# protected
- private
/ derived Visibility Symbol Accessible To
+ name : String
# address : Address
# birthdate : Date Public + All objects within your system
/ age : Date Protected # Instances of the implementing
- ssn : Id class and its subclasses.

Private - Instances of the implementing


class.
Classes Operations

Person

name : String
address : Address
birthdate : Date
ssn : Id

eat
sleep Operations describe the class behavior
work and appear in the third compartment.
play
Classes Operations cont’d

PhoneBook

newEntry (n : Name, a : Address, p : PhoneNumber, d : Description)


getPhone ( n : Name, a : Address) : PhoneNumber

You can specify an operation by stating its signature: listing the name,
type, and default value of all parameters, and, in the case of functions, a
return type.
Relationships

In UML, class interconnections (logical or physical), are modeled as


relationships.

There are three kinds of relationships in UML:

• Inheritance: (generalizations and specialization)

• associations
•Aggregate
•Composite
Inheritance Relationship

Person

Employee Customer

Manager Engineer
Inheritance Relationship Employee
hireDate

receivePay
performWork

Manager Engineer
department certifications
bonus
hireEmployee analyze
promoteEmployee design
Inheritance

Person Generalization and Specialization

Inheritance connects a subclass to its superclass.

Generalization denotes an inheritance of


attributes and behavior from the superclass to the
subclass

And denotes specialization in the subclass of the


Student more general superclass.
Inheritance

Generalization:
Person Employee
UML permits a class to inherit
from multiple superclasses,
although some programming
languages (e.g., Java) do not
permit multiple inheritance.

Teacher
Association Relationships

If two classes in a model need to communicate with each other, there must
be link between them.

An association denotes that link.

A simple association is the relationship which describes the way they are
related to each other
Association Relationships cont’d

We can indicate the multiplicity of an association by adding multiplicity


adornments to the line denoting the association.

The example indicates that a Student has one or more Instructors:

Student Instructor
1..*
Association Relationships cont’d

The example indicates that every Instructor has one or more Students:

Student Instructor
1..*
Association Relationships cont’d

We can also indicate the behavior of an object in an association (i.e.,


the role of an object) using rolenames.

teaches learns from


Student Instructor
1..* 1..*
Association Relationships cont’d

We can also name the association.

membership
Student Team
1..* 1..*

eats
Birds Insects
0..* 0..*
Association Relationships cont’d

We can specify dual associations.

member of

1..* 1..*
Student Team

1 president of 1..*
Association Relationships cont’d

We can model objects that contain other objects by way of special


associations called aggregations and compositions.

An aggregation specifies a whole-part relationship between an aggregate


(a whole) and a constituent part, where the part can exist independently
from the aggregate. Aggregations are denoted by a hollow-diamond
adornment on the association.

Engine
Car
Transmission
Association Relationships cont’d

A composition indicates a strong ownership and coincident lifetime of


parts by the whole (i.e., they live and die as a whole). Compositions are
denoted by a filled-diamond adornment on the association.

Scrollbar
1 1

Window Titlebar
1 1

Menu
1 1 .. *
Basic of C++

Download Dev C++ from: https://sourceforge.net/projects/orwelldevcpp/

We will have just a look on how to work in C++ environment

You might also like