You are on page 1of 2

KIE1008 (Theory Tutorial) Session 2016/2017 (Semester 2)

Tutorial 2: Object-Oriented Programming (Operator Overloading and Polymorphism)

1. What is the output of the following program?

class classA {
public:
virtual void print() const;
void doubleNum();
classA (int a=0);
private:
int x;
};

void classA::print() const { cout << “ClassA x: “ << x << endl; }

void classA::doubleNum() { x *= 2; }

classA::classA(int a) { x = a; }

class classB {
public:
void print() const;
void doubleNum();
classB (int a=0, int b=0);
private:
int y;
};

void classB::print() const {


classA::print();
cout << “ClassB y: “ << y << endl;
}

void classB::doubleNum() { y *= 2; }

classB::classB(int a, int b): classA(a) { y = b; }

int main() {
classA objectA(2), *ptrA;
classB objectB(3,5);
ptrA = &objectA;
ptrA->doubleNum();
ptrA->print();
cout << endl;
ptrA = &objectB;
ptrA->doubleNum();
ptrA->print();
cout << endl;
return 0;
}

2. Repeat (1) if void doubleNum()is changed to virtual void doubleNum()?


KIE1008 (Theory Tutorial) Session 2016/2017 (Semester 2)

3. Rewrite the following definition of the class student so that the functions print and
calculateGPA are pure virtual functions.

class student: public person {


public:
void print();
void calculateGPA();
void setID(int);
void getID();
void student(int, int =0);
private:
int ID;
int noOfCourses;
};

---END---

You might also like