CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure that you review all previous exams and make sure you fully understand everything on each of those exams. Then review your lecture notes for the course, and finally work through as many of these topics as you feel is necessary. This is not a comprehensive list of terms and examples, just what I had time to put together for you to use as a study guide. Important Terminology struct & class and the differences between the two class & object and the differences between the two public vs private vs protected pointer & reference variable and the differences between the two object-oriented programming operator overloading friend implicit operand & explicit operand runtime stack, heap, activation record and how they are used constructor, destructor, copy constructor this, *this static variables inheritance polymorphism abstract class derived class concrete class superclass and subclass virtual function vs a pure virtual function (how are they different? when would you use one over the other?)
Arrays a) Initializing int values[] = 5, 4, 3, 2, 1 int i = 2; b) Accessing cout << values[i]++; cout << values[i++]; cout << values[++i]; c) As arguments void swap (int &i, int &j) int t = i; i = j; j = t; swap (values[2], values[3]); swap (values[4], values[4]); d) Array manipulation - finding largest value, smallest value, exchanging elements, inserting a value into an array in an arbitrary location. Array manipulation can be code segments or as functions and may require pointer notation. e) Parallel arrays - copying one array to another, are two arrays the same, append one array on the end of another, storing related information into parallel arrays, searching and returning data in parallel arrays f) Two-dimensional arrays traversing a 2D array by rows or by columns, passing 2D arrays to functions, average values in a row or a column or down the diagonal Structures a) Declare struct Point double x, y; b) Define Point spt; c) Access spt.x = 2; d) As argument void printpoint (const Point &spt) cout << spt.x << " " << spt.y; e) Structure Variables - comparing, displaying, initializing, using a constructor with no arguments, using a constructor with arguments, returning a struct from a function g) Arrays of structs - loading an array of structs from a file, searching, inserting into, deleting from
Classes a) Declare class Point private: double x, y; Point (double x, y); // constructor Point (); // default constructor void setx (double); // mutator void sety (double); // mutator void printpt (); // accessor b) Define Point cpt, *pcpt = new Point; c) Access cpt.setx (4.5); d) Defining Member function void Point::setX (double xcoord) x = xcoord; e) Class Variable - comparing, displaying, initializing, using a constructor with no arguments, using a constructor with arguments f) What is object-oriented programming? Inheritance Show the output of the following program: class Base Base()cout << Base << endl; Base(int i)cout << Base << i << endl; ~Base()cout << Destruct Base << endl; class Der: public Base Der()cout << Der << endl; Der(int i): Base(i) cout << Der << i << endl; ~Der()cout << Destruct Der << endl; int main() Base a; Der d(2); return 0;
Polymorphism What is the difference between overloading a function, overriding, and redefining a function? Using the three classes defined next, answer parts a c. class Automobile Automobile(); virtual string getname() const = 0; virtual string gettype() const; string getcolor() const;... class Car : public Automobile Car(); virtual string getname() const; virtual string gettype() const; virtual string getcolor() const; virtual void driveme();... class Sedan : public Car virtual string gettype() const; virtual void driveme();... a. What is the problem with the following statement? Automobile anauto; b. Are there any problems with the code snippet below? Why or why not? void drive(car somecar) somecar.driveme(); int main( ) Sedan sedan; drive(sedan); return 0;
c. For the statements listed below (1-4), indicate the class from which the function will be called (i.e. Sedan, Car, or Automobile) Sedan sedan; Car car; Automobile *p1 = &sedan; Automobile *p2 = &car; cout << sedan.getcolor() << endl; // (1) cout << p1->gettype() << endl; // (2) cout << p2->getcolor() << endl; // (3) cout << car.getcolor() << endl; // (4) Operator overloading Declare a class named Triple with three private data members (floats) x, y, and z. Provide public functions for setting and getting values of all the private data members. Define a constructor that initializes the values to user-specified values or, by default, sets the values all equal to 0. Also overload the following operators: Addition so that corresponding elements are added together Output so that it displays the Triple in the form The triple is (x, y, z). Post-increment so that x and z are increased by one each. Static variables Write a class that contains two class data members numborn and numliving. The value of numborn should be equal to the number of objects of the class that have been instanced. The value of numliving should be equal to the total number of objects in existance currently (ie, the objects that have been constructed but not yet destructed.) Destructor 1. What is a destructor and when is a destructor called? 2. Why would you want to have a destructor? 3. When is a virtual destructor necessary?
Pointers & dynamic memory allocation 1. Dynamic Class: o Create a class that has a single pointer to double variable. o Write a constructor that dynamically allocates space for the double and initializes the double to the value passed to the constructor. o Write the appropriate copy constructor o Why must there be a copy constructor? o Do any operators need to be overloaded? o Write a set and get method for the double. o Write a method getwhole that returns the value to the left of the decimal point. o Write a method getfractional that returns the value to the right of the decimal point. 2. Write a function isstrequal that accepts two null terminated strings (type char *) and returns true if the two strings are identical; otherwise, false is returned. Use pointer notation. o What does a call to your function look like? o Write the function. 3. What is the difference between the two prototypes below? void processarray (const char *ptext); void processarray (char * const ptext); 4. Consider the interface for class Word below where mdata is NOT nullterminated. The number of actual characters contained in mdata is held in the variable mlength. class Word static const int MAX_SIZE = 256; Word (); // Set Word to size zero Word (const char *); // The char * passed to Word IS null terminated Word (const Word &); friend ostream &operator<< (ostream &, const Word &); private: char mdata[max_size]; int mlength; Implement all of the constructors and operator<<. Will the following program compile? If so, what is the output? If not, why not?
There is a difference in how C++ assigns a value for cword3 and cword4 in the code above. Explain the difference in detail. Create a Dictionary class that is able to hold up to 1024 Words. Implement a method add that accepts a Word and adds the word to the dictionary if the word isn t already in the dictionary. 5. Consider the class Point class Point Point (double = 0.0, double = 0.0); void setx (double); double getx () const; void sety (double); double gety () const; friend ostream &operator<< (ostream &, const Point &); private: double mx, my; A Polygon is an abstract class capable of holding up to 1024 points. Write the interface for Polygon that has appropriate constructor(s), an add method, an overloaded insertion operator, virtual functions draw and perimeter, and a pure virtual function area. Triangle is a concrete class that inherits from Polygon and is to have all of the functionality of a Polygon as well an implementation for area. Also, a Triangle is to have a method isright. Write the interface for Triangle. Important: Once a triangle is created, one shouldn t be able to change the triangle into something else (say a Rectangle).
Rectangle is a concrete class that inherits from Polygon and is to have all of the functionality of a Polygon as well an implementation for area. Write the interface for Rectangle. Important: Once a rectangle is created, one shouldn t be able to change the rectangle into something else. Create an array of up to 25 Polygon pointers that can point to a Triangle or a Rectangle. Assuming that the array of polygon pointers is pointing to numpolygon objects, output the area for each object.