SlideShare a Scribd company logo
1 of 23
Download to read offline
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 basics
vamshimahi
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
dannygriff1
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
rani marri
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 

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 (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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
hublikarsn
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 

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.