Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014.

Similar presentations


Presentation on theme: "Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014."— Presentation transcript:

1 Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014

2 Part I: Hand-Written Exercises 1. Suppose your program contains the following class, class CellPhone { public: void setPrice(double newPrice); void setProfit(double newProfit); double getPrice(); private: double price; double profit; double getProfit(); }; (To be continued on the next slide…)

3 (…continued from the previous slide) And suppose the main function of your program contains the following declaration and that the program somehow sets the values of all the member variables to some values: CellPhone nokia, samsung; double aPrice, aProfit; (To be continued on the next slide…)

4 (…continued from the previous slide) Which of the following statements are then allowed in the main function of your program? For illegal ones, please explain the reasons why they are illegal. (a) nokia.price = 1299.99; (b) samsung.setPrice(2000.97); (c) aPrice = samsung.getPrice(); (d) aProfit = samsung.getProfit(); (e) aProfit = nokia.getProfit(); (f) nokia = samsung;

5 2. (a) Explain what public: and private: mean in a class definition. (b) When you define a C++ class, should you make the member variable public or private? Should you make the member functions public or private? (c) When you define a C++ class, what items are considered part of the interface ? What items are considered part of the implementation ? 3. What is a default constructor ? Does every class have a default constructor?

6 Part II: Programming Exercises 4.Define a class for a type called CounterType. An object of this type is used to count things, so it records a count that is a nonnegative integer. Include the following member functions: (a) a function that sets the counter to a count given as an argument, (b) functions that increase the count by one and decrease the count by one. Be sure that no member function allows the value of the counter to become negative or out of range for nonnegative whole numbers, (c) a function that returns the current count value, and (d) a function that outputs the count. Embed your class definition in a C++ test program.

7 5.Define a class called Time that is an abstract data type for a particular point of time. Your class will have member variables to represent hour, minute, and second in 24-hour clock format. Include all the following member functions: (a) a default constructor, (b) a constructor to set the time using three arguments, (c) the get functions to get the current hour, minute, and second, respectively, (To be continued on the next slides…)

8 (… continued from the previous slides) (d) an output function that displays the time in universal-time format (e.g. 13:30:07), (e) an output function that displays the time in standard-time format (e.g. 1:27:06 PM), (f) and a member function tick() that increments the time stored in a Time object by one second. Write a C++ program that tests all the member functions to illustrate that they work correctly. Be sure to test the following cases: incrementing into the next minute, hour, or day.

9 Homework Submission Due: 2 weeks (Apr. 28, 2014) Hand-written exercises –Please write your name and answers on papers Programs –Please submit to http://mslin.ee.ntut.edu.tw/http://mslin.ee.ntut.edu.tw/ – File names : Please name your file according to our homework. For example, _HW1.zip or _quiz2.rar

10 Any Questions?


Download ppt "Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014."

Similar presentations


Ads by Google