C++
Lecture 4Tuesday, 15 July 2003
Struct & Classes
Structure in C++ Classes and data abstraction Class scope Constructors and destructors Examples
OOP Key Concepts
OOP encapsulates data (attributes) and function (behavior) into packages called classes.
Class is like a blueprint of house. One can build many houses (of the same type) with one blueprint.
Out of a class, programmer can create object.
Structure Definition
struct Time { int hour; int minute; int second;
};
Declaration
Time timeObject, // a variable timeArray[10], // array *timePtr, // pointer to...
&timeRef = timeObject; // ref
Accessing Members of Structure
cout << timeObject.hour;cout << timeRef.hour;timePtr = &timeObject;cout << timePtr -> hour;cout << (*timePtr).hour; // the same
Example Fig.6.1
Structure - declaration and use.
Conditional expression expr ? A : B the value is A if expr is true and B
if expr is false.
C.f. Fig.6.1.
Abstract Data Type with a Class
class Time {public:
Time(); // constructor void setTime(int, int, int); void printMilitary(); void printStandard();
private: int hour; int minute; int second;};
Declaration of Objects of Type "Time"
Time sunset, // object of type Time arrayOfTimes[5], // array of Time obj *ptrToTime, // pointer to Time obj &dinnerTime = sunset; // reference
What is an Object?
Object is a variable, Object is a function? Object is data and functions Object is an abstraction of
something that has attributes (property) and operations (function calls).
Member Functions
Member functions of Time are defined outside the class with :: binary scope resolution operator.
E.g.,void Time::setTime(int h, int m, int s){
hour = ( h>= 0 && h < 24) ? H : 0;...
}
Class Example Fig.6.3
Class definition Member function definition Main program
C.f. Fig. 6.3.
Class Scope
Class data members and member functions belong to that class's scope.
Within a class's scope, class members are references by name.
Outside a class's scope, class members are referenced through one of the handles on an object.
The Handles
Use dot (.) notation for object and references.
Use arrow (->) for pointer to the object
E.g., c.x , cpt -> x
C.f. Fig. 6.4
Separating interface from implementations
Header files contains class declarations only
Class function definition in source file
Driver program in another file
Public and Private Data or Function
Public data or functions are accessible from outside
Private data or functions are not directly accessible by the user outside the class scope
Public functions are used as an interface to access or modify private data
Initializing Class Objects with Constructors
A constructor is a class member function with the same name as the class.
The constructor is called whenever an object of that class is created.
The constructor does not return a value (not even void).
Destructors
The name of the destructor for a class is the tilde (~) character followed by the class name.
Destructor is called when an object is going to disappear (out of scope).
When Constructors and Destructors are Called
Example of Fig. 6.15-17, constructor and destructor of a class.
C.f. Fig. 6.15-17.
Default Constructor
class Time { public: Time(); // default constructor Time(int, int, int); ….}Used asTime t, t(12, 06, 0); C.f. Fig.18-20
Assignment by Default Memberwise Copy
If date1 and date2 are objects of the same type, we can say
date2 = date1; The date members values are
copied memberwise.
C.f. Fig.6.24
Questions
In the class assignment example, if the member data are arrays, are the values of the array elements copied?
If it is a pointer, is the pointer value (address) copied?
If a pointer pointing to an array, are the values of array elements copied?
Copy Constructor
You can override what the class assignment a=b means by write a so-called copy constructor
We declare and implement a function, e.g., for the class DateDate(Date &d)
C.f. an example in Fig.8.4-8.6
Problems
Find error(s) in each of the following and explain how to correct it.• Assuming the following prototype is
declared in class Time.• void ~Time(int);
Problems
Find error(s) - The following is a partial definition of class Time:class Time { public: // function prototypes private: int hour = 0; int minute = 0; int second = 0;};
Problems
Find error(s) – Assuming the following prototype is declared in class Employee:int Employee(const char *, const char *)
Top Related