Programming Concepts in C++
CT025-3-2
Classes A deeper look
Topic and structure of the lesson
Classes A deeper look
preprocessor wrapper const objects and const member functions Composition: Objects as Members of classes friend functions and friend classes dynamic memory management with operators new and delete
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 2 (of 35)
Learning Outcomes
By the end of this session, you should be able to:
create, debug and run a simple C++ programs using the concept of preprocessor wrapper, constant member objects, functions,friend functions, friend classes, static class members
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 3 (of 35)
Key terms to be used
If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams:
preprocessor wrapper constant member objects friend functions friend classes static class members
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 4 (of 35)
Preprocessor wrapper
//Time.h #ifndef TIME_H preprocessor wrapper prevents multiple inclusions #define TIME_H of header file class Time { public: Time( ); void setTime(int, int, int); void printUniversal( ); void printStandard( ); private: int hour; int minute; int second; }; preprocessor wrapper #endif
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 5 (of 35)
Constant objects
keyword const used
to specify that an object is not modifiable any attempt to modify the object should result in compilation error const Time noon (12, 0, 0); declares a const object noon of class Time and initializes it to 12 noon
declaring variables and objects const can improve performance
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 6 (of 35)
Constant member functions
#ifndef TIME_H #define TIME_H class Time { public: Time (int = 0, int = 0, int = 0); void setTime (int, int, int); void setHour(int); void setMinute(int); void setMinute(int); //get functions normally declared const int getHour( ) const; int getMinute( ) const; int getSecond( ) const;
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 7 (of 35)
Constant member functions
//print functions normally declared const void printUniversal( ) const; void printStandard( );
private: int hour; int minute; int second; }; #endif
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 8 (of 35)
Constant member functions
int Time::getHour( ) const { return hour; } int Time::getMinute( ) const { return minute; } int Time::getSecond( ) const { return second; }
void Time::printUniversal( ) const { cout<<..........<<endl; }
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 9 (of 35)
Constant member functions
#include Time.h void main( ) { Time wakeUp(6, 45, 0); const Time noon(12, 0 , 0); wakeUp.setHour(18); noon.setHour(12); wakeUp.getHour(); noon.getMinute( ); noon.printUniversal( ); noon.printStandard( ); }
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 10 (of 35)
Constant member functions
C++ compilers disallow member function calls for const objects unless the member functions themselves are also declared const A constructor can still be used to initialize a const object even though it is a non constant member function
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 11 (of 35)
Composition: Objects as members of classes
#ifndef DATE_H #define DATE_H class Date { .. .. }; #endif
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 12 (of 35)
Composition: Objects as members of classes
#ifndef EMPLOYEE_H #define EMPLOYEE_H #include Date.h class Employee { public: Employee(........const Date &, const Date &); private: const Date birthDate; const Date hireDate; }; #endif
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 13 (of 35)
Composition: Objects as members of classes
#include<iostream> using namespace std; #include Employee.h #include Date.h Employee::Employee(........const Date &dateOfBirth, const Date &dateOfHire) : birthDate( dateOfBirth), hireDate(dateOfHire) { ..... }
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 14 (of 35)
Composition: Objects as members of classes
#include<iostream> using namespace std; #include Employee.h void main( ) { Date birth(7, 24, 1949); Date hire(3, 12, 1988); Employee manager (Bob, Blue, birth, hire); ...... }
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 15 (of 35)
friend Functions and friend Classes
friend function
is defined outside that classs scope yet has the right to access non-public (and public) members of the class using this can enhance performance to declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend friend void setX( Count &, int);
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 16 (of 35)
friend Functions and friend Classes
to declare all member functions of class ClassTwo as friends of class ClassOne place a declaration of the form friend class ClassTwo; in the definition of class ClassOne
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 17 (of 35)
Example of friend function
#include<iostream> using namespace std; class Count { friend void setX(Count &, int); public: Count( ) : x(0) { } //empty body void print( ) const { cout<<x<<endl; } private: int x; };
Classes and Objects
CT025-3-2-PCPP (Programming Concepts in C++)
Slide 18 (of 35)
Example of friend function
void setX ( Count &c, int val) { c.x = val; } void main( ) { Count counter; cout<<Counter after instantiation :; counter.print( ); setX(counter, 8); cout<<Counter.x after call to setX friend function: counter.print( ); }
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 19 (of 35)
Example of attempting to modify a private member with a non friend function
#include<iostream> using namespace std; class Count { public: Count( ) : x(0) { } //empty body void print( ) const { cout<<x<<endl; } private: int x; };
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 20 (of 35)
Example of attempting to modify a private member with a non friend function
void cannotSetX ( Count &c, int val) { c.x = val; //cannot access private member in Count } void main( ) { Count counter; cannotSetX(counter, 3); //cannotSetX is not a friend }
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 21 (of 35)
Using the this pointer
this
is a C++ keyword a pointer through which an object has access to its own address is not part of the object itself is passed by the compiler as an implicit argument to each of the objects non-static member function
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 22 (of 35)
Using the this pointer
#include<iostream> using namespace std; class Test { public: Test(int = 0); void print( ) const; private: int x; }; Test::Test(int value) : x(value) { /*empty body*/
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 23 (of 35)
Using the 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; } void main ( ) { Test testObject(12); testObject.print( );
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 24 (of 35)
Using the this pointer to enable cascaded function calls
Cascade member function calls; recall dot operator associates from left to right
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 25 (of 35)
Using the this pointer to enable cascaded function calls
Set functions return reference to Time object to enable cascaded member function calls
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 26 (of 35)
Using the this pointer to enable cascaded function calls
Return *this as reference to enable cascaded member function calls
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 27 (of 35)
Dynamic Memory Management
class Employee { ..... private: char firstName[25]; char lastName[25]; } class Employee1 { ..... private: char *firstName; char *lastName; }
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 28 (of 35)
Dynamic Memory Management
allocation and deallocation of memory in a program for any built-in or user-defined typeis performed with operators
new delete
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 29 (of 35)
Dynamic Memory Management
new
to dynamically allocate / reserve the exact amount of memory required to hold each name at execution time Time *timePtr; timePtr = new Time;
delete
to deallocate or release memory to return the memory to the free store delete timePtr;
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 30 (of 35)
Dynamic Memory Management
can also provide initializer for newly created fundamental type double *ptr = new double (3.14159); Time *timePtr = new Time (12, 45, 0); how to initialize for array of objects? int *gradesArray = new int [10];
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 31 (of 35)
Dynamic Memory Management
when allocating an array of objects dynamically
cannot pass arguments to each objects constructor
to delete dynamically allocated array
delete [ ] gradesArray
without [ ] only the first object in the array will receive the destructor call
CT025-3-2-PCPP (Programming Concepts in C++) Classes and Objects Slide 32 (of 35)
Follow up Assignment
Create two classes. The first holds customer data
specifically, a customer number and zip code. The second, a class for cities, holds the city name, state and zip code.
Additionally, each class contains a constructor that takes
arguments to set the field values. Create a friend function that displays a customer number and the customers city name. Write a brief main( ) program to test the classes and the friend function.
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 33 (of 35)
Summary of key points
Classes A deeper look
preprocessor wrapper const objects and const member functions Composition: Objects as Members of classes friend functions and friend classes dynamic memory management with operators new and delete
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 34 (of 35)
Highlight of next session
Inheritance Part 1
Understanding inheritance How to create a derived class Choosing the Class Access Specifier
CT025-3-2-PCPP (Programming Concepts in C++)
Classes and Objects
Slide 35 (of 35)