Object Oriented System Using C++: UNIT-1
Object Oriented System Using C++: UNIT-1
using C++
UNIT-1
Focus is on procedures
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
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
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);
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
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
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