0% found this document useful (0 votes)
24 views15 pages

Week 6

Modern C++ NPTEL assignment

Uploaded by

Sheryl Viniba
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)
24 views15 pages

Week 6

Modern C++ NPTEL assignment

Uploaded by

Sheryl Viniba
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/ 15

Programming in Modern C++: Assignment Week 6

Total Marks : 25

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology Kharagpur, Kharagpur – 721302
[email protected]

February 22, 2024

Question 1
Consider the following code segment. [MCQ, Marks 2]

#include<iostream>
using namespace std;
class ClassA{
public:
void fun1() { cout << "A1" ; }
virtual void fun2() { cout << "A2" ; }
};
class ClassB : public ClassA{
public:
virtual void fun1() { cout << "B1" ; }
void fun2() { cout << "B2" ; }
};
int main(){
ClassA *t = new ClassB();
t->fun1();
t->fun2();
return 0;
}

What will be the output?

a) A1B1

b) A1B2

c) A2B1

d) A2B2

Answer: b)
Explanation:
As fun1() is a non-virtual function at the base class, for the t->fun1() function call static
binding is done. So, the function of pointer type will be called.
As fun2() is a virtual function, for the t->fun2() function call dynamic binding is done. So,
the function of object type will be called.

1
Question 2
Consider the following code segment. [MCQ, Marks 2]

#include<iostream>
using namespace std;
class ClassA{
public:
virtual void fun() = 0;
};
class ClassB{
public:
virtual void fun(){ }
};
class ClassC{
int x;
public:
void fun(){ }
};
int main(){
cout << sizeof(ClassA) << " " << sizeof(ClassB) << " " << sizeof(ClassC);
return 0;
}

What will be the output?

a) 8 8 1

b) 8 8 4

c) 1 8 4

d) 0 1 4

Answer: b)
Explanation:
Class ClassA and ClassB have virtual functions hence, each of them has a pointer to VFT
which is of 8 bytes. Hence, sizeof(ClassA) and sizeof(ClassB) is 8. Class ClassC has a
data member of int type. Hence, sizeof(ClassC) = 4. Option b) is correct.

2
Question 3
Consider the following code segment. [MCQ, Marks 2]

#include<iostream>
using namespace std;
class A{
public:
A() { cout<<"A "; }
~A() { cout<<"~A "; }
};
class B : public A{
public:
B() { cout<<"B "; }
virtual ~B() { cout<<"~B "; }
};
class C : public B{
public:
C() { cout<<"C "; }
virtual ~C() { cout<<"~C "; }
};
int main(){
A *t1 = new C;
delete t1;
return 0;
}

What will be the output?

a) A B C ∼C ∼B ∼A

b) A B C ∼C ∼B

c) A B C ∼B ∼A

d) A B C ∼A

Answer: d)
Explanation:
When the object of class C is created, it calls constructor of class C which in turn calls con-
structor of class B and A respectively. So, it will print A B C.
Whenever, the object is deleted, it calls destructor of class A first. The destructor of class A is
not virtual, so it will not call child class destructor. So, final result will be A B C ∼A.

3
Question 4
Consider the following code segment. [MSQ, Marks 2]

#include <iostream>
using namespace std;
class myClass {
public:
virtual void test() = 0; //LINE-1
};
void myClass::test() {
cout << "Pure virtual function";
}
int main() {
myClass m; // LINE-2
myClass *p = new myClass(); // LINE-3
myClass.test(); // LINE-4
return 0;
}

Which line/s will NOT give you error?

a) LINE-1

b) LINE-2

c) LINE-3

d) LINE-4

Answer: a)
Explanation:
Any abstract base class cannot be instantiated and hence will give an error at LINE-2. Also, we
cannot create abstract class object using new operator and hence will give an error at LINE-3.
The test() function is implemented in class myClass. Hence, the class is not pure virtual.
So, LINE-4 will also give error. So, the correct option is a).
Intentionally made as MSQ

4
Question 5
Consider the following code segment. [MSQ, Marks 2]

#include<iostream>
using namespace std;
class Base{
public:
virtual void fun() { cout << "Base"; }
};
class Derived : public Base{
public:
void fun(double i) { cout << "Derived"; }
};
int main(){
Derived t1;
Base *t2 = new Derived();
t1.fun(); //LINE-1
t1.fun(3.14); //LINE-2
t2->fun(9.8); //LINE-3
t2->fun(); //LINE-4
return 0;
}

