Object Oriented System Using C++: UNIT-1

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 44

Object Oriented System

using C++
UNIT-1

BY: VIVEK DIMRI


OBJECT ORIENTED PARADIGM
Paradigm Common Features
Demonstrates feasibility of
representation of problem
Machine solutions within computers.
 “Problems can be  
Machine Instructions (“Op
solved by running a codes”)
program in a Data representations
computer.” Input
Output
Paradigm Common Features
Improves readability of
Mnemonic (or computer programs by
Symbolic) using commands that
  looked words and numbers
“Programs should be that looked like numbers.
 
written in a manner Variable declarations
that makes them easily Assignment statements
understood by human Global scope of identifiers
Absolute, conditional jumps
beings.”
# Statement labels
# GOTO statements
Comments
Paradigm Common Features
Simplify understandability
 Modular of programs by partitioning
  of large problems into
“Large programs should smaller problems that are
be composed of smaller more easily solved.
 
blocks of code.”
Subroutines, functions,
procedures
# Return statement
Formal/actual arguments
Separation of code/data
Local Scope of identifiers
Function libraries
Paradigm Common Features
Improve readability of programs
 Structured by limiting the number of
  arbitrary ways in which
statements can be put together. 
“Proper programs Sequence
should be coded in a # Blocks
# Block scope of identifiers
way that minimizes the Selection
number of detours # If/Else statements
# Case/Switch statements
from the main flow of Iteration
the program.” # Do/For/While/Until
# Recursion
Exception handling
# Throw/Raise statements
# Catch statements
Controlled jumps
# Break/Continue/Cycle
Paradigm Common Features
Simplifies programs by placing
  Object-oriented logically related parts close
  together and by having similar
“Programs should be parts of the program behave
written as a collection in a similar manner.
 
of otherwise Encapsulation
independent objects # Classes and objects
# Fields/Attributes
that interact with one # Methods
another.” Inheritance
# Constructors and Factories
Polymorphism
#
Interfaces/abstractions/pure
Paradigm Common Features
Makes programs more
   Event-driven understandable by
 
implementing common
“Programs should be
frameworks and
written as responders conventions followed by
to external and internal all programmers.
stimuli.”
Events
Event generators
Event handlers
Paradigm Common Features
Facilitates
    Visual communication between
 
programmers and users
“Programs should be
by using standard and
written by focusing on expected user-oriented
the interfaces first and components.
adding details later.”  
Visual
components/containers
Properties
Events
Structured versus Object oriented
development
Procedural Programming
Before Object Oriented Programming (OOP) was
popular, computer software was written in a
Procedural style.

This meant that the application/program was divided


into a large number of procedures. C is largely
procedural, and to a certain degree C++ and C# are
as well
Structured Programming

Structured programming was first suggested by


Corrado Bohm and Guiseppe Jacopini.

The two mathematicians demonstrated that any


computer program can be written with just three
structures:
 Decisions
 Sequences
 loops
A popular design methodology was "Structured
Programming" but that has been rendered
obsolete by OOP
Structured Programming

Structured programming (sometimes known as


modular programming) is a subset of procedural
programming that enforces a logical structure on
the program being written to make it more
efficient and easier to understand and modify.
Certain languages such as Ada, Pascal, and
dBASE are designed with features that
encourage or enforce a logical program
structure.
Often planned using a Top-down Design or
Hierarchy chart.
Procedure Program view
Main Program
Data

Procedure1 Procedure2 Procedure3


Structured Programming

Focus is on procedures

All data is shared: no protection

More difficult to modify

Hard to manage complexity


Object-Oriented Programming
Object-oriented programming is an approach that
provides a way of modularizing programs by creating
partitioned memory area for both data and functions
that can be used as templates for creating copies of
such modules on demand.

This means that an object is considered to be a


partitioned area of computer memory that stores
data and set of operations that can access the data.

Since the memory partitions are independent, the


objects can be used in a variety of different
programs without modifications

Object = Data + Methods (Functions)


Object1
Data1+Procedures1

Object2

Data
Data2 + Procedures
Data12
Object3
Data3 + Procedures3

Object4

Data4 + Procedures4
Concept /Elements of OOPs
 Object
 Class
 Data abstraction and encapsulation
 Inheritance
 Polymorphism
 Dynamic Binding
 Message Passing
OOP Basic Concepts/ Elements

Object:
An object is a concept, abstraction, or thing with identity
that has meaning for an application
All objects have identity and are distinguishable. For
example- Two apples with the same color, shape, and
texture are still individual apples
An object contains both data and methods that
manipulate that data
“Object” is an instance of a class. Each object has a
class which defines its data and behavior
Objects are the basic run-time entities in an object-
oriented system.
OOP Basic Concepts/ Elements

Objects take up space in memory and have an


associated address like a record in Pascal, or a
structure in C.
When a program is executed, the objects interact
by sending messages to one another.
Object
Own copy of data
Active in running program
Occupies memory
Has the set of operations given in the class
Person
Age:34
OOP Basic Concepts/ Elements

Class:
A class captures the common properties of
the objects instantiated from it
A class characterizes the common behavior
of all the objects that are its instances
Classes reflect concepts, objects reflect
instances that embody those concepts.
“Class” refers to a blueprint. It defines the
variables and methods the objects support
OOP Basic Concepts/ Elements

Class:
Every object belongs to (is an instance of) a
class
An object may have fields, or variables
The class describes those fields
An object may have methods
The class describes those methods
A class is like a template, or cookie cutter
OOP Basic Concepts/ Elements
Class
Employee

Name, designation
salary

Cal salary

Class employee
class class_name {
{ char name[20];
data types char desg[20];
member functions int salary;
}; public:
int calsalary()
(syntax of class) };
(Implementation of class)
OOP Basic Concepts/ Elements

Encapsulation:
Wrapping up data methods in to a single unit
(class) is encapsulation. Data is accessible
only through its methods. (Protection))
Advantages of Encapsulation:
Protection
 Consistency
 Allows change
OOP Basic Concepts

Data Abstraction:
Refers to the act of representing essential
features without including the background
details or explanations. (Classes use the
concept of abstraction and are defined as a
list of abstract attributes such as size, weight
and cost, and methods operate on these
attribute)
OOP Basic Concepts

Inheritance:
The process by which object of one class
acquire the properties of objects another
class.
The sub-class inherits the base class’s data
members and member functions
A sub-class has all data members of its
base-class plus its own
A sub-class has all member functions of its
base class (with changes) plus its own
OOP Basic Concepts

Animal

Mammal Reptile

Rodent Primate Cats

Mouse Squirel Rabbit


OOP Basic Concepts

Polymorphism:
Ability to take more than one form. For ex. An
operation may exhibit different behavior in
different instances. The behavior depends upon
the type of data used in the operation.
For ex. The operation of addition. For numbers,
the operation will generate a sum and if the
operands are strings then the operation would
produce a third string by concatenation.
A single function name can be used to handle
different no and different arguments
OOP Basic Concepts

Types of Polymorphism:

Compile Time
 Method overloading
 Operator overloading
Run Time
 Overriding
 virtual function
Dynamic Binding
 Binding refers to the linking of a procedure call to
the code to be executed in response to the call.
Dynamic binding (also known as late binding)
means that the code associated with a given
procedure call is not known until the time of the call
at run-time. It is associated with polymorphism and
inheritance. A function call associated with a
polymorphism reference depends on the dynamic
type of that reference.
Message Passing
 An object-oriented program consists of a set of objects that
communicate with each other. The process of programming in an
object-oriented language, therefore, involves the following basic
steps:
 Creating classes that define objects and their behavior,
 Creating objects from class definitions, and
 Establishing communication among objects.
 Objects communicate with one another by sending and receiving
information much the same way as people pass message to one
another. The concept of message passing makes it easier to talk
about building systems that directly model or simulate their real-
world counterparts.

 Example
 employee .salary (name);

 Object message infornation


Advantage of OOP

simplicity: software objects model real world objects, so the


complexity is reduced and the program structure is very clear;

modularity: each object forms a separate entity whose


internal workings are decoupled from other parts of the system;

modifiability: it is easy to make minor changes in the data


representation or the procedures in an OO program. Changes
inside a class do not affect any other part of a program, since
the only public interface that the external world has to a class is
through the use of methods;

extensibility: adding new features or responding to changing


