Problem 1
Mark the following statements as true or false. a. The member variables of a class must be of the same type. b. The member functions of a class must be public. c. A class can have more than one constructor. d. A class can have more than one destructor. e. Both constructors and destructors can have parameters.
Problem 2
Find the syntax errors in the definitions of the following classes. a. class AA { public: void print()const; int sum(); AA(); int AA(int, int); private: int x; int y; }; b. class BB { int one; int two; public: bool equal() const; print(); BB(int, int); } c. class CC { public; void set(int, int); void print()const; CC(); CC(int, int); bool CC(int, int); private: int u; int v; };
Problem 3
Consider the following declarations: class xClass { public: void func(); void print() const ; xClass (); xClass (int, double); private: int u; double w; }; and assume that the following statement is in a user program: xClass x; a. How many members does class xClass have? b. How many private members does class xClass have? c. How many constructors does class xClass have? d. Write the definition of the member function func so that u is set to 10 and w is set to 15.3. e. Write the definition of the member function print that prints the contents of u and w. f. Write the definition of the default constructor of the class xClass so that the private member variables are initialized to 0. g. Write a C++ statement that prints the values of the member variables of the object x. h. Write a C++ statement that declares an object t of type xClass and initializes the member variables of t to 20 and 35.0, respectively.
Problem 4
Consider the definition of the following class: class CC { public : CC(); //Line 1 CC(int); //Line 2 CC(int, int); //Line 3 CC(double, int); //Line 4 . . . private: int u; double v; }; a. Give the line number containing the constructor that is executed in each of the following declarations. i. CC one; ii. CC two(5, 6); iii. CC three(3.5, 8); b. Write the definition of the constructor in Line 1 so that the private member variables are initialized to 0. c. Write the definition of the constructor in Line 2 so that the private member variable u is initialized according to the value of the parameter, and the private member variable v is initialized to 0. d. Write the definition of the constructors in Lines 3 and 4 so that the private member variables are initialized according to the values of the parameters.
Problem 5
Consider the definition of the following class: class testClass { public: int sum(); //Returns the sum of the private member variables void print() const; //Prints the values of the private member variables testClass(); //Default constructor //Initializes the private member variables to 0 testClass(int a, int b); //Constructors with parameters //initializes the private member variables to the values //specified by the parameters //Postcondition: x = a; y = b; private: int x; int y; }; a. Write the definitions of the member functions as described in the definition of the class testClass. b. Write a test program to test the various operations of the class testClass.
Problem 8
Explain why you would need both public and private members in a class.
Problem 9
What is a constructor? Why would you include a constructor in a class.?
Problem 10
b. ! c. ~ d.
Problem 11
What is a destructor and what is its purpose?
Problem 12
Write the definition of a class that has the following properties: a. The name of the class is secretType. b. The class secretType has four member variables: name of type string, age and weight of type int, and height of type double. c. The class secretType has the following member functions. (Make each accessor function constant.) print—outputs the data stored in the member variables with the appropriate titles setName—function to set the name setAge—function to set the age setWeight—function to set the weight setHeight—function to set the height getName—value-returning function to return the name getAge—value-returning function to return the age getWeight—value-returning function to return the weight getHeight—value-returning function to return the height constructor—with default parameters: The default value of name is the empty string " ", and the default values of age, weight, and height are 0. d. Write the definition of the member functions of the class secretType, as described in Part c.