SlideShare a Scribd company logo
1 of 5
Download to read offline
www.cppforschool.com
Time Class Case Study
In the preceding section, we introduced many basic terms and concepts of C++
object oriented programming. In this section, we take a deeper look at classes.
In this Time class case study we will demonstrate several class construction
features. We begin with a Time class that reviews several of the features
presented in the preceding section.
#include<iostream>
#include<iomanip>
using namespace std;
class Time
{
private :
int hour;
int minute;
int second;
public :
//constructor with default value 0
Time(int h = 0, int m = 0, int s = 0);
//setter function
void setTime(int h, int m, int s);
//print description of object in hh:mm:ss
void print();
//compare two time object
bool equals(Time);
};
Time :: Time(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
void Time :: setTime(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
void Time :: print()
{
cout << setw(2) << setfill('0') << hour << ":"
<< setw(2) << setfill('0') << minute << ":"
<< setw(2) << setfill('0') << second << "n";
}
bool Time :: equals(Time otherTime)
{
if(hour == otherTime.hour &&
minute == otherTime.minute &&
second == otherTime.second)
return true;
else
return false;
}
int main()
{
Time t1(10, 50, 59);
t1.print(); // 10:50:59
Time t2; //object created with default value
t2.print(); // 00:00:00
t2.setTime(6, 39, 9); //set the new time in object
t2.print(); // 06:39:09
if(t1.equals(t2))
cout << "Two objects are equalsn";
else
cout << "Two objects are not equalsn";
return 0;
}
Output :
10:50:59
00:00:00
06:39:09
Two objects are not equals
Let's us discuss our Time class and add some new concepts of programming
Constructors with Default Arguments
In Circle class we have created explicit default constructor and parameterized
constructor. Compiler overloads constructor based on match.
You can combine both statements in one as in Time class example
//constructor with default value
Time(int h = 0, int m = 0, int s = 0);
It works same way just matter of styling code.
Constant Function
In some cases, you will need that the member function should not change any
private member variables of the calling object. You can do this by adding const
to the end of the function declaration (prototype) and in the function definition.
Let’s make member functions in our Time class const if appropriate:
class Time
{
...
...
//print description of object in hh:mm:ss
void print() const;
...
...
};
....
....
void Time :: print() const
{
cout << setw(2) << setfill('0') << hour << ":"
<< setw(2) << setfill('0') << minute << ":"
<< setw(2) << setfill('0') << second << "n";
}
....
Constant Parameters
You can make a parameter in a function as being a const parameter by
preceding its type with const. This tells the compiler to disallow that parameter
changing its value inside that function. This mechanism protects you from
making inadvertent mistakes.
Let’s make parameter constant in our Time class, if appropriate:
class Time
{
.....
//constructor with default value 0
Time(const int h = 0, const int m = 0, const int s = 0);
//setter function
void setTime(const int h, const int m, const int s);
.....
};
Time :: Time(const int h, const int m, const int s)
{
hour = h;
minute = m;
second = s;
}
void Time :: setTime(const int h, const int m, const int s)
{
hour = h;
minute = m;
second = s;
}
......
Passing Objects to Functions
Object can be passed by value, by reference or by pointer. In our Time class we
passed object by value.
bool Time :: equals(Time otherTime)
{
if(hour == otherTime.hour &&
minute == otherTime.minute &&
second == otherTime.second)
return true;
else
return false;
}
This means that equals() receives a copy of object t2 with name otherTime.
If a function needs to store or change data in an object’s member variables, the
object must be passed to it by reference.
Constant Reference Parameters
Passing by const reference is the preferred way to pass objects as an alternative
to pass-by-value. When you pass by const reference, you take the argument in
by reference, but cannot make any changes to the original object.
//object passing by reference with const parameter
bool equals(const Time&);

More Related Content

What's hot

Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In CFazila Sadia
 
ES6 General Introduction
ES6 General IntroductionES6 General Introduction
ES6 General IntroductionThomas Johnston
 
Modify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletModify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletCleasbyz
 
vbscript-reference book
vbscript-reference bookvbscript-reference book
vbscript-reference bookAnand Dhana
 
String in c programming
String in c programmingString in c programming
String in c programmingDevan Thakur
 
Linguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and ThreadsLinguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and ThreadsESUG
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserMindbowser Inc
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2Aanand Singh
 
F# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordF# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordKit Eason
 
Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)chirantan.rajhans
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 

What's hot (20)

C++ day2
C++ day2C++ day2
C++ day2
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
constructors
constructorsconstructors
constructors
 
ES6 General Introduction
ES6 General IntroductionES6 General Introduction
ES6 General Introduction
 
C++ training day01
C++ training day01C++ training day01
C++ training day01
 
String handling
String handlingString handling
String handling
 
String functions in C
String functions in CString functions in C
String functions in C
 
Arrays in VbScript
Arrays in VbScriptArrays in VbScript
Arrays in VbScript
 
Computer programming 2 Lesson 14
Computer programming 2  Lesson 14Computer programming 2  Lesson 14
Computer programming 2 Lesson 14
 
Modify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletModify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutlet
 
vbscript-reference book
vbscript-reference bookvbscript-reference book
vbscript-reference book
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Linguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and ThreadsLinguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and Threads
 
Strings
StringsStrings
Strings
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - Mindbowser
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2
 
F# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordF# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, Hereford
 
Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)Ruby Code Optimizations (for beginners)
Ruby Code Optimizations (for beginners)
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 

Viewers also liked

Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-iiDeepak Singh
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesDeepak Singh
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructorDeepak Singh
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-fileDeepak Singh
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloadingDeepak Singh
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structureDeepak Singh
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-iDeepak Singh
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-classDeepak Singh
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Examshishamrizvi
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Deepak Singh
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II NotesAndrew Raj
 

