2
2 Objects An object is simply a self-contained entity that has an existence independent of other entities. An object is simply a self-contained entity that has an existence independent of other entities. a pencil a pencil a book a book a car a car a basketball a basketball a watch a watch a house a house

3
3 A class can be regarded as the general form of an object: A class can be regarded as the general form of an object: The Tower Bridge in London belongs to the class of bridges; The Tower Bridge in London belongs to the class of bridges; The Bran Castle (Dracula’s castle in Rumania) belongs to the class of castles; The Bran Castle (Dracula’s castle in Rumania) belongs to the class of castles; Barack Obama belongs to the class of USA presidents or to the class of humans; Barack Obama belongs to the class of USA presidents or to the class of humans; Lionel Messi belongs to the class of football players; Lionel Messi belongs to the class of football players; The Land Rover Defender 90 TDI 4WD with RegNo CAA236 belongs to the class of cars; The Land Rover Defender 90 TDI 4WD with RegNo CAA236 belongs to the class of cars; Objects and Classes

4
4 Attributes and Operations Properties of the class Employ: Properties of the class Employ: Attributes and operations are called members of a class. Attributes and operations are called members of a class. Attributes are called data members. Attributes are called data members. Operations are called member functions. Operations are called member functions. AttributesOperations NameHire Employee Number Promote Job title Give pay rise SalaryFire Years of employment

6
6 Information Hiding A client (user program) does not need to know how the data are actually stored or how the methods are programmed. This important programming strategy is called information hiding. A client (user program) does not need to know how the data are actually stored or how the methods are programmed. This important programming strategy is called information hiding. Data members and member functions (methods) available to a client are called public. Data members and member functions (methods) available to a client are called public. Private variables and member functions (functions) may be used in the implementation of the class, but are not available to a client. Private variables and member functions (functions) may be used in the implementation of the class, but are not available to a client. Methods of a class are public Functions in a class are private

7
7 Example – Abstract Data Type Main activities for operating a car are: Main activities for operating a car are: steering, accelerating, braking steering, accelerating, braking The design of a car can be viewed as an ADT with operations: The design of a car can be viewed as an ADT with operations: steer, accelerate, brake steer, accelerate, brake Two cars may implement these operations in different ways. Two cars may implement these operations in different ways. Most drivers can operate any car since the ADT presents a uniform method of operation. Most drivers can operate any car since the ADT presents a uniform method of operation.

17
17 Constructors Special functions – have the name of the class Special functions – have the name of the class Called automatically when an object of the class is instantiated Called automatically when an object of the class is instantiated Do not return a value (no return type is specified) Do not return a value (no return type is specified) Their purpose is to initialize the object’s attributes Their purpose is to initialize the object’s attributes Team::Team(){ strcpy(name, ""); strcpy(name, ""); wins = 0; wins = 0; draws = 0; draws = 0; losses = 0; losses = 0;}

22
22 The Destructor class Team { public:..... ~Team(); private:..... };Team::~Team(){ cout << name << ” is deleted\n” ; cout << name << ” is deleted\n” ;} Functions that are called whenever an object is deleted (e.g. goes out of scope) Functions that are called whenever an object is deleted (e.g. goes out of scope) Its purpose is to perform any necessary cleanup operations Its purpose is to perform any necessary cleanup operations Has no return type Has no return type Does not return a value Does not return a value

