Computer Programming II Lecture 5

advertisement
Computer Programming II
Lecture 5
Introduction to Object Oriented Programming
(OOP)
- There are two common programming methods : procedural
programming and object-oriented programming (OOP).
- Procedural programming : In a procedural program data is
typically stored in a collection of variables and there is a
set of functions that perform operations on the data. The
data and the functions are separate entities.
- Object-oriented programming : is centered around objects
that encapsulate both data and the functions that operate
on them.
Object Oriented Programming (OOP)
There are a few principle concepts that form the
foundation of object-oriented programming :
1- Abstraction :
- An abstraction is a general model of something. It is a
definition that includes only the general characteristics of
an object without the details that characterize specific
instances of the object.
- An abstract data type (ADT) is a data type that specifies
the values the data type can hold and the operations that
can be done on them without the details of how the data
type is implemented.
Object Oriented Programming (OOP)
2- Class:
A class is a programmer-defined data type that describes
what an object of the class will look like when it is
created. It consists of a set of variables and a set of
functions.
3- Objects:
Objects are instances of a class. They are created with a
definition statement after the class has been declared.
4- Encapsulation :
Encapsulation is placing the data and the functions that
work on that data in the same place.
Object Oriented Programming (OOP)
5- Inheritance:
- Inheritance allows to create classes which retain
characteristics of the base class.
- Inheritance involves a base class and a derived class. The
base class is the general class and the derived class is the
specialized class. The derived class is based on, or derived
from, the base class.
6- Polymorphism:
In programming languages, polymorphism means that some
code or operations or objects behave differently in
different contexts.
Classes and objects :
- In C++, the class is the construct primarily used to create
objects.
- A class is a programmer-defined data type that describes
what an object of the class will look like when it is
created. It consists of a set of variables and a set of
functions.
Classes and objects :
Declaration:
Classes are generally declared using the keyword class, with
the following format:
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Classes and objects :
- Where class_name is a valid identifier for the class,
object_names is an optional list of names for objects
of this class. The body of the declaration can contain
members, that can be either data or function
declarations, and optionally access specifiers.
- An access specifier is one of the following three keywords:
private, public or protected.
Classes and objects :
- These specifiers modify the access rights that the
members following them acquire:
• private members of a class are accessible only from
within other members of the same class or from their
friends.
•
protected members are accessible from members of their
same class and from their friends, but also from members
of their derived classes.
•
Finally, public members are accessible from anywhere
where the object is visible.
Classes and objects :
Example:
class Crectangle
{
private:
int x, y;
public:
void set_values (int a, int b);
int area ( );
} rect;
Classes and objects :
Accessing an Object’s Members:
- Public members of a class object are accessed with the
dot operator (.) .
- After the previous declarations of CRectangle and rect, we
can refer within the body of the program to any of the
public members of the object rect as if they were normal
functions or normal variables, just by putting the object's
name followed by a dot (.) operator and then the name of
the member.
For example :
rect.set_values (3,4);
myarea = rect.area();
This program is used to calculate the area of
rectangle.
Classes and objects :
Types of Member Functions:
- Accessor , get, getter function: uses but does not modify a
member variable.
- A function that uses the value of a class variable but does
not change it, is known as an accessor.
- Mutator , set, setter function: modifies a member
variable.
- A function that stores a value in a member variable or
changes its value, is known as a mutator.
Classes and objects :
Defining Member Functions:
- Member functions are part of a class declaration . Class
member functions can be defined either inside or outside
the class declaration.
- When a class function is defined within the class
declaration , it is called an inline function. Inline functions
provide a convenient way to contain function information
within a class declaration, but they can only be used when
a function body is very short, usually a single line.
Classes and objects :
- When a function body is longer, a prototype for the
function should appear in the class declaration, instead of
the function definition itself. The function definition is
then placed outside the class declaration.
- In the function definition, precede function name with
class name and scope resolution operator (::)
- The ( : : ) symbol is called the scope resolution operator. It
is needed to indicate that these are class member
functions and to tell the compiler which class they belong
to.
Classes and objects :
Classes and objects :
Write a C++ program to create a class called rectangle
which used to calculate the area of rectangle.
Area = Length * width.
Note :
Length and width are inserted by user.
Download