Expect questions related to the sample code we've gone over in class! Below are a few sample questions based on code very similar to the examples we discussed. If you completely understand the example code from class - you will do very well on the test!
#include <iostream.h>
class blah {
public:
static int a;
int b;
blah(int x) {
b=x;
a=b+1;
}
};
int blah::a = 0;
int main(void) {
blah b1(5);
blah b2(12);
cout << "b1.a is " << b1.a << endl;
cout << "b1.b is " << b1.b << endl;
cout << "b2.a is " << b2.a << endl;
cout << "b2.b is " << b2.b << endl;
}
|
#include <iostream.h>
// Class ol_int is like an int but not quite!
class ol_int {
private:
int x;
public:
// default constructor - value is 0
ol_int() {
x=0;
}
// constructor from int
ol_int(int v) {
x=v;
}
// this is called when we assign an int to an ol_int
ol_int& operator=( int i) {
x=i;
return(*this);
}
// overloaded plus operator
// we return and int, but the compiler knows how to convert this
// to an ol_int! (uses the constructor).
// the return statement
int operator+( ol_int &o) {
return( x - o.x);
}
friend ostream &operator<<( ostream &, const ol_int& );
};
// Here is the definition of the overloaded << operator
// used to send the value of an ol_int to an output stream
ostream &operator<<(ostream &o, const ol_int &v) {
o << v.x;
return(o);
}
int main(void) {
ol_int a=3;
ol_int b=7;
ol_int c,d;
cout << "A is " << a << endl;
cout << "B is " << b << endl;
c = a + b;
cout << "C is " << c << endl;
d = 3 + 7;
cout << "D is " << d << endl;
ol_int e;
cout << "E is " << e << endl;
}
|
Also write the definition of a class named "Macintosh" that is derived from the class "computer" (computer is the base class) and includes the attribute "color" (a string) and a method named print that prints out the computer name and color.
Here is main function that will test these objects, it uses a pointer to treat a Macintosh object as a computer object, and calls the print method.
int main() {
computer *p;
Macintosh imac("Joe's IMAC","Blue");
p = &imac;
p->print();
}
I want the printout of this test program to be something like:
Name: Joe's IMAC Color: Bluethat it, I want the color printed out even though the computer class does not have any knowledge of the color attribute. (in other words - make sure your system is polymorphic).