Question 1: dynamic binding
Assuming all the 'new' operations succeed,
show the exact output from the program below:
#include <iostream>
using namespace std;
class grand {
public: virtual void print() = 0;
};
class parent: public grand {
public: virtual void print();
};
class child: public parent {
public: void print();
};
int main() {
grand *g1, *g2;
g1 = new parent;
g2 = new child;
g1->print();
g2->print();
parent *p1, *p2;
p1 = new parent;
p2 = new child;
p1->print();
p2->print();
parent p;
child c;
p = c;
p.print();
}
void parent::print() {
cout << "parent" << endl;
}
void child::print() {
cout << "child" << endl;
}
Question 2: multiple inheritance
If one class inherits from two or more other classes,
there is a potential for name clashes to occur. Provide a
short code example that illustrates a name clash, then show
how to resolve it.
Question 3: operator overloading
Is the following class definition valid?
Explain why or why not.
class number {
public:
number(double d = 0);
~number();
number operator=(int i);
number operator=(float f);
number operator=(double d);
number operator=(number n);
void print();
private:
double num;
};
Question 4: copy constructors
In your own words, explain what a copy constructor is,
when one would be used, what the default copy constructor does,
and under what circumstances you should replace the default
with one of your own devising.