0% found this document useful (0 votes)
7 views

Labreport 01

Uploaded by

jobayer mahmud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Labreport 01

Uploaded by

jobayer mahmud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

E

Department of Electronics &


Telecommunication Engineering

Name of the Experiment:


Introduction to Class and Objects in OOP

Course No. : CSE 284


Course Title : Object Oriented Programming

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

• Create data members and member functions (Methods) of a class.

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

c o u t << ”Volume o f Box1 : ” << Box1 . volume ( ) << e n d l ;


c o u t << ”Volume o f Box2 : ” << Box2 . volume ( ) << e n d l ;

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 ;

void input value ( )


{
cout <<”Enter t h r e e s i d e s o f a box : ”<<e n d l ;
c i n >>l e n g t h >>breadth>>h e i g h t ;
}
void p r i n t v a l u e ( )
{
cout <<”Length : ”<<l e n g t h <<e n d l ;
cout <<”Breadth : ”<<breadth<<e n d l ;
cout <<”Height : ”<< h e i g h t <<e n d l ;

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 :

v o i d i n i t D a t a ( double l e n , double brth , double hgt ) {


length = len ;
br eadt h = brth ;
h e i g h t = hgt ;
}
do ub le c a l c u l a t e A r e a ( ) {
r e t u r n l e n g t h ∗ breadth ;
}
do ub le c a l c u l a t e V o l u m e ( ) {
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 ;

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) ;

c o u t << ” Area : ” << r e c t . getArea ( ) << e n d l ;


c o u t << ” P e r i m e t e r : ” << r e c t . g e t P e r i m e t e r ( ) << e n d l ;

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

cout << ” Enter Batsman Name : ” ;


g e t l i n e ( c i n , batsman name ) ;

cout << ” Enter Total I n n i n g s : ” ;


c i n >> t o t a l i n n i n g s ;

cout << ” Enter Not−Out I n n i n g s : ” ;


c i n >> n o t o u t i n n i n g s ;

cout << ” Enter Total Runs : ” ;


c i n >> t o t a l r u n s ;

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

You might also like