1. LAB 4: OBJECT ORIENTED
PROGRAMMING USING C++
ENG. MOHAMMED AL-OBAIDI
UNIVERSITY OF SANA’A
FACILITY OF ENGINEERING
MECHATRONICS
DEPARTMENT
2. OOP
⚫
⚫
⚫
⚫
⚫
It is programming technique in which programs are written
on the basis of objects
It is a powerful technique to develop software.
It is used to analyze and design the application in terms of
objects.
It deals with data and the procedures as a single unit
Each object is responsible to initialize and destroy itself.
3. FEATURES OF OBJECT-ORIENTED PROGRAMMING
Data abstraction
The procedure used to def i
ne a class from objects.
Encapsulation
A technique for Information Hiding.
Inheritance
It allows to def i
ne a class in terms of another class, which makes it
easier to create and maintain an application.
Polymorphism
The word polymorphism means having many forms.
Typically, polymorphism occurs when there is a hierarchy of classes and
they are related by inheritance.
4. EFFECTS OF OO METHODOLOGY ON SOFTWARE
DESIGN
Maintenance
Extensibility
Reusability
5. OBJECTS
o
o
o
Object represents an entity in the real world
Identif i
ed by its name
It consists of two things:
Properties:
Functions
Characteristics of an object
Actions performed by the
object
o
o
Everything is an object
Systems are composed of
objects
6. OBJECTS
■ An object is an instance of a
class.
■ An object is a class
variable.
■ Is can be uniquely identif i
ed by its
name.
■ Every object have a state which is represented
by the values of its attributes. These state are
changed by function which applied on the
object.
7. ⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
Everything is an object
A student, a professor
A desk, a chair, a classroom, a building
A university, a city, a country
The world, the universe
A subject such as CS, IS, Math, History, …
Systems are composed of objects
An educational system
An economic system
An information system
A computer system
8. PROPERTIES OF OBJECTS
⚫
⚫
⚫
Characteristics of an object are known as
Properties or attributes of the object
Each object has its own properties Example:
If “Person” is an object, it has following properties
Name
Age
Weight
Object: Car
Properties: Model, Color, Price
9. FUNCTIONS OF AN OBJECT
⚫Tasks or actions performed by the object are
known as functions or methods.
10. CLASSES
⚫
⚫
⚫
⚫
Collection of objects with same properties
and functions
Use to def i
ne characteristics of the object
Used as a model for creating different
objects of same type
Each object of a class is known as an
instance of the class
⚫A Class is a user defined data type to implement an abstract
object. Abstract means to hide the details. A Class is a
combination of data and functions.
11. DECLARING A CLASS
⚫
⚫
Keyword “class” is used to declare a class
The body of the class is contained within a set of braces,
{ } ; (notice the semi-colon).
Syntax:
class identifier
{
//Body of the class
};
Class: is the keyword
Identif i
er: name of the class to be declared
12. ACCESS SPECIFIERS
⚫
⚫
⚫
⚫
It specif i
es the access level of the class members
Two common access specif i
ers are:
Private:
Restrict the use of the class members within the class. It is the default
access specif i
er. It is used to protect the data members from direct
access from outside the class. Data Member are normally declared
with private access specif i
er.
Public
It allows the user to access members within the class as well as
outside the class. It can be accessed from anywhere in the program.
Member functions are normally declared with public access specif i
er.
13. ACCESS SPECIFIERS
⚫
⚫
Usually, the data members of a class are declared
in the private: section of the class and the member
functions are in public: section.
Data member or member functions may be public,
private or protected.
⚫
■
■
■
Member access specif i
ers
public:
can be accessed outside the class directly.
The public stuff is the interface.
14. ACCESS SPECIFIERS
⚫
■
■
■
■
Member access
specif i
ers
private:
Accessible only to
member functions of
class.
Private members and
methods are for internal
use only.
Protected means data
member and member
functions can be used in the
same class and its derived
class (at one level) (not
15. class
class_name
{
private:
…
…
… pub
lic:
…
…
…
};
Public members or
methods
private members or
methods
class Circle
{
private:
double radius; public:
void setRadius(double r);
double getDiameter();
double getArea();
double
getCircumference();
};
and retrieve its value directly.
The class methods are
responsible for that only.
They are accessible from
outside the class, and they
can access the member
(radius)
16. CREATING OBJECTS
⚫
⚫
⚫
⚫
⚫
Class is simply a model or prototype for creating
objects.
It is like a new data type that contains both data and
functions.
Object is created in the same way as other variables are
created.
Object is also known as instance of a class.
Process of creating an object is also called
instantiation.
Syntax:
class_name object_name; lab a;
Class_name: name of the class whose type of object is to be created
Object_name: object to be created.
17. ACCESSING CLASS MEMBERS
⚫
⚫
⚫
⚫
⚫
Member functions are used to manipulate data
members of a class.
We can access
class attributes
class methods
We need an object to access instance variables
Syntax:
Object_name.data; a.x;
Object_name.function(); a.display();
Object_name: name of object whose member function is to be executed
Function: It is the member function that is need to be executed.
18. ACCESSING CLASS MEMBERS
•
•
If we have a pointer to an object (member of pointer
operator)
Dereference the pointer then use the dot operator.
Account *moh= new Account ();
(* moh). balance;
(* moh). deposit (1000.00);
Or use the member of pointer operator (arrow operator)
Account * moh = new Account();
moh -balance;
moh -deposit (1000.00);
19. WRITE A PROGRAM
THAT DECLARES A
CLASS WITH A DATA
MEMBER AND TWO
MEMBER FUNCTIONS
OUTPUT:
enter number 10
the value of n= 10
20. DEFINING MEMBER FUNCTIONS OUTSIDE
CLASS
⚫
⚫
⚫
Functions declaration are specif i
ed within the class
(implicitly inline)
Function def i
nition is specif i
ed outside the class
Scope resolution operator :: is used in function declaration
if
the function is def i
ned outside the class.
Syntax:
Return_type class_name :: function_name(parameters)
{
function body
}
Return_type type of value to be returned by function
class_name class name to which function belongs
:: scope resoltion operator
21. EXERCISE
#include iostream
using namespace std;
class Lab4 {
public:
int x = 20; double y = 3.6 ; string m = Lab 4 ;
void display( )
{ coutx= xendl;
couty= yendl; coutm= mendl; coutz= zendl;
} //end of display function
private:
float z = 2.4;
}; //end of class Lab2
int main()
{ Lab4 a ; // creating an object
Lab4 a2; // creating an object
cout by using a : endl;
a.display();
cout by using a2 : endl;
a2.display();
a.m=“Mohammed;
a.x=4; a2.y=80.8;
cout After changing the values: endl;
cout by using a : endl;
a.display();
cout by using a2 : endl;
a2.display();
return 0; }
Lab4
+ x : int
+ y : double
+ m : string
- z : float
+ display ( ) : void
22. EXERCISE
#include iostream using namespace
std; class A2 {
public:
void disp( )
{ coutClass A2endl; }
}; //end of class A2
int main()
{ A2 a1;
a1.disp(); A2 *a2; a2-disp();
return 0;
} //end of main function
23. EXERCISE
Exercise1:
#include iostream using namespace std; int
main()
{
cout Enter the width, please: endl;
double a ;
cina;
cout Enter the height, please: endl;
doubleb ; cinb; Cal c;
c.setnum(a,b);
coutThe area is: c.getnum()endl;
return 0;
}
1
2
3
Exercise 1 contains the main
function (don’t modify this
function for any reason). You
have to write Cal class. In this
class there are 3 functions:
setnum to accept the
width and height.
cal_area to calculate
the area of rectangle.
getnum to return the value
of the area.