Anda di halaman 1dari 32

OBJECT ORIENTED

PROGRAMMING
C++
EXAMPLE 1
Uitati 2 cijela broja i izraunati im zbir, razliku, proizvod i kolinik.
Ispisati najmanji od 4 dobijena rezultata bez ponavljanja.
LETS REPEAT
Napiite program koji za unesena dva cijela broja ispisuje prvo
vei pa manji broj!
LETS REPEAT
LETS REPEAT

Decision making structures require that the programmer specify one


or more conditions to be evaluated or tested by the program, along
with a statement or statements to be executed if the condition is
determined to be true, and optionally, other statements to be
executed if the condition is determined to be false.
Following is the general form of a typical decision making
structure found in most of the programming languages:

OBJECT ORIENTED
PROGRAMMING
BASIC CONCEPT OF OOP

Abstract data types


Encapsulation
Operator overloading
Inheritance
Polymorphism

This is all native concepts in C++


ABSTRACT DATA TYPES
In computer science, an abstract data type (ADT) is a mathematical
model for data types where a data type is defined by its behavior
(semantics) from the point of view of a user of the data, specifically in
terms of possible values, possible operations on data of this type, and
the behavior of these operations. This contrasts with data structures,
which are concrete representations of data, and are the point of view
of an implementer, not a user.

Abstract data types are purely theoretical entities, used (among


other things) to simplify the description of abstract algorithms, to
classify and evaluate data structures, and to formally describe
the type systems of programming languages. However, an ADT may
be implemented by specific data types or data structures, in many
ways and in many programming languages; or described in a formal
specification language ADTs are often implemented as module: the
module's interface declares procedures that correspond to the ADT
operations, sometimes with comments that describe the constraints.
This information hiding strategy allows the implementation of the
module to be changed without disturbing the client programs.
ENCAPSULATION
Encapsulation is an Object Oriented Programming concept that binds
together the data and functions that manipulate the data, and that
keeps both safe from outside interference and misuse.

Data encapsulation led to the important OOP concept of data hiding.


Data encapsulation is a mechanism of bundling the data, and the
functions that use them and data abstraction is a mechanism of
exposing only the interfaces and hiding the implementation details
from the user.

C++ supports the properties of encapsulation and data hiding


through the creation of user-defined types, called classes. We
already have studied that a class can contain private,
protected and public members. By default, all items defined in a
class are private
OPERATOR OVERLOADING
To overload an operator, a special operator function is defined inside
the class as:

Here, returnType is the return type of the function.


The returnType of the function is followed by operator keyword.
Symbol is the operator symbol you want to overload. Like: +, <, -, ++
You can pass arguments to the operator function in similar way as
functions.
OPERATOR OVERLOADING

Operator overloading allows you to redefine the way operator works


for user-defined types only (objects, structures). It cannot be used for
built-in types (int, float, char etc.).

Two operators = and & are already overloaded by default in C++. For
example: To copy objects of same class, you can directly use =
operator. You do not need to create an operator function.

Operator overloading cannot change the precedence and


associatively of operators. However, if you want to change the order of
evaluation, parenthesis should be used.

There are 4 operators that cannot be overloaded in C++. They


are :: (scope resolution), . (member selection), .* (member selection
through pointer to function) and ?: (ternary operator).
INHERITANCE

Inheritance in Object Oriented Programming can be described as a


process of creating new classes from existing classes. New
classes inherit some of the properties and behavior of the existing
classes. An existing class that is "parent" of a new class is called a base
class.
POLYMORPHISM

The word polymorphism means having many forms.


Typically, polymorphism occurs when there is a hierarchy of classes
and they are related by inheritance. C++ polymorphism means that a
call to a member function will cause a different function to be
executed depending on the type of object that invokes the function.
POLYMORPHISM

C++ is not pure object oriented programming language that force


user to use on OO way.

C++ should use as medium and guidepost for thinking.

In OOP, projecting is majority of the problem, coding is just


implementing.
CLASS

he main purpose of C++ programming is to add object orientation to


the C programming language and classes are the central feature of
C++ that supports object-oriented programming and are often called
user-defined types.

A class is used to specify the form of an object and it combines data


representation and methods for manipulating that data into one neat
package. The data and functions within a class are called members of
the class.
CLASS DEFINITION

When you define a class, you define a blueprint for a data type. This
doesn't actually define any data, but it does define what the class
name means, that is, what an object of the class will consist of and
what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class
name; and the class body, enclosed by a pair of curly braces. A class
definition must be followed either by a semicolon or a list of
declarations. For example, we defined the Box data type using the
keyword class as follows
CLASS ACCES

The keyword public determines the access attributes of the members


of the class that follow it.

A public member can be accessed from outside the class anywhere


within the scope of the class object. You can also specify the members
of a class as private or protected which we will discuss in a sub-section.
DEFINE OBJECTS

A class provides the blueprints for objects, so basically an object is


created from a class. We declare objects of a class with exactly the
same sort of declaration that we declare variables of basic types.
Following statements declare two objects of class Box:

Box Box1; // Declare Box1 of type Box


Box Box2; // Declare Box2 of type Box

Both of the objects Box1 and Box2 will have their own copy of data
members.
ACCESSING THE DATA MEMBERS

A class provides the blueprints for objects, so basically an object is


created from a class. We declare objects of a class with exactly the
same sort of declaration that we declare variables of basic types.
Following statements declare two objects of class Box:

Box Box1; // Declare Box1 of type Box


Box Box2; // Declare Box2 of type Box

Both of the objects Box1 and Box2 will have their own copy of data
members.
ACCESSING THE DATA MEMBERS

The public data members of objects of a class can be accessed using


the direct member access operator (.). Let us try the following example
to make the things clear:
CLASS & OBJECTS IN DETAIL
So far, you have got very basic idea about C++ Classes and Objects.
There are further interesting concepts related to C++ Classes and
Objects which we will discuss in various sub-sections listed below:
EXAMPLE 1
EXAMPLE 1.1
EXAMPLE 2
EXAMPLE 3
CLASSES

Variable defined in some function member are visible just in that


function.

If variable is defined with same name in some function member as


class member, then variable is mask that member.

Masked data member can be accessed using :: operator


CLASSES

After defining a class you can do:

Define object and arrays of objects ([])

Define pointer (*) and address (&) on objects

Value assignment (=) of one object to another

Get address of object (&) and accessing objects using pointers or


indexing.

Accessing members of objects directly(.) or indirectly (->)


CLASSES

Most commonly implemented function member:

Function for setting attribute:


void setAttribute(type of attribute)

Function for getting attrbutes:


type getAttribute()
CLASSES

Class member are available to all other class member by calling their
names.

Accessing class members out of class by:

for objects:
object.member
for reference:
reference.member

for pointer:
pointer->member or (*pointer*).member
HOMEWORK
OBJECT ORIENTED
PROGRAMMING
C++

Anda mungkin juga menyukai