Which line/s will give you error?

a) LINE-1

b) LINE-2

c) LINE-3

d) LINE-4

Answer: a), c)
Explanation:
The function fun() of class Base is overloaded in class Derived. So, base class function become
hidden for derived class. So, LINE-1 will give error. On the other hand, class Base doesn’t
have fun(double) in its definition. So, LINE-3 will give an error.

5
Question 6
Consider the following code segment. [MSQ, Marks 2]

#include<iostream>
using namespace std;
class Class1{
public:
virtual void fun(){ cout << "Class1::fun() "; }
};
class Class2 : public Class1{
public:
void fun(){ cout << "Class2::fun() "; }
};
class Class3 : public Class2{
public:
void fun(){ cout << "Class3::fun() "; }
};
int main(){
Class1 *cb = ____________; //LINE-1
cb->fun();
return 0;
}

Fill in the blank at LINE-1 such that the program will print Class2::fun().

a) new Class1

b) new Class2

c) new Class3

d) It cannot be printed

Answer: b)
Explanation:
If a function is declared as virtual, the function call depends on the type of object the pointer
points to, not on the type of the pointer. Hence, to call function fun() from Class2 class,
LINE-1 need to be filled as new Class2.

6
Question 7
Consider the following code segment. [MCQ, Marks 2]

#include<iostream>
using namespace std;
class ClassA{
int b;
public:
ClassA(int i) : b(i) {}
virtual void f(ClassA *t) { cout << t->b << endl; }
};
class ClassB : public ClassA{
int d;
public:
ClassB(int i=0, int j=0) : ClassA(i), d(j) { }
void f(ClassB *t) { cout << t->d << endl; }
};
int main(){
ClassA *t1 = new ClassB(1,2);
t1->f(new ClassB); //Line-1
return 0;
}

What will be the output?

a) 0

b) 1

c) 2

d) Garbage values

Answer: a)
Explanation:
The function call at LINE-1 invokes derived class function with a temporary object as param-
eter. The temporary object of class ClassB has data members with value 0 as constructor
initializes both the data members with default value 0. Hence, the program will print 0.

7
Question 8
Consider the following code segment. [MCQ, Marks 2]

#include<iostream>
using namespace std;
class classA{
public:
virtual void f(){ cout << "1 "; }
};
class classB : public classA{
public:
void f(){ cout << "2 "; }
};
class classC : public classB{
public:
void f(){ cout << "3 "; }
};
int main(){
classC *cb = new classC;
_____________________; //LINE-1
return 0;
}

Fill in the blank at LINE-1 so that the program will print 1.

a) cb->f()

b) classA::cb->f()

c) cb->classA::f()

d) classA::f()

Answer: c)
Explanation:
As cb is a pointer to object of class classC, we can call f() from class classA as cb->classA::f()
such that it will print 1.

8
Question 9
Identify the abstract class/es from the following code snippet. [MCQ, Marks 2]

class Flower {
public:
virtual void desc() = 0 { cout << "Flower"; }
};

class FlowerWSmell : public Flower {


void desc() { cout << "Flower with smell"; }
};

class FlowerWOSmell : public Flower {};

class Tuberose : public FlowerWSmell {


public:
void desc() { cout << "Tuberose Flower"; }
};

class Jasmine : public FlowerWSmell {


public:
void desc() { cout << "Jasmine Flower"; }
};

class Sunflower : public FlowerWOSmell {


public:
void desc() { cout << "Sunflower flower"; }
};

class Dahlia : public FlowerWOSmell {};

a) Flower, FlowerWSmell, FlowerWOSmell

b) Flower, FlowerWOSmell, Dahlia

c) Flower, FlowerWSmell, FlowerWOSmell, Sunflower

d) Flower

Answer: b)
Explanation:
An abstract base class contains at least one pure virtual function. Moreover a class derived
from an abstract base class will also be abstract unless you override each pure virtual function
in the derived class with non-pure ones. So option b) is the correct answer.

9
Programming Questions

