SlideShare a Scribd company logo
class Increment
{
public:
Increment( int c = 0, int i = 1 ); // default constructor
// function addIncrement definition
void addIncrement()
{
count += increment;
} // end function addIncrement
void print() const; // prints count and increment
private:
int count;
const int increment; // const data member
}; // end class Increment
Class ‘Increment’
Increment::Increment( int c, int i )
{
count = c; // allowed because count is not constant
increment = i; // ERROR: Cannot modify a const object
} // end constructor Increment
‘const’ Data Member
Initializer
Increment::Increment( int c, int i )
: count( c ), // initializer for non-const member
increment( i ) // required initializer for const member
{
// empty body
} // end constructor Increment
class Time
{
public:
Time( int = 0, int = 0, int = 0 ); // default constructor
// set functions
void setTime( int, int, int ); // set time
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
// get functions (normally declared const)
int getHour() const; // return hour
int getMinute() const; // return minute
int getSecond() const; // return second
// print functions (normally declared const)
void printUniversal() const; // print universal time
void printStandard(); // print standard time (should be const)
private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
}; // end class Time
Class Time
class Time
{
public:
Time( int = 0, int = 0, int = 0 ); // default constructor
// set functions
void setTime( int, int, int ); // set time
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
// get functions (normally declared const)
int getHour() const; // return hour
int getMinute() const; // return minute
int getSecond() const; // return second
Class Time
int main()
{
Time wakeUp( 6, 45, 0 ); // non-constant object
const Time noon( 12, 0, 0 ); // constant object
// OBJECT MEMBER FUNCTION
wakeUp.setHour( 18 ); // non-const non-const
noon.setHour( 12 ); // const non-const
wakeUp.getHour(); // non-const const
noon.getMinute(); // const const
noon.printUniversal(); // const const
noon.printStandard(); // const non-const
return 0;
} // end main
Functions for ‘const’ Objects
#include "Date.h" // include Date class definition
class Employee
{
public:
Employee( const char * const, const char * const,
const Date &, const Date & );
void print() const;
~Employee(); // provided to confirm destruction order
private:
char firstName[ 25 ];
char lastName[ 25 ];
const Date birthDate; // composition: member object
const Date hireDate; // composition: member object
}; // end class Employee
#endif
Class Employee
Employee::Employee( const char * const first, const char * const last,
const Date &dateOfBirth, const Date &dateOfHire )
: birthDate( dateOfBirth ), // initialize birthDate
hireDate( dateOfHire ) // initialize hireDate
{
// copy first into firstName and be sure that it fits
int length = strlen( first );
length = ( length < 25 ? length : 24 );
strncpy( firstName, first, length );
firstName[ length ] = '0';
// copy last into lastName and be sure that it fits
length = strlen( last );
length = ( length < 25 ? length : 24 );
strncpy( lastName, last, length );
lastName[ length ] = '0';
// output Employee object to show when constructor is called
cout << "Employee object constructor: "
<< firstName << ' ' << lastName << endl;
} // end Employee constructor
‘const’ Objects in Composition
// Count class definition
class Count
{
friend void setX( Count &, int ); // friend declaration
public:
// constructor
Count()
: x( 0 ) // initialize x to 0
{
// empty body
} // end constructor Count
// output x
void print() const
{
cout << x << endl;
} // end function print
private:
int x; // data member
}; // end class Count
Class Count
void setX( Count &c, int val )
{
c.x = val; // allowed because setX is a friend of Count
} // end function setX
int main()
{
Count counter; // create Count object
cout << "counter.x after instantiation: ";
counter.print();
setX( counter, 8 ); // set x using a friend function
cout << "counter.x after call to setX friend function: ";
counter.print();
return 0;
} // end main
‘friend’ Function
Today’s Topics
 ‘this’ Pointer
 Dynamic Memory Allocation
‘this’ Pointer
class Test
{
public:
Test( int = 0 ); // default constructor
void print() const;
private:
int x;
}; // end class Test
// constructor
Test::Test( int value )
: x( value ) // initialize x to value
{
// empty body
} // end constructor Test
// print x using implicit and explicit this pointers;
// the parentheses around *this are required
‘this’ Pointer
void Test::print() const
{
// implicitly use the this pointer to access the member x
cout << " x = " << x;
// explicitly use the this pointer and the arrow operator
// to access the member x
cout << "n this->x = " << this->x;
// explicitly use the dereferenced this pointer and
// the dot operator to access the member x
cout << "n(*this).x = " << ( *this ).x << endl;
} // end function print
int main()
{
Test testObject( 12 ); // instantiate and initialize testObject
testObject.print();
return 0;
} // end main
Time.h
class Time
{
public:
Time( int = 0, int = 0, int = 0 ); // default constructor
// set functions (the Time & return types enable cascading)
Time &setTime( int, int, int ); // set hour, minute, second
Time &setHour( int ); // set hour
Time &setMinute( int ); // set minute
Time &setSecond( int ); // set second
// get functions (normally declared const)
int getHour() const; // return hour
int getMinute() const; // return minute
int getSecond() const; // return second
// print functions (normally declared const)
void printUniversal() const; // print universal time
void printStandard() const; // print standard time
private:
Cascaded Function calls
void main()
{
Time t; // create Time object
// cascaded function calls
t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
// output time in universal and standard formats
cout << "Universal time: ";
t.printUniversal();
cout << "nStandard time: ";
t.printStandard();
cout << "nnNew standard time: ";
// cascaded function calls
t.setTime( 20, 20, 20 ).printStandard();
} // end main

More Related Content

PDF
Javascript & Ajax Basics
PDF
_Function Builders in Swift #love_swift
PDF
JavaScript - Agora nervoso
PDF
Swift で JavaScript 始めませんか? #iOSDC
PDF
KEY
連邦の白いヤツ 「Objective-C」
PPTX
Алексей Кутумов, Вектор с нуля
Javascript & Ajax Basics
_Function Builders in Swift #love_swift
JavaScript - Agora nervoso
Swift で JavaScript 始めませんか? #iOSDC
連邦の白いヤツ 「Objective-C」
Алексей Кутумов, Вектор с нуля

What's hot (20)

ZIP
PDF
JavaScript ES6
PDF
C++ L08-Classes Part1
PDF
AnyObject – 自分が見落としていた、基本の話
PPTX
Type Driven Development with TypeScript
PPT
PPTX
C++ Lambda and concurrency
PPTX
99 líneas que lo simplifican todo( sin animar)
PDF
JavaSE7 Launch Event: Java7xGroovy
PPT
C++totural file
PPTX
Academy PRO: ES2015
PPT
Operator Overloading
PDF
Composition in JavaScript
PPTX
Operator overloading2
PDF
An Intro To ES6
DOC
Ds 2 cycle
PDF
ES6 - Next Generation Javascript
PDF
Bind me if you can
DOCX
Oop lab report
JavaScript ES6
C++ L08-Classes Part1
AnyObject – 自分が見落としていた、基本の話
Type Driven Development with TypeScript
C++ Lambda and concurrency
99 líneas que lo simplifican todo( sin animar)
JavaSE7 Launch Event: Java7xGroovy
C++totural file
Academy PRO: ES2015
Operator Overloading
Composition in JavaScript
Operator overloading2
An Intro To ES6
Ds 2 cycle
ES6 - Next Generation Javascript
Bind me if you can
Oop lab report
Ad

Similar to Class ‘increment’ (20)

PDF
Modify the Time classattached to be able to work with Date.pdf
PPT
C++: Constructor, Copy Constructor and Assignment operator
PPT
classes and constructors lec 03 & 04.ppt
DOCX
C# labprograms
PPT
w10 (1).ppt
PDF
C++ L11-Polymorphism
PPT
Constructor
PPTX
OOPS 22-23 (1).pptx
PPTX
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
PDF
Oop assignment 02
PPT
Mastering Operator Overloading in C++...
PPT
friends functionToshu
PPTX
Constructor and Destructors in C++
PPTX
Operator overload rr
PDF
Functional C++
DOC
Pads lab manual final
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPTX
constructors
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
Modify the Time classattached to be able to work with Date.pdf
C++: Constructor, Copy Constructor and Assignment operator
classes and constructors lec 03 & 04.ppt
C# labprograms
w10 (1).ppt
C++ L11-Polymorphism
Constructor
OOPS 22-23 (1).pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
Oop assignment 02
Mastering Operator Overloading in C++...
friends functionToshu
Constructor and Destructors in C++
Operator overload rr
Functional C++
Pads lab manual final
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
constructors
Object Oriented Programming (OOP) using C++ - Lecture 3
Ad

More from Syed Zaid Irshad (20)

PDF
Data Structures & Algorithms - Spring 2025.pdf
PDF
Operating System.pdf
PDF
DBMS_Lab_Manual_&_Solution
PPTX
Data Structure and Algorithms.pptx
PPTX
Design and Analysis of Algorithms.pptx
PPTX
Professional Issues in Computing
PDF
Reduce course notes class xi
PDF
Reduce course notes class xii
PDF
Introduction to Database
PDF
C Language
PDF
Flowchart
PDF
Algorithm Pseudo
PDF
Computer Programming
PDF
ICS 2nd Year Book Introduction
PDF
Security, Copyright and the Law
PDF
Computer Architecture
PDF
Data Communication
PDF
Information Networks
PDF
Basic Concept of Information Technology
PDF
Introduction to ICS 1st Year Book
Data Structures & Algorithms - Spring 2025.pdf
Operating System.pdf
DBMS_Lab_Manual_&_Solution
Data Structure and Algorithms.pptx
Design and Analysis of Algorithms.pptx
Professional Issues in Computing
Reduce course notes class xi
Reduce course notes class xii
Introduction to Database
C Language
Flowchart
Algorithm Pseudo
Computer Programming
ICS 2nd Year Book Introduction
Security, Copyright and the Law
Computer Architecture
Data Communication
Information Networks
Basic Concept of Information Technology
Introduction to ICS 1st Year Book

Recently uploaded (20)

PPTX
web development for engineering and engineering
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
composite construction of structures.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT
Mechanical Engineering MATERIALS Selection
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
additive manufacturing of ss316l using mig welding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Welding lecture in detail for understanding
PPT
Project quality management in manufacturing
web development for engineering and engineering
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Sustainable Sites - Green Building Construction
composite construction of structures.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mechanical Engineering MATERIALS Selection
bas. eng. economics group 4 presentation 1.pptx
UNIT 4 Total Quality Management .pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CYBER-CRIMES AND SECURITY A guide to understanding
additive manufacturing of ss316l using mig welding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Welding lecture in detail for understanding
Project quality management in manufacturing

Class ‘increment’

  • 1. class Increment { public: Increment( int c = 0, int i = 1 ); // default constructor // function addIncrement definition void addIncrement() { count += increment; } // end function addIncrement void print() const; // prints count and increment private: int count; const int increment; // const data member }; // end class Increment Class ‘Increment’
  • 2. Increment::Increment( int c, int i ) { count = c; // allowed because count is not constant increment = i; // ERROR: Cannot modify a const object } // end constructor Increment ‘const’ Data Member Initializer Increment::Increment( int c, int i ) : count( c ), // initializer for non-const member increment( i ) // required initializer for const member { // empty body } // end constructor Increment
  • 3. class Time { public: Time( int = 0, int = 0, int = 0 ); // default constructor // set functions void setTime( int, int, int ); // set time void setHour( int ); // set hour void setMinute( int ); // set minute void setSecond( int ); // set second // get functions (normally declared const) int getHour() const; // return hour int getMinute() const; // return minute int getSecond() const; // return second // print functions (normally declared const) void printUniversal() const; // print universal time void printStandard(); // print standard time (should be const) private: int hour; // 0 - 23 (24-hour clock format) int minute; // 0 - 59 int second; // 0 - 59 }; // end class Time Class Time
  • 4. class Time { public: Time( int = 0, int = 0, int = 0 ); // default constructor // set functions void setTime( int, int, int ); // set time void setHour( int ); // set hour void setMinute( int ); // set minute void setSecond( int ); // set second // get functions (normally declared const) int getHour() const; // return hour int getMinute() const; // return minute int getSecond() const; // return second Class Time
  • 5. int main() { Time wakeUp( 6, 45, 0 ); // non-constant object const Time noon( 12, 0, 0 ); // constant object // OBJECT MEMBER FUNCTION wakeUp.setHour( 18 ); // non-const non-const noon.setHour( 12 ); // const non-const wakeUp.getHour(); // non-const const noon.getMinute(); // const const noon.printUniversal(); // const const noon.printStandard(); // const non-const return 0; } // end main Functions for ‘const’ Objects
  • 6. #include "Date.h" // include Date class definition class Employee { public: Employee( const char * const, const char * const, const Date &, const Date & ); void print() const; ~Employee(); // provided to confirm destruction order private: char firstName[ 25 ]; char lastName[ 25 ]; const Date birthDate; // composition: member object const Date hireDate; // composition: member object }; // end class Employee #endif Class Employee
  • 7. Employee::Employee( const char * const first, const char * const last, const Date &dateOfBirth, const Date &dateOfHire ) : birthDate( dateOfBirth ), // initialize birthDate hireDate( dateOfHire ) // initialize hireDate { // copy first into firstName and be sure that it fits int length = strlen( first ); length = ( length < 25 ? length : 24 ); strncpy( firstName, first, length ); firstName[ length ] = '0'; // copy last into lastName and be sure that it fits length = strlen( last ); length = ( length < 25 ? length : 24 ); strncpy( lastName, last, length ); lastName[ length ] = '0'; // output Employee object to show when constructor is called cout << "Employee object constructor: " << firstName << ' ' << lastName << endl; } // end Employee constructor ‘const’ Objects in Composition
  • 8. // Count class definition class Count { friend void setX( Count &, int ); // friend declaration public: // constructor Count() : x( 0 ) // initialize x to 0 { // empty body } // end constructor Count // output x void print() const { cout << x << endl; } // end function print private: int x; // data member }; // end class Count Class Count
  • 9. void setX( Count &c, int val ) { c.x = val; // allowed because setX is a friend of Count } // end function setX int main() { Count counter; // create Count object cout << "counter.x after instantiation: "; counter.print(); setX( counter, 8 ); // set x using a friend function cout << "counter.x after call to setX friend function: "; counter.print(); return 0; } // end main ‘friend’ Function
  • 10. Today’s Topics  ‘this’ Pointer  Dynamic Memory Allocation
  • 11. ‘this’ Pointer class Test { public: Test( int = 0 ); // default constructor void print() const; private: int x; }; // end class Test // constructor Test::Test( int value ) : x( value ) // initialize x to value { // empty body } // end constructor Test // print x using implicit and explicit this pointers; // the parentheses around *this are required
  • 12. ‘this’ Pointer void Test::print() const { // implicitly use the this pointer to access the member x cout << " x = " << x; // explicitly use the this pointer and the arrow operator // to access the member x cout << "n this->x = " << this->x; // explicitly use the dereferenced this pointer and // the dot operator to access the member x cout << "n(*this).x = " << ( *this ).x << endl; } // end function print int main() { Test testObject( 12 ); // instantiate and initialize testObject testObject.print(); return 0; } // end main
  • 13. Time.h class Time { public: Time( int = 0, int = 0, int = 0 ); // default constructor // set functions (the Time & return types enable cascading) Time &setTime( int, int, int ); // set hour, minute, second Time &setHour( int ); // set hour Time &setMinute( int ); // set minute Time &setSecond( int ); // set second // get functions (normally declared const) int getHour() const; // return hour int getMinute() const; // return minute int getSecond() const; // return second // print functions (normally declared const) void printUniversal() const; // print universal time void printStandard() const; // print standard time private:
  • 14. Cascaded Function calls void main() { Time t; // create Time object // cascaded function calls t.setHour( 18 ).setMinute( 30 ).setSecond( 22 ); // output time in universal and standard formats cout << "Universal time: "; t.printUniversal(); cout << "nStandard time: "; t.printStandard(); cout << "nnNew standard time: "; // cascaded function calls t.setTime( 20, 20, 20 ).printStandard(); } // end main