Você está na página 1de 16

OBJECT ORIENTED

PROGRAMMING
IN C++

Objective

A crash programme on C++


and thorough understanding of
object-oriented concepts
Write C++ programs using C++
concepts
Get a better understanding of
solving problems using C++

Goal
Upon the completion of the session,
you should be able to know
Structured Programming ~Object
Oriented Programming
Software Retrospection and Need
of OOPs
OOPs- Characteristics
Classes and Objects

C++

C++ is a generic programming


language, developed by Bjarne
Stroustrup in early 80s at AT & T Lab.
It is better than C
Object Oriented Programming
More appropriate for commercial
application and system programming

Procedural verses Object


Oriented
Emphasis on Functions
Programs are divided
into functions
Share Global data
Functions transform data
Top down Approach

Emphasis on Data
Programs are divided
into Objects
Data is hidden and
highly protected
Objects communicate
each other using
functions
Bottom-up Approach

OOPs Definition

Object Oriented Programming is a


method of implementation in which
programs are organized as collection of
objects, each of which represent an
instance of classes.

Object Oriented Programming , Grady Booch

// Fig. 1.2: fig01_02.cpp

// A first program in C++

Comments

#include <iostream>

1. Comments
Written between /* and */ or following
a //.

4
5

int main()

Improve program readability and do not cause the


computer to perform any action.

1. 2. Load <iostream>

preprocessor directive
cout << "Welcome to C++!<<endl;

Message to the C++ preprocessor.

3. main

Lines beginning with # are preprocessor directives.

return 0;

// indicate that program ended successfully

3.1 Print "Welcome to C+

#include <iostream> tells the


preprocessor to
+\n"
include
the contents
theor
file
<iostream>,
which
C++
programs
containofone
more
functions, one
of
3.2 exit
(return to
0)
includes
input/output
as printing
which
must
be main operations (such
the screen).
Parenthesis are used to indicate a function

10 }

Welcome to C++!

Prints the string of characters


contained
between
the an integer
int means
that main
"returns"
Program value.
Output
quotation marks.
More in Chapter 3.
return is a way to exit a function
from a function.
A left
brace { begins
The entire line, including
std::cout,
the the
<< body of every function
and a right to
braceC++!\n"
} ends it. and
operator,
return 0, in this case,
means the
thatstring "Welcome
semicolon (;), is called a statement.
the program terminatedthe
normally.
<<
Stream insertion operator
Value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
cout << Welcome to C++!<<endl;

// Fig. 1.6: fig01_06.cpp

// Addition program

#include <iostream>

4
5

int main()

int integer1, integer2, sum;

// declaration

8
9

cout << "Enter first integer\n";

// prompt

10

cin >> integer1;

// read an integer

11

cout << "Enter second integer\n"; // prompt

12

cin >> integer2;

13

sum = integer1 + integer2;

14

cout << "Sum is " << sum << endl; // print sum
return 0;

2.2 Print "Enter first


integer"
is used
to get user

Notice how std::cin


2.2.1 Get input2.3 Print
input.
"Enter second

// read an integer
// assignment of sum

integer"
2.3.1 Get input2.4 Add
variables and put result into

15
16

1. Load <iostream>
2. 2. main
2.1 Initialize variables
integer1, integer2,
and sum

flushes the buffer and


prints a newline. sum2.5 Print "Sum is"

// indicate that program ended successfully


std::endl

17 }

Enter first integer


45
Enter second integer
72
Sum is 117

Variables can be output using std::cout << variableName.


2.5.1 Output sum
2.6 exit (return 0)
Program Output

Object Oriented Programming


Objects and Classes
They are the basic building blocks of object oriented
technology
When entities are abstracted, and the abstraction
defines the class and specific instances (examples)
form the objects
Class provides the description of an abstraction
applying to a set of objects sharing a common
structure and behavior
Everything about an object is defined in the class

Object Oriented Programming


Objects
Objects consist of
visible behavior and structure as abstracted and
implementation details encapsulated and hidden
An object has
An identity of its own
State
Interface by which the clients are serviced
Implementation of its behavior

Object Oriented Programming


Object Identity
Objects thus have a unique existence
(in memory, from programming point of view)
Each object is distinguished by its name or
a reference (to the space in memory it occupies)
Objects are created at the beginning of their life
span and are destroyed at the end of it
Identity of an object is the means by which clients
can refer to that object.

Object Oriented Programming


Object State
represented by attributes defined as part of its
structure and its current values
When an object interacts with other objects,
and clients perform operations on the object,
Behavior of the object depends on its state
and
Also, methods invoked often alter the state

Object Oriented Programming


Object Behavior
Activity, exhibited by the object in response to
operations on it, that is visible outside
When an object interacts with other objects,
and clients perform operations on the object,
behavior of the object depends on its state

Object Oriented Programming


Methods
Operations are performed by the client by passing
a message to the object. The operations that can be
performed, and the format of the message (or the
"signature" that has to be passed), are defined in
the class as "methods
All the methods of a class together constitute the
interface used by the client
Methods are implemented as functions in the class
Terms operations, messages, methods, and memberfunctions are all used interchangeably

Object Oriented Programming


Methods (Contd)
Two other essential operations are
Constructor initializing the state of an object
(when it is created)
Destructor freeing the state and destroys the object itself
Constructors make sure that the object is well created, and
hence can receive messages.
Eg. Say, Account object has methods such as withdraw() and
deposit(), and the current balance as its state. Minimum deposit
should be made before opening an account, and before one can
do any transactions on that account.

Object Oriented Programming


Classes
Describes the interface and the implementation of
the set of objects
Interface of a class provides outside view (like a
contractual agreement) and declares the expected
behavior of objects of this class in the context of
interaction with other objects.
Implementation of a class is the inside view or the
Implementor's View of how the the behavior as
defined in the interface is accomplished.

Você também pode gostar