• Home
  • Textbooks
  • C++ Programming: Program Design Including Data Structures
  • CLASSES AND DATA ABSTRACTION

C++ Programming: Program Design Including Data Structures

D. S. Malik

Chapter 10

CLASSES AND DATA ABSTRACTION - all with Video Answers

Educators


Chapter Questions

01:58

Problem 1

Mark the following statements as true or false.
a. The member variables of a class must be of the same type. (1)
b. The member functions of a class must be public. (2)
c. A class can have more than one constructor. (5)
d. A class can have more than one destructor. (5)
e. Both constructors and destructors can have parameters. (5)

Crystal Wang
Crystal Wang
Numerade Educator

Problem 2

Find the syntax errors in the following class definition. $(1,2,5)$
class syntaxErrors 1 //Line 1
\{ //Line 2
public: //Line 3
syntaxErrors (); //Line 4
void setData(double, double); //Line 5
int mult(); //Line 6
private: $\quad / /$ Line 7
int one; //Line 8
double two; $\quad / /$ Line 9
\}
//Line 10

Check back soon!

Problem 3

Find the syntax errors in the following class definition. $(1,2,5)$
class syntaxErrors $2 \quad / /$ Line 1
$\{$ //Line 2
public: //Line 3

Check back soon!

Problem 4

Find the syntax errors in the following class definition. $(1,2,5)$
class syntaxErrors 3()$\quad / /$ Line 1
\{
//Line 2
public:
//Line 3
void setXy (int, int); //Line 4
iskqual (int a, int b); //Line 5
int multiply(); //Line 6
void print() const; //Line 7
syntaxErrors 3 (int, int $=0$ ); $/ /$ Line 8
private: $/ /$ Line 9
int $\mathbf{x}$ //Line 10
int $\mathrm{y} ; \quad$ //Line 11
\}; //Line 12

Check back soon!

Problem 5

Find the syntax errors in the following class definition. $(1,2,5)$
class syntaxErrors 4
\{
//Line 1
//Line 2
$/ /$ Line 3
$/ /$ Line 4
$/ /$ Line 5
$/ /$ Line 6
$/ /$ Line 7
//Line 8
$/ /$ Line 9
$/ /$ Line 10
//Line 11
//Line 12
\}

Check back soon!

Problem 6

Consider the following declarations: $(1,2,5,7)$
class foodType
\{
public:
void set (string, int, double, int, double, double);
void print() const;
string getName() const;
int getCalories() const;
double getFat () const;
int getsugar() const;
double getCarbohydrate () const;
double getPotassium() const;
foodType () ;
foodType (string, int, double, int, double, double);

Check back soon!

Problem 7

Assume the definition of class foodType as given in Exercise 6. Answer the following questions? $(1,2,3,5,6)$
a. Write the definition of the member function set so that private members are set according to the parameters. The values of the int and double instance variables must be nonnegative.
b. Write the definition of the member function print that prints the values of the data members.
c. Write the definitions of the member functions getName, getCalories, getFat, getSugar, getCarbohydrate, getPotassium to return the values of the instance variable.
d. Write the definition of the default constructor of the class foodType so that the private member variables are initialized to $" n, 0,0.0,0,0.0,0.0$, respectively.
e. Write the definition of the constructor with parameters of the class foodType so that the private member variables are initialized according to the parameters. The values of the int and double instance variables must be nonnegative.
f. Write a ++ statement that prints the value of the object fruit2.

Check back soon!

Problem 8

\begin{aligned}
&\text { Consider the definition of the following class: }(1,2,3,5,7)\

Check back soon!

Problem 9

. Consider the definition of the class product Type as given in Exercise 8. Which function members are accessors and which are mutators? (4)

Check back soon!

Problem 10

Consider the definition of the class product Type as given in Exercise 8 . Answer the following questions. (1, 2, 3, 5, 7)
a. Write the definition of the function set so that instance variables are set according to the paramaters. Instance variables quantitiesinstock, price, and discount must be nonnegative.
b. Write the definition of the function print to output the values of the instance variables.
c. Write the definition of the function setQuantitiesInstock to set the value of the instance variable quantitiesInstock according to the parameter.
d. Write the definition of the function updateQuantitiesInstock to update the value of instance variable quantitiesInstock by adding the value of the parameter.
e. Write the definition of the function getQuantitiesInstock to return the value of instance variable quantitiesInstock.
f. Write the definition of the function setPrice to set the value of the instance variable price according to the parameter.
g. Write the definition of the function getPrice to return the value of the instance variable price.
h. Write the definition of the function setDiscount to set the value of the instance variable discount according to the parameter.
i. Write the definition of the function getDiscount to return the value of the instance variable discount.

Check back soon!
02:59

Problem 11

Consider the following declarations: $(1,2,3,6,7)$
class houseType
\{
public:
void set(string, int, int, int, int, int, double, double);
void print() const;
vold setstyle (string);
string getstyle() const;
vold setNumofBedrooms (int);
int getNumofBedrooms() const;
void setNumofBathrooms (int) ;
int getNumofBathrooms () const;
void setNumofCarsGarage (int);
int getNumofCarsGarage () const;
void setYearBuilt (int);
int getYearBuilt() const;
void setFinishedsquareFootage (int);
int getFinishedsquareFootage () const;
void setprice (double);
double getPrice() const;
vold setTax (double);
double getTax () const;
houseType (string $=" n$, int $=0$, int $=0$, int $=0$, int $=0$,
int $=0$, double $=0$, double $=0$ );
private:
string style;
int numofBedrooms;
int numofBathrooms ;
int numofCarsGarage ;
int yearBuilt;
int finishedsquareFootage;
double price;
double tax;

Ernest Castorena
Ernest Castorena
Numerade Educator

Problem 12

Assume the definition of class houseType as given in Exercise 11. Answer the following questions. $(1,2,3,5,7)$
a. Write the definition of the member function set so that private members are set according to the parameters.
b. Write the definition of the member function print that prints the values of the data members.
yright 2018 Cengage Learning. All Rights Reserved. May not be copled, scanned, or duplicated, in whole or in part. WCN $02-300$
| Chapter 10: Classes and Data Abstraction
c. Write the definition of the constructor of the class houseType so that the private member variables are initialized to according to the parameters.
d. Write a $\mathrm{C}++$ statement that prints the value of the object newHouse.
e. Write a $\mathrm{C}++$ statement that declares an object house of type newHouse, and initializes the member variables of house to "Ranch", 3, 2, 2, 2005, 1300, 185000, and 3600.0, respectively.
f. Which function members are accessors and which are mutators?

Check back soon!

Problem 13

Consider the following statements: $(1,2,3,5,7)$
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 $=n n$, 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 without specifying any parameters.

Check back soon!
30:42

Problem 14

Assume the definition of class temporary as given in Exercise 13. Answer the following questions. (1, 2, 3, 5, 7)
a. Write the definition of the member function set so that the instance variables are initialized according to the parameters.
b. Write the definition of the member function manipulate that returns a decimal number as follows: If the value of description is "rectangle", it returns first * second; if the value of description is "circle", it returns the area of the circle with radius first; if the value of description is "sphere", it returns the volume of the sphere with radius first; if the value of description is "cylinder", it returns the volume of the cylinder with radius first and height second; otherwise it returns the value - 1 .
c. Write the definition of the function print to print the values of instance variables and the values returned by the function manipulate. For example, if description = "rectangle", first $=8.5$, and second $=5$, it should print:
$$
\text { rectangle: length }=8.50 \text {, width }=5.00 \text {, area }=42.50
$$
d. Write the definition of the constructor so that it initializes the instance variables using the function set.
e. Write the definition of the remaining functions to set or retrieve the values of instance variables. Note that the function get returns the values of all instance variables.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator

Problem 15

Assume the definition of class temporary as given in Exercise 13 . What is the effect of the following statements? (5)
temporary object1; //Line 1
temporary object2 ("rectangle", 3.0, 5.0); //Line 2
temporary object3 ("circle", 6.5, 0.0); //Line 3
temporary object4 ("cylinder", 6.0, 3.5); //Line 4

Check back soon!

Problem 16

Assume the definition of class temporary as given in Exercise 13 and the definitions of the member functions and the constructor as specified in Exercise 14. What is the output of the following statements? $(3,5)$
temporary object 1 ;
temporary object2 ("rectangle", 8.5, 5);
temporary object3 ("circle", 6, 0);
temporary object 4 ("cylinder", 6, 3.5);
cout $\ll$ fixed $\ll$ showpoint $\ll$ setprecision (2);
object1.print();
object 2 .print();

Check back soon!
01:21

Problem 17

What are the built-in operations on classes? (3)

Adam Conner
Adam Conner
Numerade Educator

Problem 18

What is the main difference between a struct and a class? (8)

Check back soon!

Problem 19

Given the definition of the class clockType with constructors (as described in this chapter), what is the output of the following $\mathrm{C}++$ code? $(1,2,3,5,7)$
clockType myClock (10, 16, 59);
clockType yourclock;
myClock. incrementSeconds ();
myclock.printTime ();
cout $\ll$ endl;
yourclock.setTime (23, 59, 29);
yourclock.printTime ();
cout $\ll$ endl ;
yourClock.incrementMinutes ();
yourclock.printTime ();
cout $\ll$ endl;

Check back soon!

Problem 20

Consider the definition of the class integermanipulation given in Example 10-11. What is the output of the following $\mathrm{C}++\operatorname{code}$ ? $(1,2$, $3,5,7$ )
integerManipulation number(3510862895423079232);
number.classifyDigits();
cout $\ll$ number.getNum() $\ll$ ": The number of digits--.-"
$\ll$ endl;
cout $\ll$ " Bven: " « number.getEvensCount () « endl
$\ll$ " Zeros: " « number.getzerosCount () « endl
$\ll$ " Odd: " $\ll$ number.getoddsCount () « endl;

Check back soon!

Problem 21

Assume the definition of the class personType as given in this chapter. $(1,3)$
a. Write a C++ statement that declares student to be a personType object, and initialize its first name to "Buddy" and last name to "Arora".
b. Write a C++ statement that outputs the data stored in the object student.
c. Write a C++ statement that changes the first name of student to "Susan" and the last name to "Gilbert".

Check back soon!
01:18

Problem 22

Explain why you would need both public and private members in a class. $(2,9,10)$

Jennifer Stoner
Jennifer Stoner
Numerade Educator

Problem 23

2 What is a constructor? Why would you include a constructor in a class? (5)

Check back soon!
00:51

Problem 24

Which of the following characters appears before a desutructor's name? (5)
a. \#
b. 1
c.
d. $\$$

Ernest Castorena
Ernest Castorena
Numerade Educator

Problem 25

. What is a destructor and what is its purpose? (5)

Check back soon!
27:29

Problem 26

Write the definition of a class that has the following properties: $(1,2,3,5)$
a. The name of the class is stockType.
b. The class stockType has the following instance variables: name of type string, symbol of type string; currentPrice, lowPriceof TheDay, highPriceof TheDay, previousDayclosingPrice, fiftyTwoweeksHigh, and fiftyTwoweeksLow are of type double.
c. The class stockType has the following member functions. (Make each accessor function constant.)
print-outputs the data stored in the member variables with the appropriate titles
setStockName-function to set the stock name
getName-value-returning function to return the stock name
setSymbol-function to set the stock symbol
getsymbol-value-returning function to return the stock symbol
setCurrentPrice-function to set the current price of the stock
getCurrentPrice-value-returning function to return stock's current price
setLowPriceofTheDay-function to set stock's lowest price of the day getLowPriceofTheDay-value-returning function to return stock's lowest price of the day
setHighPriceofTheDay-function to set the stock's highest price of the day
getHighPriceof TheDay-value-returning function to return stock's highest price of the day
setPreviousDayclosingPrice-function to set the stock's previous day closing price
getPreviousDayclosingPrice-value-returning function to return stock's closing price of the previous day
setFiftyTwoWeeksHigh-function to set the stock's fifty two weeks highest price

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator

Problem 27

How does a compiler treat an inline member function of a class? (11)

Check back soon!

Problem 28

Consider the definition of the following class: $(1,2,3,5,7,11)$
class myclass \{

Check back soon!

Problem 29

Consider the following definition of the class myClass : (12)
class myclass
\{
public:
void setx (int a);
$/ /$ Function to set the value of $x$.
//Postcondition: $x=a$;
void printx() const;
//Function to output $x$.
static void printcount();
$/ /$ Function to output count.
static void incrementCount();
$/ /$ Function to increment count.
//Postcondition: count++;
myclass (int $\mathbf{a}=0)$;
$/ /$ constructor with default parameters
//Postcondition $\mathrm{x}=\mathrm{a}$;
$/ /$ If no value is specified for $a, x=0$;

Check back soon!
02:08

Problem 30

In Example 10-9, we designed the class die. Using this class declare an array, named rolls, of 100 components of type die. Write $\mathrm{C}++$ statements to roll each die of the array rolls, find and output the highest number rolled and the number of times this number was rolled, and find and output the number that was rolled maximum number of times together with its count. Also write a program to test your statements. $(1,3)$

James Kiss
James Kiss
Numerade Educator