25
25 Pointers in Classes If a class object comes into existence dynamically using the new operator, the destructor must be called when this object is destroyed using the delete operator. If a class object comes into existence because it is a local variable in a function, the destructor will be called when the function returns. If a class object comes into existence dynamically using the new operator, the destructor must be called when this object is destroyed using the delete operator. If a class object comes into existence because it is a local variable in a function, the destructor will be called when the function returns. class Vect { public: Vect(int size = 5) { Vect(int size = 5) { vectSize = size; vectSize = size; theVect = new int[size]; theVect = new int[size]; } //... //... ~Vect() { ~Vect() { delete [] theVect; delete [] theVect; }private: int vectSize; int* theVect; };

27
27 Copy Constructor & Assignment Operator The assignment operator deletes the existing array storage, allocates a new array of the proper size, and copies the elements into this array. The if-statement checks against the possibility of self assignment (a = a). This is done using the “this” keyword. For any class object, this is a pointer to the address of the object. The assignment operator deletes the existing array storage, allocates a new array of the proper size, and copies the elements into this array. The if-statement checks against the possibility of self assignment (a = a). This is done using the “this” keyword. For any class object, this is a pointer to the address of the object. Vect::Vect(const Vect &a) { vectSize = a.vectSize; vectSize = a.vectSize; theVect = new int[vectSize]; theVect = new int[vectSize]; for (int i=0;i
{
"@context": "http://schema.org",
"@type": "ImageObject",
"contentUrl": "http://images.slideplayer.com/14/4224189/slides/slide_27.jpg",
"name": "27 Copy Constructor & Assignment Operator The assignment operator deletes the existing array storage, allocates a new array of the proper size, and copies the elements into this array.",
"description": "The if-statement checks against the possibility of self assignment (a = a). This is done using the this keyword. For any class object, this is a pointer to the address of the object. The assignment operator deletes the existing array storage, allocates a new array of the proper size, and copies the elements into this array. The if-statement checks against the possibility of self assignment (a = a). This is done using the this keyword. For any class object, this is a pointer to the address of the object. Vect::Vect(const Vect &a) { vectSize = a.vectSize; vectSize = a.vectSize; theVect = new int[vectSize]; theVect = new int[vectSize]; for (int i=0;i

39
39 Inheritance In a hierarchy of classes, the classes towards the top are known as “base classes” or “super classes”. Those further down are the “derived classes” or “sub-classes”. In a hierarchy of classes, the classes towards the top are known as “base classes” or “super classes”. Those further down are the “derived classes” or “sub-classes”. Derived classes are specialisations of base classes, sharing their properties but adding their own. Derived classes are specialisations of base classes, sharing their properties but adding their own. Motor Vehicles Motor BikesCars StreetChopper4 x 4Saloon 4 doors5 doors2 doors

45
45 Calling the base class constructor from the derived class Calendar::Calendar(int d, int m, int y) : Today(d, m, y) { for (int i=0; i<366; i++) birthdaySize[i] = 0; } The constructor Today (with parameter values from the parameters of Calendar ) is invoked by placing it in a member initialization list. The constructor Today (with parameter values from the parameters of Calendar ) is invoked by placing it in a member initialization list. If there has been a base class that requires no parameters, then the compiler would ensure that it would be called automatically, before the derived class constructor. If there has been a base class that requires no parameters, then the compiler would ensure that it would be called automatically, before the derived class constructor. However, even if no parameters are required, it does not harm to invoke the base class constructor explicitly, as above. However, even if no parameters are required, it does not harm to invoke the base class constructor explicitly, as above.

49
49 Arrays of Mixed Object Types int main() { Time* t[2]; t[0] = new Time24(15, 29, 30); t[1] = new Time12(22, 30, 45); t[0]->DisplayTime(); t[1]->DisplayTime(); return 0; } 15 29 30 22 30 45 The problem with arrays is that each of its members must be of the same type. The problem with arrays is that each of its members must be of the same type. However, we can declare an array of pointers to the base class and assign objects of deferent derived classes to it. However, we can declare an array of pointers to the base class and assign objects of deferent derived classes to it. It must be noted however that the function DisplayTime executed is the one belonging to the base class. It must be noted however that the function DisplayTime executed is the one belonging to the base class.

50
50 Polymorphism Polymorphism means having many shapes and it refers to the ability to use the same name to perform different functions in different classes. Polymorphism means having many shapes and it refers to the ability to use the same name to perform different functions in different classes. One form of polymorphism is overloading functions. One form of polymorphism is overloading functions. Another one is by using virtual functions in the base classes. Another one is by using virtual functions in the base classes. class Time { public: Time(int, int, int); virtual void DisplayTime() const; protected: int hour, min, sec; hourType hour_type; }; Time::Time(int h, int m, int s) : hour(h), min(m), sec(s) { hour_type = unknown; } void Time::DisplayTime() const { cout << hour << ’\n’ << min << ’\n’ << sec << endl; }

51
51 Virtual Functions int main() { Time* t[3]; t[0] = new Time(8, 16, 12); t[1] = new Time24(15, 29, 30); t[2] = new Time12(22, 30, 45); t[0]->DisplayTime(); // from base class Time t[1]->DisplayTime(); // from derived class Time24 t[2]->DisplayTime(); // from derived class Time12 delete []t; return 0; } 8 16 12 15:29:30 10:30:45 pm One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. With non-virtual functions the compiler contains a specific reference to the function (static binding). With non-virtual functions the compiler contains a specific reference to the function (static binding). With virtual functions the compiler produces code that will invoke the correct function for the object that is being referred to while the program is running (dynamic binding). With virtual functions the compiler produces code that will invoke the correct function for the object that is being referred to while the program is running (dynamic binding).

56
56 Nesting Classes and Types within Classes class Complex { private: class Node { //... (Node definition here) } //... (Remainder of complex definition) }; A class definition can be nested within another class. A class definition can be nested within another class. In the example below only the Complex class can use the Node class. We can also embed other sorts of user-defined entities inside the class definition, such as enumerations, structures, and typedefs. In the example below only the Complex class can use the Node class. We can also embed other sorts of user-defined entities inside the class definition, such as enumerations, structures, and typedefs.

61
Template Specialization 61 template class myContainer {...}; template <> class myContainer {...}; A different implementation to a template can be defined when a specific type is passed as a template parameter. To do this a specialization of that template must be declared. A different implementation to a template can be defined when a specific type is passed as a template parameter. To do this a specialization of that template must be declared. Class template Class template specialization When specializations for a template class are declared, all its members, even those exactly the same to the generic template class, must be defined. The reason to this is that there is no “inheritance” of members from the generic template to the specialization. When specializations for a template class are declared, all its members, even those exactly the same to the generic template class, must be defined. The reason to this is that there is no “inheritance” of members from the generic template to the specialization.