Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.

Similar presentations


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

1 Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015

2 Part I: Hand-Written Exercises 1. Suppose your program contains the following class, class SmartPhone { 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: SmartPhone iphone6, samsung_s6, htc1_m9; double price, profit; (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) iphone6.price = 299.99; (b) samsung_s6.setPrice(199.97); (c) htc1_m9 = samsung_s6; (d) price = htc1_m9.getPrice(); (e) profit = iphone6.profit; (f) iphon6.setProfit(samsung_s6.getProfit());

5 2. (a) Explain what public: and private: mean in a class definition. (b) What should be made public and what should be private? Please describe the reasons. (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 are static variables and static functions ? When do we need them? Please give an example.

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 Day that is an abstract data type for a day. Your class will have one member variable of type int to represent a day (1 for Monday, 2 for Tuesday, and so forth). Include all the following member functions: (a) a constructor to set the day using the first three letters in the name of the day as one string argument (“MON” for Monday, “TUE” for Tuesday, and so forth), (b) a constructor to set the day using an integer as an argument (1 for Monday, 2 for Tuesday, and so forth), (c) a default constructor, (To be continued on the next slides…)

8 (… continued from the previous slides) (d) an input function that reads the day as an integer, (e) an input function that reads the day as the first three letters in the name of the day, (f) an output function that displays the day as an integer, (g) an output function that displays the day as the first three letters in the name of the day, (h) and a member function that returns the next day as a value of type Day. Embed your class definition in a C++ test program.

9 Homework Submission Due: 2 weeks (May 8, 2015) 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 or Comments?


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

Similar presentations


Ads by Google