Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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 following class definition: class mystery //Line 1 { //Line 2 public: //Line 3 void print() const; //Line 4 void setNum(double, double); //Line 5 int power(); //Line 6 double mystery(); //Line 7 double mystery(double, double); //Line 8 private: //Line 9 double x; //Line 10 double y; //Line 11 };

Problem 3

Find the syntax errors in the following class definition: class secret //Line 1 { //Line 2 public: //Line 3 bool multiply(); //Line 4 print() const; //Line 5 secret(int = 0, int = 0); //Line 6 private: //Line 7 int one; //Line 8 int two; //Line 9 }; //Line 10 B

Problem 4

Find the syntax errors in the following class definition: class secret //Line 1 { //Line 2 public: //Line 3 bool compare(); //Line 4 void print() const; //Line 5 secret(int = 0, int = 0) const; //Line 6 private: //Line 7 string str; //Line 8 int one; //Line 9 int two; //Line 10 }; //Line 11

Problem 5

Find the syntax errors in the following class definition: class discover //Line 1 { //Line 2 public; //Line 3 void set(string, int, int); //Line 4 void print() const; //Line 5 discover(); //Line 6 discover(string, int, int); //Line 7 bool discover(string, int, int); //Line 8 private: //Line 9 string type; //Line 10 int l; //Line 11 int w; //Line 12 } //Line 13

Problem 6

Consider the following declarations: class bagType { public: void set(string, double, double, double, double); void print() const; string getStyle() const; double getPrice() const; void get(double, double, double, double); bagType(); bagType(string, double, double, double, double); private: string style; double l; double w; double h; double price; }; bagType newBag; //variable declaration a. How many members does class bagType have? b. How many private members does class bagType have? c. How many constructors does class bagType have? d. How many constant functions does class bagType have? e. Which constructor is used to initialize the object newBag?

Problem 8

Consider the definition of the following class: class employee //Line 1 { //Line 2 public: //Line 3 employee(); //Line 4 employee(string, int, double); //Line 5 employee(int, double); //Line 6 employee(string); //Line 7 void setData(string, int, double); //Line 8 void print() const; //Line 9 void updatePay(double x); //Line 10 int getNumOfServiceYears() const; //Line 11 double getPay() const; //Line 12 private: //Line 13 string name; //Line 14 int numOfServiceYears; //Line 15 double pay; //Line 16 }; //Line 17 a. Give the line number containing the constructor that is executed in each of the following declarations: i. employee tempEmployee; ii. employee newEmployee("Harry Miller", 0, 25000); iii. employee oldEmployee("Bill Dunbar", 15, 55000); b. Write the definition of the constructor in Line 4 so that the instance variables are initialized to "", 0, and 0.0, respectively. c. Write the definition of the constructor in Line 5 so that the instance variables are initialized according to the parameters. d. Write the definition of the constructor in Line 6 so that the instance variable name is initialized to the empty string and the remaining instance variables are initialized according to the parameters.

Problem 11

Consider the following statements: class temporary { public: void set(string, double, double); void print(); double manipulate(); void get(string&, double&, double&); void setDescription(string); void setFirst(double); void setSecond(double); string getDescription() const; double getFirst()const; double getSecond()const; temporary(string = "", double = 0.0, double = 0.0); private: string description; double first; double second; }; a. How many members does class temporary have? b. How many private members does class temporary have? c. How many constructors does class temporary have? Can this constructor be used to initialize an object without specifying any parameters? If yes, then illustrate with an example; otherwise, explain why it cannot be used to initialize an object witout specifying any parameters.

Problem 16

What is the main difference between a struct and a class?

Problem 20

Explain why you would need both public and private members in a class.

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks