Vous êtes sur la page 1sur 12

Wk2: object Orientation

OOP (C++) / Dr. I. H. Shah.


Object Orientation
o In real life an object is an entity that:
 is visible or otherwise tangible.
 acts or can be acted upon.
o In software an object is:
 A computational resource of our system that parallels an object of real life.
 A unit that groups together data and functions which act upon that data.
o Software objects exist in time and can be created, destroyed, copied,
shared and updated.
o Every software object is an instance of some class / type.
 A class is blueprint / template from which objects are generated.
 A class defines a set of values and a set of operations applicable to an
object generated from that class.
What is a Class?
A class consists of : The class name
1. data (contained in variables) Data
(variables)
2. a set of functions which can access or
change the data.
person
• The data and functions are also called
members of the class. name : String
gender : char
• Example: Let us define a class person; to
id : integer
begin with this class has three data
items (attributes) and no functions. The
following is the illustration of this class in
UML notation.

Functions
Private and Public Members
• A class can contain private as well as public members; by default all
items defined in a class are private. A private member cannot be
accessed directly; it can be accessed through a public function only.
• The public members can be accessed directly by other parts of the
program.
• When a class is defined, things are normally arranged in such a way
that the data can be accessed / manipulated only by the functions of
that class; in this way the class is closed off and its data items are
protected from the outside interference. This is called encapsulation.
Encapsulation
• An Object Oriented Programming Language facilitates:
1. Defining a class
2. Restricting access to the members of the class
• The attributes of a class are defined as private and the methods are
defined as public. A private attribute can be accessed by a method of
the class only.
• Example1:
To understand encapsulation, let us first design, implement and use a
class; one such class is Rectangle contained in a file rectangle.cpp The
attributes of the class are not directly accessible.
The Syntax of a class
class Class-name The access specifiers can be;
{ private data and functions
public, private or protected.
access specifier
data and functions By default data and functions defined in
a class are private to that class and may be
access specifier
accessed by the members of the class.
data and functions The public data and functions can be
… accessed by a program. The protected
access specifier data and functions are required when
data and functions.. inheritance is involved.
} object-list;

The object list declares objects of this but it is optional;


Creating Objects

Creation of an Object is a two step process:


• Declare an Object.

Syntax class object_name;


Example Rectangle r1;
The class rectangle (UML Design)

Rectangle
The class name
length : double
breadth : double
data

functions
Implementing a class Rectangle without functions
#include<iostream>
using namespace std;
class Rectangle
{ public:
double length;
double breadth;
};
int main()
{ Rectangle r1;
double perim;
r1.length = 5.5;
r1.breadth = 4.1;
// compute perimeter
perim = (r1.length+ r1.breadth)*2;
cout << "Perimeter of r1 =" << perim << endl;
return 0;
}
The class rectangle with functions (UML Design)

Rectangle
The class name
length : double
breadth : double
data
area () : double
perimeter () : double

functions
Including Functions in a class
#include<iostream>
using namespace std;
class Rectangle
{ public:
double length;
double breadth;
double peri();
double area();
};
double Rectangle::peri()
{return 2*(length + breadth);
}
double Rectangle::area()
{return length*breadth;
}
Including Functions in a class
int main()
{ Rectangle r1, r2;
double perim, ar;
r1.length = 5.5;
r1.breadth = 4.1;
r2.length = 2.5;
r2.breadth = 1.6;
perim = r1.peri(); // compute perimeter of r1
cout << "\n\tPerimeter of r1 =" << perim;
ar = r1.area(); // compute area of r1
cout << "\n\tArea of r1 =" << ar;
perim = r2.peri(); // compute perimeter of r2
cout << "\n\tPerimeter of r2 =" << perim;
ar = r2.area(); // compute area of r2
cout << "\n\tArea of r2 =" << ar;
return 0;
}

Vous aimerez peut-être aussi