0% found this document useful (0 votes)
86 views2 pages

What Is The Output of The Following Program?: KIE1008 (Theory Tutorial) Session 2016/2017 (Semester 2)

This document discusses object-oriented programming concepts like operator overloading and polymorphism. It contains 3 questions: 1. It asks what the output of a sample program is that uses polymorphism through a base class and derived class. 2. It asks what the output would be if a function was changed to be virtual. 3. It asks to modify a student class to make two functions pure virtual, making the class abstract.

Uploaded by

Mazen Jamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views2 pages

What Is The Output of The Following Program?: KIE1008 (Theory Tutorial) Session 2016/2017 (Semester 2)

This document discusses object-oriented programming concepts like operator overloading and polymorphism. It contains 3 questions: 1. It asks what the output of a sample program is that uses polymorphism through a base class and derived class. 2. It asks what the output would be if a function was changed to be virtual. 3. It asks to modify a student class to make two functions pure virtual, making the class abstract.

Uploaded by

Mazen Jamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy