Lecture 05
Examples: Classes and Data Abstraction cont.
C++ Programming: Program Design Including Data Structures, Eighth Edition
1
Examples
Classes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
The Components of a Class
1 // [Link]
2 // Define class GradeBook with a member function displayMessage;
3 // Create a GradeBook object and call its displayMessage function.
4 #include <iostream>
Beginning of class
5 using std;
7
definition for class
8 // GradeBook class definition
GradeBook
9 class GradeBook Beginning of class
10 {
body
11 public:
Access specifier public;
12 // function that displays a welcome message to the GradeBook user
13 void displayMessage()
makes members available to
14 { the public
15 cout << "Welcome to the Grade Book!" << endl;
16 } // end function displayMessage
17 }; // end class GradeBook
18 Member function displayMessge
19 // function main begins program execution returns nothing
End of class
20 int main()
21 {
body
22 GradeBook myGradeBook; // create a GradeBook object named myGradeBook
23 [Link](); // call object's displayMessage function
24 return 0; // indicate successful termination
Use dot operator to call
GradeBook’s member function
25 } // end main
Welcome to the Grade Book!
Deitel & Deitel
3
3
Examples of Classes :Example 1
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 4
or otherwise on a password-protected website for classroom
Examples of Classes :Example 1
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 5
or otherwise on a password-protected website for classroom
Examples of Classes
:Example 1
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 6
or otherwise on a password-protected website for classroom
Examples of Classes :Example 1
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 7
or otherwise on a password-protected website for classroom
Examples of Classes :Example 2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 8
or otherwise on a password-protected website for classroom
Examples of Classes :Example 2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 9
or otherwise on a password-protected website for classroom
Examples of Classes :Example 2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 10
or otherwise on a password-protected website for classroom
Examples of Classes :Example 2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 11
or otherwise on a password-protected website for classroom
Quick Review (1 of 3)
• A class is a collection of a fixed number of components
• Components of a class are called the members of the class
• Accessed by name
• Classified into one of three categories: private, protected, and public
• In C++, class variables are called class objects or class instances or,
simply, objects
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 14
or otherwise on a password-protected website for classroom
Quick Review (2 of 3)
• The only built-in operations on classes are assignment and member selection
• Constructors guarantee that data members are initialized when an object is
declared
• A default constructor has no parameters
• The destructor automatically executes when a class object goes out of scope
• A class can have only one destructor
• The destructor has no parameters
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 15
or otherwise on a password-protected website for classroom
Quick Review (3 of 3)
• An abstract data type (ADT) is a data type that separates the logical properties
from the implementation details
• A public static member, function or data, of a class can be accessed
using the class name and the scope resolution operator, ::
• static member variables of a class exist even when no object of the
class type exists
• Instance variables are non-static data members
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 16
or otherwise on a password-protected website for classroom