Viewers also liked (16)

Iris biometrics
Iris     biometricsIris     biometrics
Iris biometrics
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructor
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloading
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
Chapter17 oop
Chapter17 oopChapter17 oop
Chapter17 oop
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 

Similar to Chapter20 class-example-program

Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfaaseletronics2013
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6Nitay Neeman
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++HalaiHansaika
 
C questions
C questionsC questions
C questionsparm112
 
Class & Objects in JAVA.ppt
Class & Objects in JAVA.pptClass & Objects in JAVA.ppt
Class & Objects in JAVA.pptRohitPaul71
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and BeyondComicSansMS
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answersQuratulain Naqvi
 

Similar to Chapter20 class-example-program (20)

Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdf
 
Module 4.pdf
Module 4.pdfModule 4.pdf
Module 4.pdf
 
Oop rosenschein
Oop rosenscheinOop rosenschein
Oop rosenschein
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
C questions
C questionsC questions
C questions
 
E7
E7E7
E7
 
Class & Objects in JAVA.ppt
Class & Objects in JAVA.pptClass & Objects in JAVA.ppt
Class & Objects in JAVA.ppt
 
Thread
ThreadThread
Thread
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and Beyond
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
lecture10.ppt
lecture10.pptlecture10.ppt
lecture10.ppt
 
lecture10.ppt
lecture10.pptlecture10.ppt
lecture10.ppt
 

More from Deepak Singh

Computer networks - CBSE New Syllabus (083) Class - XII
Computer networks - CBSE  New Syllabus (083) Class - XIIComputer networks - CBSE  New Syllabus (083) Class - XII
Computer networks - CBSE New Syllabus (083) Class - XIIDeepak Singh
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handlingDeepak Singh
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objectsDeepak Singh
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-arrayDeepak Singh
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimensionDeepak Singh
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library FunctionDeepak Singh
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Deepak Singh
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Deepak Singh
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptDeepak Singh
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramDeepak Singh
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperDeepak Singh
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Deepak Singh
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011Deepak Singh
 

More from Deepak Singh (17)

Computer networks - CBSE New Syllabus (083) Class - XII
Computer networks - CBSE  New Syllabus (083) Class - XIIComputer networks - CBSE  New Syllabus (083) Class - XII
Computer networks - CBSE New Syllabus (083) Class - XII
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 

Chapter20 class-example-program

  • 1. www.cppforschool.com Time Class Case Study In the preceding section, we introduced many basic terms and concepts of C++ object oriented programming. In this section, we take a deeper look at classes. In this Time class case study we will demonstrate several class construction features. We begin with a Time class that reviews several of the features presented in the preceding section. #include<iostream> #include<iomanip> using namespace std; class Time { private : int hour; int minute; int second; public : //constructor with default value 0 Time(int h = 0, int m = 0, int s = 0); //setter function void setTime(int h, int m, int s); //print description of object in hh:mm:ss void print(); //compare two time object bool equals(Time); }; Time :: Time(int h, int m, int s) { hour = h; minute = m; second = s; } void Time :: setTime(int h, int m, int s) { hour = h; minute = m; second = s; }
  • 2. void Time :: print() { cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << "n"; } bool Time :: equals(Time otherTime) { if(hour == otherTime.hour && minute == otherTime.minute && second == otherTime.second) return true; else return false; } int main() { Time t1(10, 50, 59); t1.print(); // 10:50:59 Time t2; //object created with default value t2.print(); // 00:00:00 t2.setTime(6, 39, 9); //set the new time in object t2.print(); // 06:39:09 if(t1.equals(t2)) cout << "Two objects are equalsn"; else cout << "Two objects are not equalsn"; return 0; } Output : 10:50:59 00:00:00 06:39:09 Two objects are not equals Let's us discuss our Time class and add some new concepts of programming
  • 3. Constructors with Default Arguments In Circle class we have created explicit default constructor and parameterized constructor. Compiler overloads constructor based on match. You can combine both statements in one as in Time class example //constructor with default value Time(int h = 0, int m = 0, int s = 0); It works same way just matter of styling code. Constant Function In some cases, you will need that the member function should not change any private member variables of the calling object. You can do this by adding const to the end of the function declaration (prototype) and in the function definition. Let’s make member functions in our Time class const if appropriate: class Time { ... ... //print description of object in hh:mm:ss void print() const; ... ... }; .... .... void Time :: print() const { cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << "n"; } ....
  • 4. Constant Parameters You can make a parameter in a function as being a const parameter by preceding its type with const. This tells the compiler to disallow that parameter changing its value inside that function. This mechanism protects you from making inadvertent mistakes. Let’s make parameter constant in our Time class, if appropriate: class Time { ..... //constructor with default value 0 Time(const int h = 0, const int m = 0, const int s = 0); //setter function void setTime(const int h, const int m, const int s); ..... }; Time :: Time(const int h, const int m, const int s) { hour = h; minute = m; second = s; } void Time :: setTime(const int h, const int m, const int s) { hour = h; minute = m; second = s; } ...... Passing Objects to Functions Object can be passed by value, by reference or by pointer. In our Time class we passed object by value. bool Time :: equals(Time otherTime) { if(hour == otherTime.hour && minute == otherTime.minute &&
  • 5. second == otherTime.second) return true; else return false; } This means that equals() receives a copy of object t2 with name otherTime. If a function needs to store or change data in an object’s member variables, the object must be passed to it by reference. Constant Reference Parameters Passing by const reference is the preferred way to pass objects as an alternative to pass-by-value. When you pass by const reference, you take the argument in by reference, but cannot make any changes to the original object. //object passing by reference with const parameter bool equals(const Time&);