OBJECT – ORIENTED PROGRAMMING EXERCISES
Direction: Perform the following tasks:
1. a. Create a class named TestClass that holds a single private integer field and a public constructor. The only statement in the constructor is one that displays the message “Constructing”. Write a main()function that instantiates one object of the TestClass. Save the file as TestClass.cpp. Run the program and observe the results.
b. Write another main()function that instantiates an array of 10 TestClass objects. Save the file as TestClassArray.cpp. Run this program and observe the results.
2. Create a class named Car. The Car class contains a static integer field named count. Create a constructor that adds 1 to the count and displays the count each time a Car is created. Create a destructor that subtracts 1 from the count and displays the count each time a Car goes out of scope. Write a main()function that declares an array of five Car objects. Output consists of five constructor messages and five destructor messages, each displaying the current count, similar to the output in Figure 8-34. Save the file as Car.cpp.
2. Create a class named Instructor. It contains a first and last name and an office
number, and its only constructor requires all three as arguments. Create a class named Classroom. It contains a building and a room number, and its only constructor requires both as arguments. Create a class named CollegeCourse. A CollegeCourse contains an Instructor, a Classroom, and a number of credits. Its constructor requires a first and last name of an instructor, the instructor’s office number, a Classroom building and room number, and a number of credits. Each of these classes contains a function that displays an object’s values. Write a main()function that instantiates at least two CollegeCourse objects and displays their values. Save the file as CollegeCourse.cpp.