Professional Documents
Culture Documents
PROGRAMMING IN C/C++
Class 11: Chapter 10 Introduction to Classes
November 18, 2017
Agenda
Object-Based Programming
Creating Your Own Classes
Constructors
Using Classes with Examples
Class Scope and Duration Categories
Break
Lab
Object-
Object-Based Programming
Class can be considered a construction plan for objects and how they
can be used
Plan lists the required data items and supplies instructions for using the data
Many objects can be created from the same class
Each different object type requires its own class
Two components:
Declaration section: declares data types and function for class
Implementation section: defines functions whose prototypes were declared
in declaration section
Class Construction (cont'd.)
Notice the ;
It indicates the end of the class declaration
Class Construction (cont'd.)
public:
Date(int = 7, int = 4, int = 2012); member methods
void setDate(int, int, int);
void showDate(void);
}; // this is a declaration - don't forget the semicolon
Class Construction (cont'd.)
Functions: defined the same as other C++ functions but also include
scope resolution operator :: to identify function as member of class
Class Construction (cont'd.)
Program10.1.cpp
Class Construction (cont'd.)
We defined the class. We can now create objects of this class
Description of main():
Three objects of class Date defined
Constructor function: Date(), automatically called
Memory allocated for each object
Data members of the objects initialized
Object a: no parameters assigned; therefore, defaults are used:
a.month = 7
a.day = 4
a.year = 2012
Class Construction (cont'd.)
Object-Based Programming
Creating Your Own Classes
Constructors
Using Classes with Examples
Class Scope and Duration Categories
Break
Lab
Constructors
Output:
Created a new data object with data values 7, 4, 2012
Created a new data object with data values 7, 4, 2012
Created a new data object with data values 4, 1, 2009
Calling Constructors
Program 10.4
Arrays of Objects
Object-Based Programming
Creating Your Own Classes
Constructors
Using Classes with Examples
Class Scope and Duration Categories
Break
Lab
Example 1: Constructing a Room Object
Functions needed:
Constructor: to specify an actual length and width value when a room is
instantiated
Accessor: to display rooms length and width
Mutator: to change above values
Function to calculate rooms floor area from its length and width values
Example 2: Constructing an Elevator Object
Functions needed:
Constructor: initializes the elevators number, starting floor position, and highest floor
request(): to alter the elevators position
Class Scope and Duration Categories
Class data members are local to the class in which theyre declared
Class method names are local to the class theyre declared in and
can be used only by objects declared for the class
Class Scope and Duration Categories (contd.)
Static Class Members
As each class object is created, it gets its own block of memory for its
data members
In some cases, its convenient for every created object to share the
same memory location for a specific variable
C++ handles this situation by declaring a class variable to be static
Static class variables share the same storage space for all class objects
They act as global variables for the class and provide a means of
communication between objects
Program 10.7
Friend Functions
Friend functions
Nonmember methods that are granted the same privileges as its member
methods
Series of method prototype declarations preceded with the keyword friend
and included in the class declaration section
Program 10.9
Break !
Lab 6 next
Homework 6 is posted