SlideShare a Scribd company logo
LAB 4: OBJECT ORIENTED
PROGRAMMING USING C++
ENG. MOHAMMED AL-OBAIDI
UNIVERSITY OF SANA’A 
FACILITY OF ENGINEERING
MECHATRONICS
DEPARTMENT
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.
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.
EFFECTS OF OO METHODOLOGY ON SOFTWARE
DESIGN



Maintenance
Extensibility
Reusability
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
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.
⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
⚫
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
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
FUNCTIONS OF AN OBJECT
⚫Tasks or actions performed by the object are
known as functions or methods.
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.
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
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.
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.
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
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)
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.
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.
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);
WRITE A PROGRAM
THAT DECLARES A
CLASS WITH A DATA
MEMBER AND TWO
MEMBER FUNCTIONS
OUTPUT:
enter number 10
the value of n= 10
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
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
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
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.

More Related Content

Similar to Lab 4 (1).pdf

Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basicsvamshimahi
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)dannygriff1
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
jayeshsoni49
 
Oops
OopsOops
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
rani marri
 
C++ Notes
C++ NotesC++ Notes
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
SURBHI SAROHA
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
Usman Mehmood
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Oops
OopsOops
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
study material
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
C++ classes
C++ classesC++ classes
C++ classes
imhammadali
 

Similar to Lab 4 (1).pdf (20)

Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
 
Class and object
Class and objectClass and object
Class and object
 
My c++
My c++My c++
My c++
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Oops
OopsOops
Oops
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Oops
OopsOops
Oops
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Only oop
Only oopOnly oop
Only oop
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
C++ classes
C++ classesC++ classes
C++ classes
 

More from MohammedAlobaidy16

cnc_codes_and_letters.ppt
cnc_codes_and_letters.pptcnc_codes_and_letters.ppt
cnc_codes_and_letters.ppt
MohammedAlobaidy16
 
1684424.ppt
1684424.ppt1684424.ppt
1684424.ppt
MohammedAlobaidy16
 
Lab 3.pptx
Lab 3.pptxLab 3.pptx
Lab 3.pptx
MohammedAlobaidy16
 
Lab 3.pptx
Lab 3.pptxLab 3.pptx
Lab 3.pptx
MohammedAlobaidy16
 
ch22.ppt
ch22.pptch22.ppt
ch07.ppt
ch07.pptch07.ppt
LAB3_CADCAM_using_MasterCam.pptx
LAB3_CADCAM_using_MasterCam.pptxLAB3_CADCAM_using_MasterCam.pptx
LAB3_CADCAM_using_MasterCam.pptx
MohammedAlobaidy16
 
MS Office Word.pptx
 MS Office Word.pptx MS Office Word.pptx
MS Office Word.pptx
MohammedAlobaidy16
 
MT 308 Industrial Automation.ppt
MT 308 Industrial Automation.pptMT 308 Industrial Automation.ppt
MT 308 Industrial Automation.ppt
MohammedAlobaidy16
 
ch25.ppt
ch25.pptch25.ppt
ch05.ppt
ch05.pptch05.ppt
ch02.ppt
ch02.pptch02.ppt
Lab1 - Introduction to Computer Basics Laboratory.pdf
Lab1 - Introduction to Computer Basics Laboratory.pdfLab1 - Introduction to Computer Basics Laboratory.pdf
Lab1 - Introduction to Computer Basics Laboratory.pdf
MohammedAlobaidy16
 
lab3&4.pdf
lab3&4.pdflab3&4.pdf
lab3&4.pdf
MohammedAlobaidy16
 
LAB2_Gcode_Mcode.pptx
LAB2_Gcode_Mcode.pptxLAB2_Gcode_Mcode.pptx
LAB2_Gcode_Mcode.pptx
MohammedAlobaidy16
 
4_5931536868716842982.pdf
4_5931536868716842982.pdf4_5931536868716842982.pdf
4_5931536868716842982.pdf
MohammedAlobaidy16
 
ch01.ppt
ch01.pptch01.ppt
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
MohammedAlobaidy16
 

More from MohammedAlobaidy16 (18)

cnc_codes_and_letters.ppt
cnc_codes_and_letters.pptcnc_codes_and_letters.ppt
cnc_codes_and_letters.ppt
 
1684424.ppt
1684424.ppt1684424.ppt
1684424.ppt
 
Lab 3.pptx
Lab 3.pptxLab 3.pptx
Lab 3.pptx
 
Lab 3.pptx
Lab 3.pptxLab 3.pptx
Lab 3.pptx
 
ch22.ppt
ch22.pptch22.ppt
ch22.ppt
 
ch07.ppt
ch07.pptch07.ppt
ch07.ppt
 
LAB3_CADCAM_using_MasterCam.pptx
LAB3_CADCAM_using_MasterCam.pptxLAB3_CADCAM_using_MasterCam.pptx
LAB3_CADCAM_using_MasterCam.pptx
 
MS Office Word.pptx
 MS Office Word.pptx MS Office Word.pptx
MS Office Word.pptx
 
MT 308 Industrial Automation.ppt
MT 308 Industrial Automation.pptMT 308 Industrial Automation.ppt
MT 308 Industrial Automation.ppt
 
ch25.ppt
ch25.pptch25.ppt
ch25.ppt
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
ch02.ppt
ch02.pptch02.ppt
ch02.ppt
 
Lab1 - Introduction to Computer Basics Laboratory.pdf
Lab1 - Introduction to Computer Basics Laboratory.pdfLab1 - Introduction to Computer Basics Laboratory.pdf
Lab1 - Introduction to Computer Basics Laboratory.pdf
 
lab3&4.pdf
lab3&4.pdflab3&4.pdf
lab3&4.pdf
 
LAB2_Gcode_Mcode.pptx
LAB2_Gcode_Mcode.pptxLAB2_Gcode_Mcode.pptx
LAB2_Gcode_Mcode.pptx
 
4_5931536868716842982.pdf
4_5931536868716842982.pdf4_5931536868716842982.pdf
4_5931536868716842982.pdf
 
ch01.ppt
ch01.pptch01.ppt
ch01.ppt
 
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
 

Recently uploaded

Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 

Recently uploaded (20)

Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 

Lab 4 (1).pdf

  • 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.