Labreport 01
Labreport 01
Experiment No. : 01
Date of Exp. : 25.09.2024
Date of Submission : 8.10.24
Remarks : Name : ALI HOSEN
ID : 2108039
Level : 02
Term : 02
Group : 02
Experiment Name:
Introduction to Class and Objects in OOP
Objectives
• Introduce the concept of Class and Objects in C++.
• Understand the concept of visibility of data members and member functions (Public
and Private access).
1
Example 1:
A C++ program to define a class BOX and create objects of this class:
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s Box {
public :
do uble l e n g t h ;
do uble breadth ;
do uble h e i g h t ;
do uble volume ( ) {
r e t u r n l e n g t h ∗ breadth ∗ h e i g h t ;
}
};
i n t main ( ) {
Box Box1 ;
Box Box2 ;
Box1 . h e i g h t = 5 . 0 ;
Box1 . l e n g t h = 6 . 0 ;
Box1 . breadth = 7 . 0 ;
Box2 . h e i g h t = 1 0 . 0 ;
Box2 . l e n g t h = 1 2 . 0 ;
Box2 . breadth = 1 3 . 0 ;
2
return 0;
}
Output Screenshot
Example 2:
A C++ program to define a class BOX with member functions.
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s Box
{
public :
do ub le l e n g t h ,
breadth , h e i g h t ;
3
}
do ub le volume ( )
{
do ub le v=l e n g t h ∗ breadth ∗ h e i g h t ;
return v ;
}
};
i n t main ( )
{
Box myBox ;
myBox . i n p u t v a l u e ( ) ;
myBox . p r i n t v a l u e ( ) ;
do ub le v o l= myBox . volume ( ) ;
cout <<”Volume o f t he box : ”<<vol <<e n d l ;
}
Output Screenshot
4
Example 3:
A C++ program to understand public and private access of class data members.
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s myTest
{
private :
int a ,b , c ;
public :
void a c c e s s p r i v a t e ( )
{
c i n >>a>>b>>c ;
cout<<a<<” ”<<b<<” ”<<c<<e n d l ;
}
};
i n t main ( )
{
myTest v ;
v. access private ();
}
Output Screenshot
5
Example 4:
A C++ program to understand public and private access of class data members.
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s BOX {
private :
do ub le l e n g t h ;
do ub le breadth ;
do ub le h e i g h t ;
public :
Box box1 ;
box1 . i n i t D a t a ( 2 2 . 5 , 1 0 . 8 , 5 . 2 ) ;
c o u t << ” Area o f BOX = ” << box1 . c a l c u l a t e A r e a ( ) << e n d l ;
c o u t << ”Volume o f BOX = ” << box1 . c a l c u l a t e V o l u m e ( ) << e n d l ;
return 0;
6
}
Output Screenshot
Example 5:
A class having two private variables and one member function which will return the area
and perimeter of the rectangle.
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s Rectangle
{
private :
do uble l e n g t h ;
do uble width ;
public :
R e c t a n g l e ( double l , double w)
{
length = l ;
width = w;
}
do uble getArea ( )
{
7
r e t u r n l e n g t h ∗ width ;
}
do uble g e t P e r i m e t e r ( )
{
r e t u r n 2 ∗ ( l e n g t h + width ) ;
}
};
i n t main ( )
{
do uble l , w;
c o u t << ” Enter l e n g t h and width o f th e r e c t a n g l e : ” ;
c i n >> l >> w;
Rectangle r e c t ( l , w) ;
return 0;
}
Output Screenshot
8
Example 5:
A class having two private variables and one member function which will return the area
and perimeter of the rectangle.
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s t r i n g >
u s i n g namespace s t d ;
c l a s s Batsman
{
private :
i n t batsman code ;
s t r i n g batsman name ;
int total innings ;
int notout innings ;
int total runs ;
f l o a t batting avg ;
void calcavg ( )
{
i f ( t o t a l i n n i n g s − n o t o u t i n n i n g s != 0 )
{
b a t t i n g a v g = s t a t i c c a s t <f l o a t >( t o t a l r u n s ) / ( t o t a l i n n i n g s −
}
else
{
batting avg = 0;
}
}
public :
invoke calcavg ()
9
void readdata ( )
{
cout << ” Enter Batsman Code ( 4 d i g i t s ) : ” ;
c i n >> batsman code ;
cin . ignore ( ) ;
a l l data
calcavg ( ) ;
}
void displaydata ( )
{
cout << ”\ nBatsman Code : ” << batsman code << e n d l ;
cout << ”Batsman Name : ” << batsman name << e n d l ;
cout << ” Total I n n i n g s : ” << t o t a l i n n i n g s << e n d l ;
cout << ”Not−Out I n n i n g s : ” << n o t o u t i n n i n g s << e n d l ;
cout << ” Total Runs : ” << t o t a l r u n s << e n d l ;
cout << ” B a t t i n g Average : ” << b a t t i n g a v g << e n d l ;
}
};
10
i n t main ( )
{
Batsman p l a y e r ;
player . readdata ( ) ;
player . displaydata ( ) ;
return 0;
}
Output Screenshot
11
Discussion
• The concept of classes was explored as blueprints for creating objects that encapsu-
late data and functionality. This foundational knowledge was identified as crucial
for effective object-oriented programming (OOP).
• .The importance of data members and member functions was highlighted, defining
the properties and behaviors of an object. Object data was manipulated using
member functions, illustrating the principle of encapsulation.
12