










































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Prof. Prabhat Patel at Ankit Institute of Technology and Science for Advanced Programming for Engineers. It includes: Classes, Data, Abstraction, Implementing, Scope, Members, Initializing, Arguments, Memberwise, Assignment
Typology: Slides
1 / 50
This page cannot be seen from the preview
Don't miss anything!











































6.1 INTRODUCTION
Object-oriented programming (OOP)
Encapsulates data (attributes) and functions (behavior) into packages called classes
Information hiding
Implementation details hidden within classes themselves
User-defined (programmer-defined) types: classes
Data (data members)
Functions (member functions or methods)
Similar to blueprints – reusable
Class instance: object
6.5 IMPLEMENTING A TIME ABSTRACT DATA
TYPE WITH A CLASS
Constructor function
Special member function
Initializes data members Same name as class
Called when object instantiated
Several constructors
Function overloading
No return type
1 C L A S S T I M E { 2 3 P U B L I C : 4 T I M E ( ) ; / / C O N S T R U C T O R 5 V O I D S E T T I M E ( I N T , I N T , I N T ) ; / / S E T H O U R , M I N U T E , S E C O N D (^6) F O R M A T V O I D P R I N T U N I V E R S A L ( ) ; / / P R I N T U N I V E R S A L - T I M E
(^7) F O R M A T V O I D P R I N T S T A N D A R D ( ) ; / / P R I N T S T A N D A R D - T I M E
8 9 P R I V A T E : 1 0 I N T H O U R ; / / 0 - 2 3 ( 2 4 - H O U R C L O C K F O R M A T ) 1 1 I N T M I N U T E ; / / 0 - 5 9 1 2 I N T S E C O N D ; / / 0 - 5 9 1 3 1 4 } ; / / E N D C L A S S T I M E
Member access specifiers.
Definition of class begins with keyword class.
Class body starts with left brace.
Class body ends with right brace.
Definition terminates with semicolon.
Function prototypes for public member functions.
private data members accessible only to member functions.
Constructor has same name as class, Time , and no return type.
6.5 IMPLEMENTING A TIME ABSTRACT DATA
TYPE WITH A CLASS
Objects of class
After class definition
Class name new type specifier C++ extensible language Object, array, pointer and reference declarations
Example:
Time sunset; // object of type Time
Class name becomes new type specifier.
1 / / F I G. 6. 3 : F I G 0 6 _ 0 3. C P P 2 / / T I M E C L A S S. 3 # I N C L U D E < I O S T R E A M > 4 5 U S I N G S T D : : C O U T ; 6 U S I N G S T D : : E N D L ; 7 8 # I N C L U D E < I O M A N I P > 9 1 0 U S I N G S T D : : S E T F I L L ; 1 1 U S I N G S T D : : S E T W ; 1 2 1 3 / / T I M E A B S T R A C T D A T A T Y P E ( A D T ) D E F I N I T I O N 1 4 C L A S S T I M E { 1 5 1 6 P U B L I C : 1 7 T I M E ( ) ; / / C O N S T R U C T O R 1 8 V O I D S E T T I M E ( I N T , I N T , I N T ) ; / / S E T H O U R , M I N U T E , S E C O N D 1 9 V O I D P R I N T U N I V E R S A L ( ) ; / / P R I N T U N I V E R S A L - T I M E F O R M A T 2 0 V O I D P R I N T S T A N D A R D ( ) ; / / P R I N T S T A N D A R D - T I M E F O R M A T 2 1
Define class Time.
2 2 P R I V A T E :
2 3 I N T H O U R ; / / 0 - 2 3 ( 2 4 - H O U R C L O C K F O R M A T )
2 4 I N T M I N U T E ; / / 0 - 5 9
2 5 I N T S E C O N D ; / / 0 - 5 9
2 6
2 7 } ; / / E N D C L A S S T I M E
2 8
2 9 / / T I M E C O N S T R U C T O R I N I T I A L I Z E S E A C H D A T A M E M B E R T O Z E R O A N D
3 0 / / E N S U R E S A L L T I M E O B J E C T S S T A R T I N A C O N S I S T E N T S T A T E
3 1 T I M E : : T I M E ( )
3 2 {
3 3 H O U R = M I N U T E = S E C O N D = 0 ;
3 4
3 5 } / / E N D T I M E C O N S T R U C T O R
3 6
3 7 / / S E T N E W T I M E V A L U E U S I N G U N I V E R S A L T I M E , P E R F O R M V A L I D I T Y
3 8 / / C H E C K S O N T H E D A T A V A L U E S A N D S E T I N V A L I D V A L U E S T O Z E R O
3 9 V O I D T I M E : : S E T T I M E ( I N T H , I N T M , I N T S )
4 0 {
4 1 H O U R = ( H > = 0 & & H < 2 4 )? H : 0 ;
4 2 M I N U T E = ( M > = 0 & & M < 6 0 )? M : 0 ;
4 3 S E C O N D = ( S > = 0 & & S < 6 0 )? S : 0 ;
4 4
4 5 } / / E N D F U N C T I O N S E T T I M E
4 6
Constructor initializes private data members to 0.
public member function checks parameter values for validity before setting private data members.
7 0 / / O U T P U T T I M E O B J E C T T ' S I N I T I A L V A L U E S
7 1 C O U T < < " T H E I N I T I A L U N I V E R S A L T I M E I S " ;
7 2 T. P R I N T U N I V E R S A L ( ) ; / / 0 0 : 0 0 : 0 0
7 3
7 4 C O U T < < " \ N T H E I N I T I A L S T A N D A R D T I M E I S " ;
7 5 T. P R I N T S T A N D A R D ( ) ; / / 1 2 : 0 0 : 0 0 A M
7 6
7 7 T. S E T T I M E ( 1 3 , 2 7 , 6 ) ; / / C H A N G E T I M E
7 8
7 9 / / O U T P U T T I M E O B J E C T T ' S N E W V A L U E S
8 0 C O U T < < " \ N \ N U N I V E R S A L T I M E A F T E R S E T T I M E I S " ;
8 1 T. P R I N T U N I V E R S A L ( ) ; / / 1 3 : 2 7 : 0 6
8 2
8 3 C O U T < < " \ N S T A N D A R D T I M E A F T E R S E T T I M E I S " ;
8 4 T. P R I N T S T A N D A R D ( ) ; / / 1 : 2 7 : 0 6 P M
8 5
8 6 T. S E T T I M E ( 9 9 , 9 9 , 9 9 ) ; / / A T T E M P T I N V A L I D S E T T I N G S
8 7
8 8 / / O U T P U T T ' S V A L U E S A F T E R S P E C I F Y I N G I N V A L I D V A L U E S
8 9 C O U T < < " \ N \ N A F T E R A T T E M P T I N G I N V A L I D S E T T I N G S : "
9 0 < < " \ N U N I V E R S A L T I M E : " ;
9 1 T. P R I N T U N I V E R S A L ( ) ; / / 0 0 : 0 0 : 0 0
Invoke public member functions to print time.
Set data members using public member function.
Attempt to set data members to invalid values using public member function.
docsity.com
9 3 C O U T < < " \ N S T A N D A R D T I M E : " ;
9 4 T. P R I N T S T A N D A R D ( ) ; / / 1 2 : 0 0 : 0 0 A M
9 5 C O U T < < E N D L ;
9 6
9 7 R E T U R N 0 ;
9 8
9 9 } / / E N D M A I N
6.5 IMPLEMENTING A TIME ABSTRACT DATA
TYPE WITH A CLASS
Destructors
Same name as class
Preceded with tilde ( ~ )
No arguments
Cannot be overloaded
Performs “termination housekeeping”
For example, if your constructor uses new to allocate memory, the destructor should use delete to free that memory
6.5 IMPLEMENTING A TIME ABSTRACT DATA
TYPE WITH A CLASS
Advantages of using classes
Simplify programming
Software reuse
Class objects included as members of other classes
Inheritance New classes derived from old
6.6 CLASS SCOPE AND ACCESSING CLASS
MEMBERS
Function scope
Variables declared in member function
Only known to function
Variables with same name as class-scope variables
Class-scope variable “hidden” Access with scope resolution operator ( :: ) ClassName::classVariableName
Variables only known to function they are defined in
SEPARATING INTERFACE FROM
IMPLEMENTATION
We began by including a class's definition and member-function definitions in one file.
We then demonstrated separating this code into two files a header file for the class definition (i.e., the class's interface) and a source code file for the class's member function definitions (i.e., the class's implementation).
Recall that this makes it easier to modify programs.