BIG C++ Cay Horstmann & Timothy Budd
Laboratory Notebook Chapter 3 - Objects
Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience in
Declaring an object to store information is like declaring a variable of numeric data type. For instance:
int counter; /* makes an int */ Time lunch_time; /* makes a Time object */
While both of these statements declare a new variable, there is a difference in how each is initialized. When declaring counter, the value will be whatever is leftover from that memory location's last use. Quality tip 2.x suggests setting int counter = 0; to avoid this problem.
Objects are initialized when they are defined, based upon the object type. Time objects are initialized to the current time. If you do not want to initialize an object with the default, you supply construction parameters. For example, you can construct a Time object by specifying hour, minute and second, for example Time (11,20,0)
To what values is a Time object set by Time new_time = Time()
Initialize a Time variable to the time you usually get up.
What is the outcome of constructing a Time object with
Time next_date(Saturday, 18, 0, 0);
What is the outcome of Time next_date = 6:00pm?
What is the difference between Time next_date(18, 0, 0) and next_date = Time (18, 0, 0);
An object's member functions are applied to a particular instance of an object type using the dot-notation. For example, time_var.get_seconds() returns the number of seconds stored in a particular Time object called time_var. Member functions that return stored information when applied to an object are called accessors, those that change the stored information are called mutators.
Given two instances of the Time class, the beginning and end of a typical workday:
tstart = Time(9,0,0) tend = Time(17,0,0)
What is the result of each of the following:
cout << tstart.get_hours() << " / " << tstart.get_minutes()
tend.add_seconds(1200);
tend.seconds_from(tstart);
A direct and natural comparison can be made between an object and the entity that it represents. For example, here is how you might construct and access objects of an Address class:
Address a(number, street_name, street_type); a.get_number(); a.get_street_name(); a.get_street_type();
Personal information about an individual would be another common example. Give a constructor and corresponding accessors for personal information having the following form:
Serdni Vasnar 096-33-2123 December 30, 1949
Write a program that uses five Line objects to draw a pentagon, like this:
/* paste program here */
Use Circle and Line objects to draw a train engine like this :
Generate five circles with center (0,0) and radius 1, 2, 3, 4, and 5. Use the move method of the Circle class to draw the circles all tangent at a common point, like this:
Given a coordinate system where the upper lefthand corner is (0,0) and the lower righthand corner is (3,3), write a graphics program to draw a stopsign like this:
Change the program to display the same stopsign in a coordinate system with upper left = (-2.5, 1.5) and lower-right = (1.5, -3.5).
Which coordinate system was easier for you to work with? Why?
Modify the phoenix.cpp program of the textbook to display the points in a zoomed-out coordinate system, that is, the curve should look as though it is being viewed from a greater distance, but still be centered, like this:
#include "ccc_win.cpp" int main() { cwin.coord(1, 33, 12, 11) // only change this line!!! cwin << Point(1, 11); cwin << Point(2, 12); cwin << Point(3, 16); cwin << Point(4, 20); cwin << Point(5, 25); cwin << Point(6, 31); cwin << Point(7, 33); cwin << Point(8, 32); cwin << Point(9, 29); cwin << Point(10, 23); cwin << Point(11, 16); cwin << Point(12, 12); return 0; }
Write a program that:
On a Cartesian coordinate plane with upper-left = (-10,10) and lower-right = (10, -10), write a program that:
Verify the appearance of the Y-intercept by calculating its value by hand. Show your work here.
Do not forget to send your answers when you are finished.