C++ - 1

15
Class and Object Members Access Specifier Constructor and destructor CSE-201 Object Oriented Programming in C++ Basic Syntax - Part I Mahmudul Faisal Al Ameen May 28, 2013 Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 1/15

Transcript of C++ - 1

Class and Object Members Access Specifier Constructor and destructor

CSE-201 Object Oriented Programming in C++Basic Syntax - Part I

Mahmudul Faisal Al Ameen

May 28, 2013

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 1/15

Class and Object Members Access Specifier Constructor and destructor

Contents

1 Class and Object

2 Members

3 Access Specifier

4 Constructor and destructor

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 2/15

Class and Object Members Access Specifier Constructor and destructor

Class

Listing 1: It creates a class1 c l a s s X{23 } ;45 i n t main (){67 }

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 3/15

Class and Object Members Access Specifier Constructor and destructor

Object

Listing 2: It creates an object of a class1 c l a s s Cls X{23 } ;45 i n t main (){6 Cl s X o b j x ;7 }

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 4/15

Class and Object Members Access Specifier Constructor and destructor

Array of Object

Listing 3: It creates an array of objects of a class1 c l a s s Cls X{23 } ;45 i n t main (){6 Cl s X o b j x [ 1 0 ] ;7 }

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 5/15

Class and Object Members Access Specifier Constructor and destructor

Property of a Class

Listing 4: It creates a property ‘number of its blades’ in the class‘Fan’

1 c l a s s Fan{2 i n t numberOfBlades ;3 } ;45 i n t main (){6 Fan l i v i n g r o o m f a n ;7 }

Exercise

What is ‘livingroomfan’?

How to create an object ‘bedroomfan’ of the class ‘Fan’?

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 6/15

Class and Object Members Access Specifier Constructor and destructor

Another Property of a Class

Listing 5: It adds additional property in the class ‘Fan’1 c l a s s Fan{2 i n t numberOfBlades ;3 double r o t a t i o n S p e e d ;4 } ;56 i n t main (){7 Fan l i v i n g r o o m f a n ;8 }

Exercise

Create a class named ‘Color’.

Create three properties ‘red’, ‘green’ and ‘blue’ of type ‘int’ inthe class ‘Color’.

Create a property ‘color’ of type ‘Color’ in the class ‘Fan’.

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 7/15

Class and Object Members Access Specifier Constructor and destructor

Method

Listing 6: A method declaration of class ‘Fan’1 c l a s s Fan{2 i n t numberOfBlades ;3 double r o t a t i o n S p e e d ;4 boo l i s R o t a t i n g ;56 vo id s t a r t ( ) ;7 } ;89 i n t main (){

10 Fan l i v i n g r o o m f a n ;11 }

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 8/15

Class and Object Members Access Specifier Constructor and destructor

Method

Listing 7: Defining the method ‘start()’ of class ‘Fan’1 c l a s s Fan{2 i n t numberOfBlades ;3 double r o t a t i o n S p e e d ;4 boo l i s R o t a t i n g ;56 vo id s t a r t ( ) ;7 } ;89 vo id Fan : : s t a r t (){

10 i s R o t a t i n g = t rue ;11 }1213 i n t main (){14 Fan l i v i n g r o o m f a n ;15 }

Exercise

Declare and define a method ‘stop()’ in class ‘Fan’.

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 9/15

Class and Object Members Access Specifier Constructor and destructor

Access Specifier and use of public members

Listing 8: Use of Access Specifier1 c l a s s Fan{2 p r i v a t e :3 double r o t a t i o n S p e e d ;4 boo l i s R o t a t i n g ;5 pub l i c :6 i n t numberOfBlades ;7 vo id s t a r t ( ) ;8 } ;9

10 vo id Fan : : s t a r t (){11 i s R o t a t i n g = t rue ;12 }1314 i n t main (){15 Fan l i v i n g r o o m f a n ;16 l i v i n g r o o m f a n . numberOfBlades = 4 ;17 l i v i n g r o o m f a n . s t a r t ( ) ;18 }

Exercise

Can you try to assign ‘livingroomfan.rotationSpeed = 5.7’?

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 10/15

Class and Object Members Access Specifier Constructor and destructor

Use of private properties through public methods

Listing 9: Use of Access Specifier1 c l a s s Fan{2 p r i v a t e :3 double r o t a t i o n S p e e d ;4 boo l i s R o t a t i n g ;5 pub l i c :6 vo id s e t R o t a t i o n S p e e d ( double ) ;7 } ;89 vo id Fan : : s e t R o t a t i o n S p e e d ( double s ){

10 r o t a t i o n S p e e d = s ;11 }1213 i n t main (){14 Fan l i v i n g r o o m f a n ;15 l i v i n g r o o m f a n . s e t R o t a t i o n S p e e d ( 2 0 0 ) ;16 }

Exercise

Can you try to assign ‘livingroomfan.rotationSpeed = 5.7’?

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 11/15

Class and Object Members Access Specifier Constructor and destructor

Use of private properties through public methods

Listing 10: Use of Access Specifier1 #inc lude<i o s t r e a m>2 c l a s s Fan{3 p r i v a t e :4 double r o t a t i o n S p e e d ;5 boo l i s R o t a t i n g ;6 pub l i c :7 vo id s e t R o t a t i o n S p e e d ( double ) ;8 double g e t R o t a t i o S p e e d ( ) ;9 } ;

10 vo id Fan : : s e t R o t a t i o n S p e e d ( double s ){11 r o t a t i o n S p e e d = s ;12 }13 double Fan : : g e t R o t a t i o S p e e d (){14 r e t u r n r o t a t i o n S p e e d ;15 }16 i n t main (){17 Fan l i v i n g r o o m f a n ;18 l i v i n g r o o m f a n . s e t R o t a t i o n S p e e d ( 2 0 0 ) ;19 s t d : : cout << l i v i n g r o o m f a n . g e t R o t a t i o n S p e e d ( ) ;20 }

Exercise

Can you try to assign ‘livingroomfan.rotationSpeed = 5.7’?

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 12/15

Class and Object Members Access Specifier Constructor and destructor

Constructor

Listing 11: Use of Constructor1 c l a s s Fan{2 p r i v a t e :3 double r o t a t i o n S p e e d ;4 boo l i s R o t a t i n g ;5 i n t numberOfBlades ;6 pub l i c :7 Fan ( ) ;8 vo id s t a r t ( ) ;9 } ;

1011 Fan : : Fan (){12 r o t a t i o n S p e e d = 1 0 0 0 ;13 i s R o t a t i n g = f a l s e ;14 numberOfBlades = 3 ;15 }1617 vo id Fan : : s t a r t (){18 i s R o t a t i n g = t rue ;19 }2021 i n t main (){22 Fan l i v i n g r o o m f a n ;23 l i v i n g r o o m f a n . s t a r t ( ) ;24 }

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 13/15

Class and Object Members Access Specifier Constructor and destructor

Constructor

Listing 12: Use of Constructor with parameters1 c l a s s Fan{2 p r i v a t e :3 double r o t a t i o n S p e e d ;4 boo l i s R o t a t i n g ;5 i n t numberOfBlades ;6 pub l i c :7 Fan ( i n t ) ;8 vo id s t a r t ( ) ;9 } ;

1011 Fan : : Fan ( i n t nob ){12 r o t a t i o n S p e e d = 1 0 0 0 ;13 i s R o t a t i n g = f a l s e ;14 numberOfBlades = nob ;15 }1617 vo id Fan : : s t a r t (){18 i s R o t a t i n g = t rue ;19 }2021 i n t main (){22 Fan l i v i n g r o o m f a n ( 4 ) ;23 l i v i n g r o o m f a n . s t a r t ( ) ;24 }

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 14/15

Class and Object Members Access Specifier Constructor and destructor

Destructor

Listing 13: Use of Destructor1 #inc lude<i o s t r e a m>2 c l a s s Fan{3 p r i v a t e :4 double r o t a t i o n S p e e d ;5 boo l i s R o t a t i n g ;6 i n t numberOfBlades ;7 pub l i c :8 Fan ( ) ;9 ˜Fan ( ) ;

10 vo id s t a r t ( ) ;11 } ;12 Fan : : Fan (){13 s t d : : cout << ”Fan i s c r e a t e d ” << s t d : : e n d l ;14 }15 Fan : : ˜ Fan (){16 s t d : : cout << ”Fan i s d e l e t e d ” << s t d : : e n d l ;17 }18 vo id Fan : : s t a r t (){19 i s R o t a t i n g = t rue ;20 s t d : : cout << ”Fan s t a r t e d to r o t a t e ” << s t d : : e n d l ;21 }22 i n t main (){23 Fan l i v i n g r o o m f a n ;24 l i v i n g r o o m f a n . s t a r t ( ) ;25 }

Mahmudul Faisal Al Ameen — CSE-201 Object Oriented Programming in C++ 15/15