Sections 0201 - 0204 (Mr. Raouf) Monday, March 10, 2003
Use the list of questions below as a guide when studying for Exam 1.
It is by no means a comprehensive list of questions.
You are responsible for all material presented in lecture up through and
including copy constructors and assignment operator.
You are also responsible for all associated reading assignments and material
covered in discussion/lab.
A wise student won't forget the suggested exercises.
General C++ Coding
- Write C++ code to output the sentence
"I am the Greatest" to the standard output device,
one word per line.
- Write a single line of code to instantiate an ifstream
object named infile, associate with a file named "myfile.txt"
that contains integers separated by whitespace, and open the file.
- Assuming that infile was successfully opened in the question
above, write the declarations and code needed to read three (3) integers
from the file and insert them into the first 3 elements of a vector
named IntVector.
- Again assuming that infile from above was opened successfully, write
the while loop control structure that reads all the integers from the file one at a
time until end-of-file.
- Explain the differences between the function prototypes below
void GetFirstCharacter ( string s);
void GetFirstCharacter ( string& s);
void GetFirstCharacter ( const string& s);
- Why is function overloading possible in C++, but not in C?
- The concepts of constructor, destructor may be confusing
because they are member functions of a class which are never explictly called.
Explain when a constructor is called. Explain when a destructor is called.
- Define PreConditon, PostCondition as used in function header
comments required by our coding standards.
- Explain three ways in which functions might handle unstatisfied PreConditions.
- Define cout, cerr and cin
Strings and Vectors
- Given the declaration
string aString = "the quick brown fox jumped over the lazy dog";
write syntactically correct C++ code to
- Output the number of characters in aString to the standard output stream
- Output the string backwards to the standard output stream
- Instantiate a string object named anotherString as a copy
of aString
- Write the function
bool FindSubString(const string& string1, const string& string2);
that returns true if string2 is a substring of string1; false otherwise.
Write this function without the use of any string "find" methods.
- Give at least three ways in which strings are superior to C-style
char arrays used as strings.
- Write the variable declaration for a vector of strings named text
- Explain the difference between these two declarations
vector< int > v1( 10 );
vector< int > v2[10];
- In what ways are vectors like arrays?
- In what ways are vectors different from arrays?
- Given the declaration vector< int > vInt;
write C++ code that will store 2, 4, 6, 8, 10
in the vector in that order and then print the contents
of the vector.
Object-Oriented Programming
- Explain the difference between a class and an object
Use "real life" entity as an example.
The following 10 questions refer to the following class definition
class Car
{
public:
Car( int cylinders = 4, int passengers = 5,
const string& color = "Red");
~Car ( void );
void Start( void );
void Stop( void );
void TurnLeft( void );
void TurnRight ( void );
const string& GetColor ( void ) const;
int GetCylinders( void ) const;
int GetNrPassangers( void ) const;
void SetColor (const string & color);
void SetCylinders (int cylinders = 4);
void SetNrPassageners ( int passengers);
private:
int m_cylynders;
int m_passengers;
string m_color;
};
- Write the code to implement Car's constructor.
- In how many ways may Car's constructor be invoked? List them.
- Write the code to implement Car's SetNrPassengers()
method.
- Write the code to implement Car's SetNrPassengers()
method using the pointer known as "this".
- What is the significance of const in the
prototype for GetCylinders()
- What is the significance of const string&
as the return type in the protoype of GetColor().
- Write the declarations and code to instantiate a Car object for
a Black, 6-cylinder car that can hold two people and then start the Car.
- What is the significance of int cylinders = 4
as the parameter for SetCylinders()
- Under what circumstances can a user of Car directly access m_color?
- Identify the use of "aggregation" in the definition of Car (if any).
- Define: member access specifier, object, instance, instantiation,
class scope, user, implementer, mutator, accessor, function signature
- What is the purpose of the variable this?
- Explain how the C++ features of private data members,
public methods, and public data members help or hinder the
OO objectives of encapsulation and data hiding.
- Explain the differences among the procedural, modular,
and object-oriented abstraction models.
- Define ADT (abstract data type). In particular, tell what and ADT is and
what it is not.
- You have been asked to write a set of four functions that find the smallest
element in a vector of ints, a vector of doubles, a vector of chars
and a vector of strings. You decide to use function overloading.
Write the prototypes for these four overloaded functions.
- Can a const object be passed to non-const member function? If so why?
If not, why not?
- Can a non-const object be passed to non-const member function? If so why?
If not, why not?
- Can a const object be passed as an argument by reference to a function?
If so why? If not, why not?
- Can a non-const object be passed as an argument by reference to a function?
If so why? If not, why not?
- What is the significance of a function being declared a friend
of a class?
- What is the significance of a class being declared a friend
of another class?
- Given the following class definitions
class Point class Circle
{ {
public: public:
Point( int x = 0, int y = 0); Circle (const Point& p, int radius);
int GetX ( void ) const; const Point& GetCenter( void ) const;
int GetY ( void ) const; int GetRadius( void ) const;
void Move (int deltaX, int deltaY); void Move (int deltaX, int deltaY);
private: private:
int m_xcoordinate; Point m_center;
int m_ycoordinate; int m_radius;
}; };
and the declarations
Point point(3, 6);
Circle circle1 (point, 5);
Circle circle2 (Point (1, 2), 3);
- Write the implementation of Circle's Move() method.
- Write the implementation of Circle's GetCenter() method.
- Write one line of code to output the x- and y- coordinates of
circle2's x- and y- coordinates to cout.
- Write one line of code to make circle2
and circle1 concentric (i.e. have the same center).
- Find the errrors in the following statements (if any)
- Point p = (1, 3);
- Point p2;
- Circle c;
- cout << circle1.GetX() << endl;
- circle1 = circle2;
Operator Overloading
Use the class definition of Fraction for the questions below
class Fraction
{
public:
Fraction (int numerator = 1, int denominator = 1);
int GetNumerator( void ) const;
int GetDenominator( void ) const;
// other public methods???
private:
int m_numerator;
int m_denominator;
};
- Write the prototype for operator<< for Fraction.
- Write the code for operator<< assuming operator<<
IS NOT a friend of Fraction.
- Write the code for operator<< assuming operator<<
IS a friend of Fraction.
- Explain why operator<< CANNOT be a member function of Fraction.
- Write the prototype for operator+ that adds two Fractions and returns
a Fraction as a result, if operator+ is a member function of Fraction.
- Write the prototype for operator+ that adds two Fractions and returns
a Fraction as a result, if operator+ is NOT a member function of Fraction.
- Which version of operator+ is most useful? Explain your answer.
- Write the prototype for the unary negation operator (operator-) for Fraction.
- Write the prototype for the increment operator of class Fraction
if is declared as a friend function.
Dynamic memory allocation
- Define "memory leak".
- Given the following declarations and code, draw a picture of
memory that shows the relationships among the variables
after the code has been executed.
modified exercise from lecture notes
- Given the following statements that allocate dynamic
memory, write the corresponding statements to free the memory.
- string *p = new string;
- string *p = new string[3];
- Time *p = new Time(12, 0, 0);
- int *p = new int(33);
- int *p = new int[33];
- Identify and explain how to fix any errors in the following
code. Assume the proper #includes and namespace std.
int main ( )
{
int *p1 = new int (5);
int *p2 = p1;
cout << "p1 is pointing to the value " << *p2 << endl;
delete [] p1;
delete p2;
return 0;
- Explain what happens in the following new and delete
expressions
Car *parkingLot = new Car[50];
delete [] parkingLot;
Copy Constructor and Assignment Operator
- Why must the parameter to the copy constructor be
passed by (const) reference?
- Write the prototype for the copy constructor
of a class named AB123YZ.
- Given the declaration string bob = "bobby"; which of the
following invoke the copy constructor?
- string mary = bob;
- string mary( bob );
- string mary = "bob";
- Under what condition(s) will the default copy constructor supplied
by the compiler lead to memory leaks or runtime errors?
- What is the purpose of the test if (this != &rhs)
in the code for operator= (where rhs is the parameter)?
- Why does the assignment operator always return *this?
- Define deep copy, shallow copy
- Write a copy constructor for the Car class above.
- Write an assignment operator for the Car class above.