Discussion:
Review Questions
(too old to reply)
Jeff Ford
2005-05-04 15:02:08 UTC
Permalink
1. Write the declaration of class B that publicly inherits from class B.
You don't have to include any of the new member functions or data members.

2. What does it mean when class B declares that class A is a friend, but
class A does not declare that class B is a friend?

3. How do you initialize the data member numEmployees in the class which
follows?

public class Employee {
public:
// constructors and member functions not shown
private:
string name;
long idNumber;
static long numEmployees;
};

4. Write a constructor for a class called AlarmClock that takes four integer
arguments, the first two of which it passes to the constructor for Clock
(which AlarmClock inherits from), and the latter two of which it initializes
data members alarmHour and alarmMinute.

5. Suppose class B inherits publicly from class A and they both define a
print() member function which takes no arguments. What keyword must be
used in the declaration of print() in class A for pointer-based calls to
print() to be associated with a version of print() at run-time.

6. Write the declaration of an abstract class.

7. Why should destructors be virtual?

8. Which version of casting is used to treat a pointer as a pointer to
a derived class?

9. What is the return type of the member function get() of the istream
class that takes no parameters?

10. Write a declaration of a buffer of 100 characters and then a function
call that reads 100 characters from ifstream "in" without regards to
whitespace.

11. The setfill() manipulator sets the character that is used to pad space
when an output is put in a larger space than it needs. Write a function
that takes a vector of integers as a parameter and outputs each integer
in a space of width 10, using *'s to fill the empty space.

12. What is output by the code below?

ifstream infile("test.txt");
int x;
while (!infile.eof()) {
infile >> x;
cout << x << " ";
}

test.txt:
14 29 100
23
-7

13. Write a templated function that takes an ostream and an object and
which writes the object to the ostream in binary format.

14. Write a function call that places the get marker 5 characters before the
end of istream "in".

15. Which data types cannot be thrown as exceptions?

16. What is the difference between a function which includes throw () after
its parameter list and one which doesn't have a throw list at all?

17. Write a try/catch block that calls function f() with no parameters
and catches type "exception" and outputs the associated message if f()
throws type "exception". It should also output "Error" if f() throws
any type other than exception.

18. If s is a string, what is the difference between s[10] and s.at(10)?

19. The subtr() member function takes two parameters, an index to start a
substring and a substring length. If s is the string "applesauce", write
a call to substr() which returns "ples".

20. What is the difference between ("a" + "b") and 'a' + 'b'))?

21. Write a templated stack class. It should have a vector data member and
member functions empty() which checks if the stack is empty, push() which
adds an element to the stack, and pop() which removes an element from the
stack.

22. Given the above class, write the declaration of a stack of strings
called stringStack.

23. Suppose for a vector v that v.end() - v.begin() = 17. How many items
are in the vector?

24. The sort() function in <algorithm> takes two iterators as parameters
and sorts the elements between them. Write a call to sort that sorts
the following array:

int myArray[] = {17, 123, 143, 12, 10, 10, -4, 34, 23, 30};

25. Write a loop which will output all of the items of a set<int> s.
--
Jeff Ford http://www.cs.utexas.edu/users/jeffford/
u***@domain.invalid
2005-05-10 17:39:42 UTC
Permalink
Is one of the 'B's in question 1 supposed to be an 'A'?

Zac
Post by Jeff Ford
1. Write the declaration of class B that publicly inherits from class B.
You don't have to include any of the new member functions or data members.
2. What does it mean when class B declares that class A is a friend, but
class A does not declare that class B is a friend?
3. How do you initialize the data member numEmployees in the class which
follows?
public class Employee {
// constructors and member functions not shown
string name;
long idNumber;
static long numEmployees;
};
4. Write a constructor for a class called AlarmClock that takes four integer
arguments, the first two of which it passes to the constructor for Clock
(which AlarmClock inherits from), and the latter two of which it initializes
data members alarmHour and alarmMinute.
5. Suppose class B inherits publicly from class A and they both define a
print() member function which takes no arguments. What keyword must be
used in the declaration of print() in class A for pointer-based calls to
print() to be associated with a version of print() at run-time.
6. Write the declaration of an abstract class.
7. Why should destructors be virtual?
8. Which version of casting is used to treat a pointer as a pointer to
a derived class?
9. What is the return type of the member function get() of the istream
class that takes no parameters?
10. Write a declaration of a buffer of 100 characters and then a function
call that reads 100 characters from ifstream "in" without regards to
whitespace.
11. The setfill() manipulator sets the character that is used to pad space
when an output is put in a larger space than it needs. Write a function
that takes a vector of integers as a parameter and outputs each integer
in a space of width 10, using *'s to fill the empty space.
12. What is output by the code below?
ifstream infile("test.txt");
int x;
while (!infile.eof()) {
infile >> x;
cout << x << " ";
}
14 29 100
23
-7
13. Write a templated function that takes an ostream and an object and
which writes the object to the ostream in binary format.
14. Write a function call that places the get marker 5 characters before the
end of istream "in".
15. Which data types cannot be thrown as exceptions?
16. What is the difference between a function which includes throw () after
its parameter list and one which doesn't have a throw list at all?
17. Write a try/catch block that calls function f() with no parameters
and catches type "exception" and outputs the associated message if f()
throws type "exception". It should also output "Error" if f() throws
any type other than exception.
18. If s is a string, what is the difference between s[10] and s.at(10)?
19. The subtr() member function takes two parameters, an index to start a
substring and a substring length. If s is the string "applesauce", write
a call to substr() which returns "ples".
20. What is the difference between ("a" + "b") and 'a' + 'b'))?
21. Write a templated stack class. It should have a vector data member and
member functions empty() which checks if the stack is empty, push() which
adds an element to the stack, and pop() which removes an element from the
stack.
22. Given the above class, write the declaration of a stack of strings
called stringStack.
23. Suppose for a vector v that v.end() - v.begin() = 17. How many items
are in the vector?
24. The sort() function in <algorithm> takes two iterators as parameters
and sorts the elements between them. Write a call to sort that sorts
int myArray[] = {17, 123, 143, 12, 10, 10, -4, 34, 23, 30};
25. Write a loop which will output all of the items of a set<int> s.
Jeff Ford
2005-05-11 20:32:53 UTC
Permalink
Post by u***@domain.invalid
Is one of the 'B's in question 1 supposed to be an 'A'?
Yes.
Post by u***@domain.invalid
Post by Jeff Ford
1. Write the declaration of class B that publicly inherits from class B.
You don't have to include any of the new member functions or data members.
Jeff
--
Jeff Ford http://www.cs.utexas.edu/users/jeffford/
Loading...