You are on page 1of 3

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

Tutorial 1: Object-Oriented Programming (Composition and Inheritance)

1. With justification, mark the following statements as true or false:


a. The constructor of a derived class specifies a call to the constructor of the base class in the
heading of the function definition.
b. Suppose that x and y are classes, one of the data members of x is an object of type y, and both
classes have constructors. The constructor of x specifies a call to the constructor of y by using
the object name of type y.
c. A derived class must have a constructor.

2. Consider the following class hierarchy, the arrows show the inheritance between classes, and X,
Y and Z show the types of inheritance, complete the table below by stating the members that
are public, protected, and private for class D, for different combination of inheritances

Class A Members: a (Public); b (Protected); c (Private)

Class B Members: d (Public); e (Protected); f (Private)

Class C Members: g (Public); h (Protected); i (Private)

Class D Members: j (Public); k (Protected); l (Private)

Types of Inheritance Class D members


X Y Z Public Protected Private
public public public
protected protected protected
private private private
public protected private
private protected public
private private public
protected private private
public protected protected
KIE1008 (Theory Tutorial) Session 2016/2017 (Semester 2)

3. Consider the following statements,

class yClass {
public:
void one();
void two(int, int);
yClass();
private:
int a, b;
};

class xClass : public yClass {


public:
void one();
xClass();
private:
int z;
};

yclass y;
xclass x;

Are the following statements valid? If not, how to make it valid.


a. void yclass:: one () { cout << a+b << endl; }
b. y.a = 15; x.b = 30;
c. cout << y.a << “ “ << y.b << “ “ << x.z << endl;
d. void xclass:: one () {
a=10, b=15, z=30;
cout << a+b+z << endl;
}

Repeat if class xClass : private yClass.

4. What is the output of the following program?

class baseClass {
public:
void print() const;
baseClass(string s=“ “, int a=0);
protected:
int x;
private:
string str;
};

class derivedClass : public baseClass {


public:
void print() const;
derivedClass (string s=” “, int a=0, int b=0);
KIE1008 (Theory Tutorial) Session 2016/2017 (Semester 2)

private:
int y;
};

int main(){
baseClass baseObject(“This is base class”, 2);
derivedClass derivedObject(“This is derived class”, 3, 7);
baseObject.print();
derivedObject.print();
return 0;

void baseClass::print() const {cout << str << “ “ << x << endl; }

baseClass::baseClass(string s, int a) { str = s, x = a; }

void derivedClass::print() const {


baseClass::print();
cout << y << endl;
}

derivedClass::baseClass(string s, int a, int b) : baseClass(s, a+b)

{ y=b; }

5. Consider the following code, write the function setData() and print() of class one and
two.

class one {
public:
void print() const; // output the values of x and y.
protected:
void setData(int, int); // values for x and y
private:
int x, y;
};

class two : public one {


public:
void print() const; //output the values of x, y and z.
void setData(int, int, int); //values for x, y and z
private:
int z;
};

---END---

You might also like