0% found this document useful (0 votes)
6 views13 pages

Lecture 11

This lecture focuses on C++ programming, specifically on the concepts of objects and classes, constructors, destructors, and overloaded constructors. It provides examples of how to define and use classes in C++, including the implementation of member functions and the automatic initialization of objects. The document serves as a guide for understanding the fundamental principles of object-oriented programming in C++.

Uploaded by

muneebali129056
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Lecture 11

This lecture focuses on C++ programming, specifically on the concepts of objects and classes, constructors, destructors, and overloaded constructors. It provides examples of how to define and use classes in C++, including the implementation of member functions and the automatic initialization of objects. The document serves as a guide for understanding the fundamental principles of object-oriented programming in C++.

Uploaded by

muneebali129056
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Programming

Lecture 11: C++ Programming

Dr. Syed Muhammad Wasif


April 7, 2025
Spring Semester 2025

Email: [email protected]
Dr. S. M. Wasif Computer Programming 1 / 13
Outline

1 Objects and Classes


Objects and Classes
Constructors
Destructors
Overloaded Constructors

Dr. S. M. Wasif Computer Programming 2 / 13


Section 1

Objects and Classes

Dr. S. M. Wasif Computer Programming 3 / 13


Objects and Classes
• Objects represent variables of a user-defined data type.
• Example of a simple class.
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 //
/////////////////////////////////////////////////////////

4 c l a s s D i s t a n c e // E n g l i s h D i s t a n c e c l a s s
5 {
6 private :
7 int feet ;
8 float inches ;
9 public :
10 v o i d s e t d i s t ( i n t f t , f l o a t i n ) // s e t D i s t a n c e t o
args
11 { feet = ft ; inches = in ; }
12 v o i d g e t d i s t ( ) // g e t l e n g t h from u s e r
13 {
14 c o u t << ” \ n E n t e r f e e t : ” ; c i n >> f e e t ;
15 c o u t << ” E n t e r i n c h e s : ” ; c i n >> i n c h e s ;
Dr. S. M. Wasif Computer Programming 4 / 13
Objects and Classes

1 }
2 v o i d s h o w d i s t ( ) // d i s p l a y d i s t a n c e
3 { c o u t << f e e t << ”\’−” << i n c h e s << ’ \” ’ ; }
4 };
5 //
/////////////////////////////////////////////////////////

6 i n t main ( )
7 {
8 D i s t a n c e d i s t 1 , d i s t 2 ; // d e f i n e two l e n g t h s
9 d i s t 1 . s e t d i s t ( 1 1 , 6 . 2 5 ) ; // s e t d i s t 1
10 d i s t 2 . g e t d i s t ( ) ; // g e t d i s t 2 from u s e r
11 // d i s p l a y l e n g t h s
12 c o u t << ” \ n d i s t 1 = ” ; d i s t 1 . s h o w d i s t ( ) ;
13 c o u t << ” \ n d i s t 2 = ” ; d i s t 2 . s h o w d i s t ( ) ;
14 c o u t << e n d l ;
15 return 0;
16 }

Dr. S. M. Wasif Computer Programming 5 / 13


Constructors
• Constructor: automatic initialization is carried out using a special
member function – that is executed automatically whenever an object
is created.
• Example of a constructor.
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 //
/////////////////////////////////////////////////////////

4 c l a s s Counter
5 {
6 private :
7 u n s i g n e d i n t c o u n t ; // c o u n t
8 public :
9 C o u n t e r ( ) : c o u n t ( 0 ) // c o n s t r u c t o r
10 { /∗ empty body ∗/ }
11 v o i d i n c c o u n t ( ) // i n c r e m e n t c o u n t
12 { c o u n t ++; }

Dr. S. M. Wasif Computer Programming 6 / 13


Constructors
1 i n t g e t c o u n t ( ) // r e t u r n c o u n t
2 { r e t u r n count ; }
3 };
4 //
/////////////////////////////////////////////////////////

5 i n t main ( )
6 {
7 C o u n t e r c1 , c2 ; // d e f i n e and i n i t i a l i z e
8 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y
9 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
10 c1 . i n c c o u n t ( ) ; // i n c r e m e n t c1
11 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
12 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
13 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y a g a i n
14 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
15 c o u t << e n d l ;
16 return 0;
17 }

Dr. S. M. Wasif Computer Programming 7 / 13


Destructors
• Destructor: function is automatically called when an object is
destroyed to deallocate the object memory.
• Example of a destructor.
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 //
/////////////////////////////////////////////////////////

