Documente Academic
Documente Profesional
Documente Cultură
Seventh Edition
Chapter 15:
Classes and Objects
Objectives
Differentiate between procedure-oriented and objectoriented programming
Define the terms used in object-oriented programming
Create a class definition
Instantiate an object from a class that you define
Create a default constructor
Create a parameterized constructor
Include methods other than constructors in a class
Overload the methods in a class
An Introduction to Programming with C++, Seventh Edition
Object-Oriented Terminology
In a procedure-oriented program, the primary
components are the tasks to accomplish
In an object-oriented program, the primary components
are objects
An object is anything that can be seen, touched, or used.
In C++, you can also define you own classes and create
instances (objects) from those classes
Use a class definition to specify the attributes and
behaviors
10
11
12
13
14
15
16
17
18
Header Files
Although you can enter a class definition in the program
that uses the class, most programmers enter a class
definition in a separate text file called a header file
Header filenames end with .h
Use an #include directive to include the header file in a
program
19
Figure 15-7 Square class definition entered in the Square.h header file
An Introduction to Programming with C++, Seventh Edition
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Summary
A class encapsulates all of an objects attributes and
behaviors
Attributes: the characteristics that describe the object
Behaviors: the actions that the object can perform or to
which the object can respond
35
Summary (contd.)
Polymorphism: the object-oriented feature that allows
the same instruction to be carried out differently
depending on the object
A class definition contains two sections: declaration and
implementation
Another name for creating an object is instantiation
36
Summary (contd.)
Most C++ programmers enter class definitions in header
files, which have a file extension of .h
You can use a constructor to initialize the data
members in a class when an object is instantiated
Each constructor in a class has the same name, but its
formal parameters (if any) must be different from any
other constructor in the class
A constructor does not have a data type, because it
cannot return a value
You can overload the methods in a class
An Introduction to Programming with C++, Seventh Edition
37
38
39
40
41
42