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 define a class from objects.
 Encapsulation
A technique for Information Hiding.
 Inheritance
It allows to define 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 Object represents an entity in the real world
o Identified by its name
o It consists of two things:
⯍Properties:
⯍Functions
Characteristics of an object
Actions performed by the object
o Everything is an object
o Systems are composed of objects
OBJECTS
■ An object is an instance of a class.
■ An object is a class variable.
■ Is can be uniquely identified 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 define 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
Identifier: name of the class to be declared
ACCESS SPECIFIERS
⚫It specifies the access level of the class members
⚫Two common access specifiers are:
⚫ Private:
Restrict the use of the class members within the class. It is the default
access specifier. It is used to protect the data members from direct
access from outside the class. Data Member are normally declared with
private access specifier.
⚫ 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 specifier.
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 specifiers
■ public:
■ can be accessed outside the class directly.
■ The public stuff is the interface.
ACCESS SPECIFIERS
⚫Member access specifiers
■ 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
inmain function).
class class_name
{
private:
…
…
…
public:
…
…
…
};
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;
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;
Object_name.function();
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;
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 specified within the class (implicitly
inline)
⚫ Function definition is specified outside the class
⚫ Scope resolution operator :: is used in function declaration if
the function is defined 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
function_name name of funtio to be defined
EXERCISE
#include <iostream>
using namespace std;
class Lab4 {
public:
int x = 20; double y = 3.6 ; string m = "Lab 4" ;
void display( )
{ cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl; cout<<"m= "<<m<<endl; cout<<"z= "<<z<<endl;
} //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; }
Lab3
+ x : int
+ y : double
+ m : string
- z : float
+ display ( ) : void
EXERCISE
#include <iostream>
using namespace std;
class A2 {
public:
void disp( )
{ cout<<"Class A2"<<endl; }
}; //end of class A2
int main()
{ A2 a1;
a1.disp();
A2 *a2;
a2->disp();
return 0;
} //end of main function
EXERCISE
Exercise 1:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the width, please:" << endl;
double a ;
cin>>a;
cout << "Enter the height, please:" << endl;
double b ;
cin>>b;
Cal c;
c.setnum(a,b);
cout<<"The area is: "<<c.getnum()<<endl;
return 0;
}
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:
1 setnum to accept the
width and height.
2 cal_area to calculate
the area of rectangle.
3 getnum to return the
value of the area.

More Related Content

Similar to oopusingc.pptx

Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programmingHariz Mustafa
 
Lecture01
Lecture01Lecture01
Lecture01
artgreen
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
Synapseindiappsdevelopment
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
RaazIndia
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
akila m
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
Object oriented programming
Object oriented programmingObject oriented programming
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
ArpitaJana28
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
Oop ppt
Oop pptOop ppt
Oop ppt
Shani Manjara
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
SURBHI SAROHA
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 

Similar to oopusingc.pptx (20)

Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
 
Lecture01
Lecture01Lecture01
Lecture01
 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Oop ppt
Oop pptOop ppt
Oop ppt
 
My c++
My c++My c++
My c++
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 

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

Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
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
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
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
 
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
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
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
 

Recently uploaded (20)

Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
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
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
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
 
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
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
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
 

oopusingc.pptx

  • 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 define a class from objects.  Encapsulation A technique for Information Hiding.  Inheritance It allows to define 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 Object represents an entity in the real world o Identified by its name o It consists of two things: ⯍Properties: ⯍Functions Characteristics of an object Actions performed by the object o Everything is an object o 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 identified 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 define 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 Identifier: name of the class to be declared
  • 12. ACCESS SPECIFIERS ⚫It specifies the access level of the class members ⚫Two common access specifiers are: ⚫ Private: Restrict the use of the class members within the class. It is the default access specifier. It is used to protect the data members from direct access from outside the class. Data Member are normally declared with private access specifier. ⚫ 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 specifier.
  • 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 specifiers ■ public: ■ can be accessed outside the class directly. ■ The public stuff is the interface.
  • 14. ACCESS SPECIFIERS ⚫Member access specifiers ■ 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 inmain function).
  • 15. class class_name { private: … … … public: … … … }; 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; 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; Object_name.function(); 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;
  • 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 specified within the class (implicitly inline) ⚫ Function definition is specified outside the class ⚫ Scope resolution operator :: is used in function declaration if the function is defined 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 function_name name of funtio to be defined
  • 21. EXERCISE #include <iostream> using namespace std; class Lab4 { public: int x = 20; double y = 3.6 ; string m = "Lab 4" ; void display( ) { cout<<"x= "<<x<<endl; cout<<"y= "<<y<<endl; cout<<"m= "<<m<<endl; cout<<"z= "<<z<<endl; } //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; } Lab3 + x : int + y : double + m : string - z : float + display ( ) : void
  • 22. EXERCISE #include <iostream> using namespace std; class A2 { public: void disp( ) { cout<<"Class A2"<<endl; } }; //end of class A2 int main() { A2 a1; a1.disp(); A2 *a2; a2->disp(); return 0; } //end of main function
  • 23. EXERCISE Exercise 1: #include <iostream> using namespace std; int main() { cout << "Enter the width, please:" << endl; double a ; cin>>a; cout << "Enter the height, please:" << endl; double b ; cin>>b; Cal c; c.setnum(a,b); cout<<"The area is: "<<c.getnum()<<endl; return 0; } 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: 1 setnum to accept the width and height. 2 cal_area to calculate the area of rectangle. 3 getnum to return the value of the area.

Editor's Notes

  1. A data type that separates the logical properties from the implementation details called Abstract Data Type(ADT).
  2. Member functions can be executed only after creating objects
  3. Member functions can be executed only after creating objects
  4. // Define the Cal class class Cal { // Declare the private attributes: width, height, and area private: double width; double height; double area; // Declare the public methods: setnum, cal_area, and getnum public: // Define the setnum method to accept the width and height void setnum(double w, double h) { // Assign the parameters to the attributes width = w; height = h; } // Define the cal_area method to calculate the area of rectangle void cal_area() { // Calculate the area by multiplying the width and height attributes area = width * height; } // Define the getnum method to return the value of the area double getnum() { // Return the area attribute return area; } };