operating environments can be solved by introducing a few
new objects and modifying some existing ones;
Advantage of OOP

maintainability: objects can be maintained separately,


making locating and fixing problems easier

re-usability: objects can be reused in different programs.

Provides a clear modular structure for programs. This


makes it good for defining abstract data types, where
implementation details are hidden and the unit has a clearly
defined interface.

Provides a good framework for code libraries, where


supplied software components can be easily adapted and
modified by the programmer. This is particularly useful for
developing graphical user interfaces.

Makes it easy to maintain and modify existing code, as new


objects can be created with small differences to existing ones.
COMPARISON BETWEEN OOP &
STRUCTURED PROGRAMMING

Look through the graph above. For simple


or easy (even for semi hard) programs,
procedural approach can be a good solution.
COMPARISON BETWEEN OOP &
STRUCTURED PROGRAMMING

But for the hard ones it is not.

On the other hand for simple problems,


approaching OOP concept is rather lengthy,
time consuming and not useful. It is however,
useful for tough problems.
COMPARISON BETWEEN OOP &
STRUCTURED PROGRAMMING

 Procedural  Object Oriented

Withdraw, deposit, transfer Customer, money, account


COMPARISON BETWEEN OOP &
STRUCTURED PROGRAMMING

OOP is easier to develop, debug, and maintain applications


then structured programming

The unit in procedural programming is function, and unit in


object-oriented programming is class

Procedural programming concentrates on creating functions,


while object-oriented programming starts from isolating the
classes, and then look for the methods inside them.

Procedural programming separates the data of the program


from the operations that manipulate the data, while object-
oriented programming focus on both of them
Structure Object Oriented
1. It is function oriented. 1. It is data oriented.

2. Data & function are not 2. Data & function are


combined together. combined together.

3. Data is not secure. 3. Data is secure.

4. It follows top town 4. It follows bottom up


approach. approach.

5. New data &function can not 5. New data &function can


easily added. easily added.
INTRODUCTION TO C++
History of C and C++
 History of C
 Evolved from two other programming languages
 CPL
 BCPL and B
 “Typeless” languages
 Dennis Ritchie (Bell Laboratories)
 C
 Added data typing, other features

 Extension of C
 Early 1980s: Bjarne Stroustrup (Bell Laboratories)
 C with class
 C++
 Provides capabilities for object-oriented programming
 Objects: reusable software components
 Model items in real world
 Object-oriented programs
 Easy to understand, correct and modify
Compilation Model
Program is created in
Editor
Phases of C++ Programs: Disk the editor and stored
on disk.

Preprocessor program
1. Edit Preprocessor Disk
processes the code.
Compiler creates
2. Preprocess Compiler Disk object code and stores
it on disk.
Linker links the object
3. Compile Linker Disk code with the libraries,
creates a.out and
stores it on disk
4. Link Primary
Memory
Loader

5. Load Loader puts program


in memory.
Disk ..

6. Execute ..
..

Primary
Memory
CPU CPU takes each
 
instruction and
executes it, possibly
storing new data
..
.. values as the program
..
executes.
Program Hello
// This program is to print hello

#include <iostream>
using namespace std;

int main() {
cout <<"Hello!"<< endl;
return 0;
}
Output & Input

cout << "Enter an integer: ";


 Output values to the screen
 Standard output stream (cout), or “see-out”
 Stream insertion operator (<<), or “put to”

cin >> int1;


 Obtain a value from the keyboard
 Standard input stream (cin), or “see-in”
 Stream extraction operator (>>), or “get from”
using namespace std;

This tells the compiler to use the std namespace.


Namespaces are a recent addition to C++.
A namespace creates a declarative region in which
various program elements can be placed.
Namespaces help in the organization of large
programs.
The using statement informs the compiler
that you want to use the std namespace. This is the
namespace in which the entire Standard C++ library
is declared. By using the std namespace you
simplify access to the standard library.
Program Average
#include <iostream.h>
using namespace std;

int main() {
int x;
int y;
cout <<"Enter two numbers \n";
cin >> x >> y;
cout <<"Their average is: ";
cout << (x + y)/2.0 << endl;
return 0;
}
Out put
Enter two numbers 8 10
Their average is: 9

You might also like