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. 17, 2012

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

3 (…continued from the previous slide)
And suppose the main function of your program contains the following declarations and that the program somehow sets the values of all the member variables to some values: Automobile hyundai, jaguar, tata; double aPrice, aProfit; int cnt; (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) hyundai.price = ; (b) jaguar.setPrice( ); (c) tata.setPrice( ); (d) tata.setCount(500); (e) aPrice = jaguar.getPrice(); (f) aProfit = jaguar.getProfit(); (g) hyundai =jaguar; (h) tata.count = 500;

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 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?

6 Part II: Programming Exercises
Define a class IntSet that can store a set of integers. Write the following member functions: (a) a default constructor and a constructor that can put an array of integers into the set, (b) a member function that puts a single integer into the set, (c) a member function that returns the current number of elements in the set, (d) two member functions that computes the sum and average of the current set of integers respectively, (e) a member function that lists all the integers in the current set. Embed the class in a C++ test program.

7 5. Define a class Month that is an abstract data type for a month
5. Define a class 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 on). Include the following member functions: (a) a constructor to set the month using the first three letters in the English name of the month as one string argument (“JAN” for January, “FEB” for February, and so forth), (b) a constructor to set the month using an integer as an argument (1 for January, 2 for February, 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 month as an integer, (e) an input function that reads the month as the first three letters in the name of the month, (f) an output function that displays the month as an integer, (g) an output function that displays the month as the first three letters in the name of the month, (h) and a member function that returns the next month as a value of type Month. Embed your class in a C++ test program.

9 Homework Submission Due: 2 weeks (May 1, 2012) 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

10 Any Questions?


Download ppt "Homework #3: Classes and Constructors"

Similar presentations


Ads by Google