Question 1
Complete the program with the following instructions.
• Fill in the blank at LINE-1 with appropriate keyword,
• Fill in the blanks at LINE-2 to declare fun() as a pure virtual function,
• fill in the blank at LINE-3 with approriate object instantiation,
The program must satisfy the given test cases. Marks: 3
#include<iostream>
using namespace std;
class Base{
protected:
int s;
public:
Base(int i=0) : s(i){}
_______ ~Base(){ } //LINE-1
______________________ //LINE-2
};
class Derived : public Base{
int x;
public:
Derived(int i, int j) : Base(i), x(j) {}
~Derived();
int fun(){
return s*x;
}
};
class Wrapper{
public:
void fun(int a, int b){
Base *t = _______________; //LINE-3
int i = t->fun();
cout << i << " ";
delete t;
}
};
Derived::~Derived(){ cout << int(s/x) << " "; }
int main(){
int i, j;
cin >> i >> j;
Wrapper w;
w.fun(i,j);
return 0;
}

Public 1
Input: 5 2
Output: 10 2

10
Public 2
Input: 8 4
Output: 32 2

Private 1
Input: 9 5
Output: 45 1

Answer:
LINE-1: virtual
LINE-2: virtual int fun() = 0;
LINE-3: new Derived(a, b)
Explanation:
The destructor of Base class needs to be declared as virtual in order to call Derived class
destructor at the time of deletion. The function fun can be declared as pure virtual function
at LINE-2 as virtual int fun() = 0;. We can’t instantiate abstract class. So, LINE-3 can
be filled as new Derived(a, b).

11
Question 2
Consider the following program with the following instructions.
• Fill in the blank at LINE-1 and LINE-2 with appropriate return statements
The program must satisfy the sample input and output. Marks: 3
#include <iostream>
using namespace std;
const double ir = 7;
const double bonus = 4;
class FD{
protected:
double principal;
public:
FD(double _p = 0.0) : principal(_p){}
double getInterest(){ return principal * ir / 100; }
double getExtraAmt(){ return principal * bonus / 100 ; }
virtual double getMaturity() = 0;
};
class Customer : public FD{
public:
Customer(double _amt) : FD(_amt){}
double getMaturity();
};
class Employee : public FD{
public:
Employee(double _amt) : FD(_amt){}
double getMaturity();
};
double Customer::getMaturity(){
return _____________________; //LINE-1
}
double Employee::getMaturity(){
return ______________________________; //LINE-2
}
int main(){
double d;
cin >> d;
FD *ep[2] = {new Customer(d), new Employee(d)};
cout << ep[0]->getMaturity() << " ";
cout << ep[1]->getMaturity();
return 0;
}

Public 1
Input: 100 100
Output: 107 111

Public 2
Input: 1000 5000
Output: 1070 1110

12
Private
Input: 10 20
Output: 10.7 11.1

Answer:
LINE-1: FD::getInterest() + principal
LINE-2: FD::getInterest() + FD::getExtraAmt() + principal
Explanation:
To satisfy the test cases, we must call the functions FD::getInterest() and FD::getExtraAmt().
The blank at LINE-1 must be filled by:
FD::getInterest() + principal
and the blank at LINE-2 must be filled by:
FD::getInterest() + principal

13
Question 3
Consider the following program. Fill in the blanks as per the instructions given below:

• Fill in the blank at LINE-1 with an appropriate destructor declaration.

• Fill in the blank at LINE-2 with an appropriate constructor statement.

such that it will satisfy the given test cases. Marks: 3

#include<iostream>
using namespace std;
class A{
public:
A(){ cout << "11 "; }
A(int n){ cout << n + 2 << " "; }
______________; //LINE-1
};

class B : public A{
public:
B(int n) : ________ //LINE-2
{
cout << n + 5 << " ";
}
B(){ cout << "21 "; }
virtual ~B(){ cout << "22 "; }
};
A::~A(){ cout << "12 "; }
int main(){
int i;
cin >> i;
A *pt = new B(i);
delete pt;
return 0;
}

Public 1
Input: 4
Output: 6 9 22 12

Public 2
Input: 5
Output: 7 10 22 12

Private
Input: 8
Output: 10 13 22 12

Answer:
LINE-1: virtual ∼A()
LINE-2: A(n)

14
Explanation:
At LINE-1, the destructor need to be defined as virtual destructor, so that if the derived class
object gets deleted it will be called automatically. Hence, LINE-1 has to be filled with virtual
∼A();
The initialization-list required at LINE-2 is A(n).

15

You might also like