Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
Tutorial-04 (Solutions) Computer and Programming Course Code: KEET 1150
Tips: Also please try to solve the Self Review Exercises and Exercises of Chapter 3 provided in the book: H.M Deitel, P. J. Deitel, “C++ How to Program”->Chapter 3: Introduction to Classes and Objects, and Chapter 4: Control Statements: Part 1 [only the parts taught in lecture session] 1. Explain how a program could use class string without inserting a using declaration. ANS: A program could create string variables without a using declaration if each occurrence of the word string were prefixed by the namespace std and the binary scope resolution operator (::), as in std::string. 2. Explain why a class might provide a set function and a get function for a data member. ANS: A data member is typically declared private in a class so that only the member functions of the class in which the data member is declared can manipulate the variable. A class typically provides a set function and a get function for a data member to allow clients of the class to manipulate the data member in a controlled manner. A set function should validate the data it is setting to ensure that invalid data is not placed in the object. A get function can return the value of a data member without allowing clients to interact with the data member directly. In addition, set and get functions hide the internal data representation. 3. (Modifying Class GradeBook) Modify class GradeBook as follows: a) Include a second string data member that represents the course instructor’s name. b) Provide a set function to change the instructor’s name and a get function to retrieve it. c) Modify the constructor to specify course name and instructor name parameters. d) Modify function displayMessage to output the welcome message and course name, then the string "This course is presented by: " followed by the instructor’s name. Use your modified class in a test program that demonstrates the class’s new capabilities.
Sample Output:
1
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
ANS:
2
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
3
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
4. (Employee Class) Create a class called Employee that includes three pieces of information as data members—a first name (type string), a last name (type string) and a monthly salary (type int). [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—called floating-point values—to represent dollar amounts.] Your class should have a constructor that initializes the three data members. Provide a set and a get function for each data member. If the monthly salary is not positive, set it to 0. Write a test program that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10 percent raise and display each Employee’s yearly salary again. Sample Output:
ANS:
4
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
5
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
6
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
5. (Date Class) Create a class called Date that includes three pieces of information as data members— a month (type int), a day (type int) and a year (type int). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in the range 1–12; if it is not, set the month to 1. Provide a set and a get function for each data member. Provide a member function displayDate that displays the month, day and year separated by forward slashes (/). Write a test program that demonstrates class Date’s capabilities.
ANS:
7
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
8
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
6. (Dangling-else Problem) State the output for each of the following when x is 9 and y is 11 and when x is 11 and y is 9. Note that the compiler ignores the indentation in a C++ program. The C++ compiler always associates an else with the previous if unless told to do otherwise by the placement of braces {}. On first glance, you may not be sure which if and else match, so this is referred to as the “dangling-else” problem. We eliminated the indentation from the following code to make the problem more challenging. [Hint: Apply indentation conventions you have learned.]
9
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
Sample Output:
ANS:
10
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
11
Ahmed Wasif Reza, Lecturer, Electrical Engineering (L-08-04) Phone: 03-7967 7631, E-mail:
[email protected]
12