Programming Languages Homework Public Members Class Can Directly Accessed The Member Functions Any Derived Class

subject Type Homework Help
subject Pages 14
subject Words 245
subject Authors D. S. Malik

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
81
public members of a class can be directly accessed by the member functions of any derived
class as well as in a program that uses that class.
24. a. setZ and secret
b. getX, getY, mystryNum, and the default constructor.
25. a. class yourClass: protected base
{
}
b. The members setXYZ, setX, getX, setY, getY, mystryNum, and print, z, setZ, and
26. a. class dummyClass: base
{
}
or
27. a. Because the memberAccessSpecifier is not specified, it is a private inheritance.
29. a.
void base::print() const
{
page-pf2
{
59.50
num = 3, x = 1.50, z = 2.00
30.
2 This is the base class
page-pf3
83
Chapter 12
1. a. false; b. false ; c. false; d. true; e. false; f. true; g. false; h. false; i. true; j. false;
k. true; l. true; m. false; n. true; o. true; p. false;
2. a. Valid
b. Valid;
c. Invalid; p3 is a pointer of type double and p2 is a pointer variable of type int. The value of p2
cannot be assigned to p3.
3. a. To create a pointer, in the variable declaration, operator * is placed between the data type and
the variable name. For example the statement int *p; declares p to be a pointer of type int.
5. *numPtr given the address of the memory location to which numPtr points, while &numPtr
gives the address of numPtr.
page-pf4
84
33.8 3.8
10. In the last four cout statements, use the dereferencing operator to access the values of the
radius of the base (*baseRadius) and height (*height). The correct code is:
double *baseRadius;
double *height;
cout << fixed << showpoint << setprecision(2);
11. The correct code is:
double *length;
double *width;
Area: 19.50, Perimeter: 19.00
12. 10 18
13. Trip total cost: $550.00
page-pf5
85
14. In Line 4, &speed gives the address of speed not the address of memory location speed is
pointing to. Replace &speed with *speed. The statement in Line 5, stores 8.5 in the
memory location pointed to by travelTime. However, no such memory location exists. Add
statement to allocate a memory space of type double and store the address of the allocated
16. 54 49
18. 7 9 13 19 27
20. The operator new allocates memory space of a specific type and returns the address of the
23. a. sales = new double[50];
page-pf6
86
c. int maxIndex = 0;
for (int i = 1; i < 50; i++)
if (sales[maxIndex] < sales[i])
maxIndex = i;
d. delete []sales;
24. a. for (int i = 0; i < 4; i++)
cout << strPtr[i] << " ";
cout << endl;
b. delete strPtr;
26. Because p is a dynamic array, the range-based loop in Line 5 cannot be used on p.
28. a. The statement in Line 4 copies the value of myPtr into yourPtr. After this statement
executes, both myPtr and yourPtr point to the same memory space. The statement in Line 6
deallocates the memory space pointed to by yourPtr, which in turn invalidates myPtr.
29. int *myList;
int *yourList;
myList = new int[5];
30. a. int ** votes;
b. votes = new int* [50];
for (int i = 0; i < 50; j++)
page-pf7
87
for (int j = 0; j < 10; j++)
cout << votes[i][j] << " ";
cout << endl;
}
d. for (int i = 0; i < 50; i++)
delete [] votes[i];
delete [] votes;
31. The copy constructor makes a copy of the actual variable.
33. Classes with pointer data members should include the destructor, overload the assignment
34. 5
small: --
35. 5
small: --
36. In compile-time binding, the compiler generates the necessary code to call a function. In run-time binding,
the run-time system generates the necessary code to make the appropriate function call.
38.
public studentType: public personType
{
public:
virtual void print() = 0;
virtual void calculateGPA() = 0;
page-pf8
88
char cG[] = NULL, int noOfC = 0);
private:
long studentId;
string courses[6];
char coursesGrade[6];
int noOfCourses;
}
39. a. Because employeeType is an abstract class, you cannot instantiate an object of this class.
Therefore, this statement is illegal.
page-pf9
Chapter 13
1. a. true; b. false; c. true; d. false; e. false; f. false; g. false; h. false; i. true; j. false;
k. false; l. true; m. true
2. To overload an operator for a class:
4. a. Using the pointer this.
5. A friend function of a class is a nonmember function of the class, but has access to all the
8. a. friend bool before(const dateType&, const dateType&);
b. bool before(const dateType& obj1, const dateType& obj2)
{
if ((obj1.dYear < obj2.dYear) ||
}
page-pfa
90
12. operator+(object1, object2)
13. a. bool b. bool
14. a. friend istream& operator>>(istream&, strange&);
b. const strange& operator=(const strange&);
16. The statement in Line 4, overloads the binary operator * for the class opOverload. Because * is a
17. In Line 4, the formal parameter of the function operator+ should be of type myClass. The correct
statement is:
18. In Line 4, the function operator + is overloaded as member of the class, so it should have only one
parameter. The correct statement is:
19. In Line 3, the return type of the function operator should be bool. The correct statement is:
20. In Line 3, the function operator+ is a friend function of the class, so it cannot be constant.
21. In Line 3 and 10, the return type of the function operator should be findErrors. In Line 3, the type
of the objects a and b must be findErrors. Also since operator* is a friend function of the class, the
name of the class and the scope resolution operator in the heading of the function, in Line 10, are not
page-pfb
91
const findErrors& b) //Line 10
temp.first = a.first * b.first; //Line 13
temp.second = b.second * b.second; //Line 14
25. The function that overloads the pre increment operator has no parameter, while the function that overloads
the post increment operator has one (dummy) parameter.
27. a. None b. One
29.
class complexType
{
//overload the stream insertion and extraction operators
friend ostream& operator<<(ostream&, const complexType&);
friend istream& operator>>(istream&, complexType&);
complexType operator*(const complexType& otherComplex) const;
//overload *
bool operator==(const complexType& otherComplex) const;
page-pfc
92
complexType complexType::operator~() const
{
complexType temp = *this;
temp.imaginaryPart = -temp.imaginaryPart;
return temp;
}
double complexType::operator!() const
{
return (pow((realPart * realPart +
imaginaryPart * imaginaryPart), 0.5));
}
30.
class complexType
{
//overload the stream insertion and extraction operators
friend ostream& operator<<(ostream&, const complexType&);
friend istream& operator>>(istream&, complexType&);
//overload ==
public:
void setComplex(const double& real, const double& imag);
//set the complex number according to the parameters
//Postcondition: realPart = real; imaginaryPart = imag
page-pfd
93
temp.imaginaryPart = - temp.imaginaryPart;
return temp;
}
double operator!(const complexType& cObj)
{
return (pow((cObj.realPart * cObj.realPart +
cObj.imaginaryPart * cObj.imaginaryPart), 0.5));
}
31. When the class has pointer data members.
34. a. strange<int> sObj;
}
37.
template <class Type>
}
38.
a. (i) Function prototypes to appear in the definition of the class:
newString operator+(const newString& right) const;
newString operator+(char ch) const;
private:
page-pfe
94
(ii) Definitions of the functions to overload +
newString newString::operator+(const newString& rightStr)
{
newString temp = *this;
strConcat(temp.strPtr, rightStr.strPtr);
temp.strLength = strlen(temp.strPtr);
return temp;
}
newString newString::operator+(char ch)
{
newString temp = *this;
char *temp1 = strPtr;
char chStr[2];
chStr[0] = ch;
chStr[1] = '\0';
str[i] = strPtr[i];
for (int i = len1; i < len; i++)
str[i] = str2[i - len1];
str[len] = '\0';
return str;
}
page-pff
95
delete [] str1;
str1 = new char[len1 + len2 + 1];
for (int i = 0; i < len1; i++)
str1[i] = temp[i];
for (int i = len1; i < len1 + len2; i++)
str1[i] = str2[i - len1];
str1[len1 + len2] = '\0';
return str1;
}
{
char chStr[2];
chStr[0] = ch;
chStr[1] = '\0';
}
39. These statements generate and output a random integer between 10 and 25.
page-pf10
Chapter 14
2. The statements that may generate an exception are placed in a try block. The try block also
4. If no exception is thrown in a try block, all catch blocks associated with that try block are
10. The catch block has no associated try block, that is, the catch block does not follow any try
block. Also, the try block has no associated catch block, that is, there is no catch block that
follows the try block. The correct code is:
double balance = 25000;
page-pf11
97
11. The cout statement in Line 12 separates the catch block from the try block. Therefore, the
catch block has no associated try block and the try block has no associated catch block. The
catch block in Line 13 has no parameters. The correct code is:
double salary = 78000; //Line 1
double raise; //Line 2
b. There are two catch blocks in the code. The first catch block between 18 and 22; the second
c. The parameter of the catch block at line 18 is num and it is of type int. The parameter of the
d. This code contains the following throw statements:
page-pf12
98
13. (In the following, the user input is shaded.)
b. Enter the number of items: -55
c. Enter the number of items: 37
d. Enter the number of items: -10
14. a. Entering the try block.
Exception: Lower limit violation.
15. a. 55
Exiting the try block.
16. class exception
17. a. class out_of_range
19. A throw statement.
20. One possible answer is:
page-pf13
class tornadoException
{
}
message = "Tornado: " + str + " miles away; and approaching!";
}
private:
string message;
};
private:
string message;
};
Definition of the member functions:
page-pf14
tornadoException::tornadoException(int m)
{
string str = "";
int rem;
while (m != 0)
{
rem = m % 10;
str = static_cast<char>(rem + static_cast<int> ('0')) + str;
m = m / 10;
}
message = "Tornado: " + str + " miles away; and approaching!";
}
tornadoException::string what()
{
return message;
}
21. (Assume that the definition of the class tornadoException is in the header file
tornadoException.h.)
#include <iostream>
#include "tornadoException.h"
}
22. If the exception is thrown with the default constructor, the output is:

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.