4 c l a s s Counter
5 {
6 private :
7 u n s i g n e d i n t c o u n t ; // c o u n t
8 public :
9 C o u n t e r ( ) : c o u n t ( 0 ) // c o n s t r u c t o r
10 {}
11 ˜ C o u n t e r ( ) // d e s t r u c t o r ( same name w i t h t i l d e )
12 {}

Dr. S. M. Wasif Computer Programming 8 / 13


Destructors
1 v o i d i n c c o u n t ( ) // i n c r e m e n t c o u n t
2 { c o u n t ++; }
3 i n t g e t c o u n t ( ) // r e t u r n c o u n t
4 { r e t u r n count ; }
5 };
6 //
/////////////////////////////////////////////////////////

7 i n t main ( )
8 {
9 C o u n t e r c1 , c2 ; // d e f i n e and i n i t i a l i z e
10 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y
11 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
12 c1 . i n c c o u n t ( ) ; // i n c r e m e n t c1
13 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
14 c2 . i n c c o u n t ( ) ; // i n c r e m e n t c2
15 c o u t << ” \ nc1=” << c1 . g e t c o u n t ( ) ; // d i s p l a y a g a i n
16 c o u t << ” \ nc2=” << c2 . g e t c o u n t ( ) ;
17 c o u t << e n d l ;
18 return 0;
19 }
Dr. S. M. Wasif Computer Programming 9 / 13
Overloaded Constructors

• Explicit constructors with the same name mean the constructor is


overloaded.
• A member function can be defined outside the class but declared
inside the class.

Dr. S. M. Wasif Computer Programming 10 / 13


Overloaded Constructors
• Example of an overloaded constructor.
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 //
/////////////////////////////////////////////////////////

4 c l a s s D i s t a n c e // E n g l i s h D i s t a n c e c l a s s
5 {
6 private :
7 int feet ;
8 float inches ;
9 p u b l i c : // c o n s t r u c t o r ( no a r g s )
10 Distance () : feet (0) , inches (0.0)
11 { }
12 // c o n s t r u c t o r ( two a r g s )
13 Distance ( int ft , f l o a t in ) : feet ( f t ) , inches ( in )
14 { }
15 v o i d g e t d i s t ( ) // g e t l e n g t h from u s e r
16 {
17 c o u t << ” \ n E n t e r f e e t : ” ; c i n >> f e e t ;

Dr. S. M. Wasif Computer Programming 11 / 13


Overloaded Constructors
1 c o u t << ” E n t e r i n c h e s : ” ; c i n >> i n c h e s ;
2 }
3 v o i d s h o w d i s t ( ) // d i s p l a y d i s t a n c e
4 { c o u t << f e e t << ”\’−” << i n c h e s << ’ \” ’ ; }
5 v o i d a d d d i s t ( D i s t a n c e , D i s t a n c e ) ; // d e c l a r a t i o n
6 };
7 //
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

8 // add l e n g t h s d2 and d3
9 v o i d D i s t a n c e : : a d d d i s t ( D i s t a n c e d2 , D i s t a n c e d3 )
10 {
11 i n c h e s = d2 . i n c h e s + d3 . i n c h e s ; // add t h e i n c h e s
12 f e e t = 0 ; // ( f o r p o s s i b l e c a r r y )
13 i f ( i n c h e s >= 1 2 . 0 ) // i f t o t a l e x c e e d s 1 2 . 0 ,
14 { // t h e n d e c r e a s e i n c h e s
15 i n c h e s −= 1 2 . 0 ; // by 1 2 . 0 and
16 f e e t ++; // i n c r e a s e f e e t
17 } // by 1
18 f e e t += d2 . f e e t + d3 . f e e t ; // add t h e f e e t
19 }
Dr. S. M. Wasif Computer Programming 12 / 13
Overloaded Constructors

1 //
/////////////////////////////////////////////////////////

2 i n t main ( )
3 {
4 D i s t a n c e d i s t 1 , d i s t 3 ; // d e f i n e two l e n g t h s
5 D i s t a n c e d i s t 2 ( 1 1 , 6 . 2 5 ) ; // d e f i n e and i n i t i a l i z e d i s t 2
6 d i s t 1 . g e t d i s t ( ) ; // g e t d i s t 1 from u s e r
7 d i s t 3 . a d d d i s t ( d i s t 1 , d i s t 2 ) ; // d i s t 3 = d i s t 1 + d i s t 2
8 // d i s p l a y a l l l e n g t h s
9 c o u t << ” \ n d i s t 1 = ” ; d i s t 1 . s h o w d i s t ( ) ;
10 c o u t << ” \ n d i s t 2 = ” ; d i s t 2 . s h o w d i s t ( ) ;
11 c o u t << ” \ n d i s t 3 = ” ; d i s t 3 . s h o w d i s t ( ) ;
12 c o u t << e n d l ;
13 return 0;
14 }

Dr. S. M. Wasif Computer Programming 13 / 13

You might also like