920 likes | 1.1k Views
More about Classes and Data Abstraction. const specifier is used for not modifiable objectsC would not let any member functions to call for const object, unless the member functions themselves are also declared const. Even for get functions. In class declaration: int getHour() c
E N D
1. More about Classes and Data Abstraction const specifier and const member function
friend function
Dynamic allocation
static data member and function member
this point
2. More about Classes and Data Abstraction const specifier is used for not modifiable objects
C++ would not let any member functions to call for const object, unless the member functions themselves are also declared const. Even for get functions
3. More about Classes and Data Abstraction Member functions declared with const can not modify the const object.
A non-const member function can not even access const object, without modification on the object.
The following example shows the combination of cont/non-const objects and invoked const/non-const member functions.
12. More about Classes and Data Abstraction Member initializer can be used to initialize const data member within constructor.
Example:
16. More about Classes and Data Abstraction
17. More about Classes and Data Abstraction If we use an assignment statement to initialize const data member, it will fail.
Example:
22. More about Classes and Data Abstraction Composition: Objects can be members of classes
Here is a example to show two classes: Employee and Data, and the data members of Employee Class are the objects of Data Class
37. More about Classes and Data Abstraction friend function of a class is defined outside that class's scope.
A function or entire class may be declared to be a friend of another class.
How to declare a friend function or a friend of a class?
44. More about Classes and Data Abstraction Each object has access to its own address through a pointer called "this".
"this" pointer is a special pointer, which compiler assign to non-static member function call to object.
Use this pointer, we can implicitly access both the data and function members of a class.
It can also be used explicitly.
45. More about Classes and Data Abstraction In a non-constant member function of a class, this pointer of class Employee, the this pointer has type Employee *const (a constant pointer to an Employee object)
In a constant member function of class Employee the this pointer has type const Employee * const (a constant pointer to an Employee object that is a constant)
Example:
59. More about Classes and Data Abstraction
60. More about Classes and Data Abstraction
61. More about Classes and Data Abstraction
62. More about Classes and Data Abstraction
63. More about Classes and Data Abstraction
64. More about Classes and Data Abstraction
65. More about Classes and Data Abstraction
66. More about Classes and Data Abstraction
67. More about Classes and Data Abstraction
68. More about Classes and Data Abstraction
69. More about Classes and Data Abstraction
70. More about Classes and Data Abstraction
71. More about Classes and Data Abstraction Dynamic allocation with operators new and delete
With comparison of C's malloc() and free() functions
Dynamic allocation can be used to dynamically reserve variables or objects of different types during execution procedure.
72. More about Classes and Data Abstraction In review C, we have
73. More about Classes and Data Abstraction In C++, we have
74. More about Classes and Data Abstraction pointer to array dynamically
75. More about Classes and Data Abstraction Static Class Members
If a variable is shared with all objects of a class, a static data member of the class can be introduced.
Static data members have class scope.
Static data member can be initialized.
Static data member can be referenced through any member functions of the class.
Example: static data and function members
85. More about Classes and Data Abstraction Data abstraction and information hiding
Classes normally hide their implement details from the clients of the classes. This is called information hiding
Data structure (stack example, last-in and first out)
hide stack implementation in data structure
Stacks can be created by either array or linked list.
Describing the function of a class independent of its implementation is called data abstraction
Clients only need to know is the interface to communicate the class
86. More about Classes and Data Abstraction Examples of ADT (textbooks)
Container class (collection class): designed to hold collections of objects
Container class commonly provide services such as insertion, deletion, sort, search,
Examples: array, stacks, queue, tree, linked list,
Iterators (iterative objects)
87. More about Classes and Data Abstraction Proxy classes