Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #3: Classes and Constructors

Similar presentations


Presentation on theme: "Homework #3: Classes and Constructors"— Presentation transcript:

1 Homework #3: Classes and Constructors
By J. H. Wang Apr. 19, 2011

2 Part I: Hand-Written Exercises
1. Suppose your program contains the following class definition (along with definitions of the member functions): class YourClass { public: YourClass(int newInfo, char moreNewInfo); YourClass(); void doStuff(); private: int information; char moreInformation; }; (To be continued on the next slide…)

3 (…continued from the previous slide)
Which of the following are legal? For illegal ones, please explain the reasons why they are illegal. (a) YourClass anObject(42, ‘A’); (b) YourClass anotherObject; (c) YourClass yetAnotherObject(); (d) anObject = YourClass(99, ‘B’); (e) anObject = YourClass(); (f) anObject = YourClass;

4 2. (1) Explain what public: and private: mean in a class definition.
(2) When you define a C++ class, should you make the member variables public or private? Should you make the member functions public or private? Please explain the reasons why. 3. What is a default constructor? Does every class have a default constructor?

5 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: (1) a function that sets the counter to a count given as an argument, (2) 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, (3) a function that returns the current count value, and (4) a function that outputs the count. Embed your class definition in a C++ test program.

6 5. Define a class for a type called Month that is an abstract data type for a month. Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so forth). Include all the following member functions: (1) a constructor to set the month using the first three letters in the name of the month as one string argument (“JAN” for January, “FEB” for February, and so forth), (2) a constructor to set the month using an integer as an argument (1 for January, 2 for February, and so forth), (3) a default constructor, (To be continued on the next slides…)

7 (… continued from the previous slides) (4) an input function that reads the month as an integer, (5) an input function that reads the month as the first three letters in the name of the month, (6) an output function that displays the month as an integer, (7) an output function that displays the month as the first three letters in the name of the month, (8) and a member function that returns the next month as a value of type Month. Embed your class in a C++ test program.

8 Homework Submission Due: 2 weeks (May 3, 2011) Hand-written exercises
Please write your name and answers on papers Programs Please submit to File names: Please name your file according to our homework. For example, HW1.zip or quiz2.rar

9 Any Questions?


Download ppt "Homework #3: Classes and Constructors"

Similar presentations


Ads by Google