Chapter 3: Objects
- To become familiar with objects
- To learn about the properties of several
sample classes that were designed for this book
- To be able to construct objects and supply
- To understand member functions and the dot
- To be able to modify and query the state of an
object through member functions
- An object is a value that can be
created, stored and manipulated in a programming language.
string greeting = "Hello";
cout << greeting.substr(0,4);
- Every object in C++ must belong to a class.
A class is a data type (like int or
double) that is programmer defined, whereas int and double are defined by the designers of the C++ language.
We will learn to work with the class Time
and the class Employee.
- Remember, these are programmer defined and
not part of standard C++.
Suppose you want to know how many seconds will
elapse between now and midnight.
To use the Time class we must
include the file that contains its definition:
- We use " " instead of <
> because the file is not a standard (system)
- The name stands for Computing
Concepts with C++ Essentials.
- Creating any object is called construction.
- We can construct an object just like we create
We initialize an object by passing construction parameters
when the object is created.
Time day_end = Time(23, 59, 59); /* the last second of the day */
double interest_rate = 3.5;
string greeting = "Hello";
The shorthand in this very common situation is:
Time day_end(23, 59, 59); /* the last second of the day */
Creating objects without parameters is called
No parameters means not parenthesis! The Time class creates an object with the current time.
Time now = Time(); /* the time this object is created */
Time now; /* the time this object is created */
Time later(); /* NO! */
An object can be created anytime by supplying
the class name with (or without) parameters. The assignment
operator changes the value of the
sometime = Time(12, 5, 18); /* we already declared sometime */
Syntax 3.1: Constant Object Construction
Time(19, 0, 0);
||Construct a new object for use in
Syntax 3.2: Object Variable Definition
Class_name variable_name(construction parameters);
Time homework_due(19, 0, 0);
||Define a new object variable and
supply parameter values for initialization.
- A function applied to an object with the dot
notation is called a member function.
greeting.length(); /* returns the length of greetings */
greeting.substr(0,1); /* returns a substring */
- The Time class has several member functions
used to find the state of the object:
now.get_seconds(); /* returns the seconds value of now */
now.get_minutes(); /* returns the minutes value of now */
now.get_hours(); /* returns the hours value of now */
- You can add a certain number of seconds to the
now.add_seconds(1000); /* add 1000 seconds to the time stored in now */
The Time class does not have any member functions
used to set the state of the object to avoid abuse:
now.set_hours(2); /* NO! Not a supported member function */
now.set_hours(9999); /* NO! Doesn't make sense */
The Time class does provide other member functions to
facilitate other actions:
now.add_seconds(1000); /* Changes now to move by 1000 seconds */
now.seconds_from(day_end); /* Computes number of seconds between now and day_end */
- Object-oriented programming allows us to
easily model entities from real life.
As an example, the text provides a simple Employee class:
- All employees have names and salaries
which are set at construction.
Employee harry("Hacker, Harry", 45000.00);
- Both attributes can be queried.
cout << "Name: " << harry.get_name() << "\n";
cout << "Salary: " << harry.get_salary() << "\n";
- Only salary can be set.