0% found this document useful (0 votes)
26 views

Week 5

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)
26 views

Week 5

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 5

Total Marks : 25

Partha Pratim Das


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

February 15, 2024

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

#include <iostream>
using namespace std;
class Base {
protected:
double i;
public:
Base(double _i) : i(_i) {}
void calculate() { cout << i << endl; }
};
class Derived : public Base {
public:
Derived(double _i) : Base(_i) {}
void calculate(double pi) { cout << i * pi << endl; }
};
int main(){
Derived i1(6.75);
i1.calculate(); //LINE-1
return 0;
}

What will be the output/error?

a) 6.75

b) 0

c) 675

d) Compilation error: no matching function for call to ’Derived::calculate()’

Answer: d)
Explanation:
When we overload base class function in the derived class, the base class function will not be
available to call using derived class object. So, it will be compilation error at LINE-1.

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

#include <iostream>
using namespace std;
class Base {
public:
void print() { cout << "C Programming" << endl; }
};
class Derived : public Base {
public:
void print() { cout << "C++ Programming" << endl; }
};
int main(){
Base *a1 = new Base();
Base *b1 = new Derived();
a1->print();
b1->print();
return 0;
}

What will be the output?

a) C Programming
C++ Programming

b) C++ Programming
C Programming

c) C Programming
C Programming

d) C++ Programming
C++ Programming

Answer: c)
Explanation:
Since print() is a non-virtual function, the binding of the function calls a1->print() and
b1->print() depend on the type of the pointers. In our case, both pointers are having C class
type. So, both pointer will call base class function Base::print(.).

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

#include<iostream>
using namespace std;
class One{
public:
One() { cout<<"1 "; }
~One() { cout << "-1 "; }
};
class Two : public One {
public:
Two() { cout << "2 "; }
~Two() { cout << "-2 "; }
};
class Three : public Two{
Two b;
public:
Three() { cout << "3 "; }
~Three() { cout << "-3 "; }
};
int main(){
Three t1;
return 0;
}

What will be the output?

a) 1 2 3 -3 -2 -1

b) 1 2 1 2 3 -3 -2 -1 -2 -1

c) 1 3 -3 -1

d) 1 1 2 3 -3 -2 -1 -1

Answer: b)
Explanation:
When an object of class Three is being instantiated, the constructor of class Two is called, which
will print ”1 2” first. Then data member of class Three is created, which will again print ”1”
then print ”2”. Then at last ”3” is printed from constructor of class Three. After the end of
main() function, reverse of already printed sequence will be printed from the destructor of the
classes. So, the answer is (b).

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

#include<iostream>
using namespace std;
class Father{
public:
void print() { cout << "Father" << endl; }
};
class Son : protected Father {
public:
Son() { ______________ } //LINE-1
};
int main(){
Son t1;
return 0;
}

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

a) Father::print();

b) Father.print();

c) (new Father)->print();

d) Father->print();

Answer: a), c)
Explanation:
It can be seen that the print() function needs to be called from class Son constructor in
order to print Father. So, it can be called using class name or temporary object. So, option
a) and c) are correct.

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

#include<iostream>
using namespace std;
class Base {
int a;
public:
Base(int _a) : a(_a) { }
void f() { cout<< a; }
};
class Derived : public Base {
int b;
public:
Derived(int x, int y) : Base(x), b(y) { }
void f() { cout<< b; };
};
int main() {
Derived obj(1,2);
________________; //LINE-1
return 0;
}

Fill in the blank at LINE-1 so that the output is 1.

a) obj.Base->f()

b) obj.Base::f()

c) obj.Base.f()

d) Base::f()

Answer: b)
Explanation:
As the function f() need to be called from the base class Base, the appropriate syntax for
function call is obj.Base::f().
Intentionally made as MSQ

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

#include <iostream>
using namespace std;
int x = 10;
class Class1{
protected:
int x;
public:
Class1() : x(20) {}
~Class1() {}
};
class Class2 : Class1{
protected:
int x;
public:
Class2() : x(30) {}
~Class2(){}
void print() { cout << _______________________________; } //LINE-1
};
int main(){
Class2 d;
d.print();
return 0;
}

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

a) Class2::x << " " << Class1::x << " " << x

b) Class2::x << " " << Class1::x << " " << ::x

c) x << " " << Class1::x << " " << ::x

d) ::x << " " << Class1::x << " " << x

Answer: b), c)
Explanation:
Since x = 30 is in the scope of class Class2 which is also the local scope for the function
print(), data can be accessed by writing Class2::x or just by x. Since x = 20 is in the scope
of class Class1, it can be accessed by writing Class1::x. Since x = 10 is in the global scope,
it can be accessed by writing ::x. So option b) and c) both are correct.

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

#include<iostream>
using namespace std;
class Print{
int x;
public:
Print(int _x) : x(_x){ }
int fun(){ return x; }
};
class RePrint : public Print{
int y;
public:
RePrint(int _x, int _y) : ________________{} //LINE-1
void fun(){ cout << Print::fun() << y; }
};
int main(){
RePrint *b2 = new RePrint(2,3);
b2->fun();
return 0;
}

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

a) Print( x), y( y)

b) Print( y), y( x)

c) y( y), Print( x)

d) y( x), Print( y)

Answer: a), c)
Explanation:
It is noted that data member of class Print should be assigned with value 2 and data member
of class RePrint i.e. y should be assigned with value 3. Hence, option a) and c) are correct.
Note that, we need to call Base class constructor in order to assign value to its data member.

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

#include<iostream>
using namespace std;
class A{
public:
int x;
A(int _x) : x(_x){}
};
class B : private A{
public:
int y;
B(int _x, int _y) : A(_x), y(_y){}
};
int main(){
A t1(1);
B t2(2,3);
cout << t1.x << " ";
cout << t2.x;
return 0;
}

What will be the output/error?

a) 1 2

b) 1 3

c) 1 1

d) Compilation error: int A::x is inaccessible

Answer: d)
Explanation:
The public class member of class A can be accessed from anywhere using the class object. So,
first cout function get complied successfully. In the second cout statement, we are trying
to access data member x using the B class object. Since the inheritance is private, the data
member x becomes private in the child class. Thus, it can not be accessed. It will give
compilation error.

8
Question 9
Consider the following code segment. [MSQ, Marks 2]

#include<iostream>
using namespace std;
class A{
public:
void printA(){ cout << "Class A"; }
};
class B : _____________ A{ //LINE-1
public:
void printB(){ cout << "Class B"; }
};
int main(){
B b;
b.printA();
return 0;
}

Fill in the blank at LINE-1 such that the program will run successfully without any error.

a) private

b) protected

c) public

d) friend

Answer: c)
Explanation:
The inherited members from the base class must appear as public in derived class so that
they can be accessed by outside functions. Only public inheritance makes sure that the public
members of the base class remain public in the derived class. Thus, c) is the correct option.
Intentionally made as MSQ

9
Programming Questions

Question 1
Complete the program with the following instructions.

• Fill in the blank at LINE-1 with appropriate inheritance statement,

• Fill in the blanks at LINE-2 appropriate initializer list,

The program must satisfy the given test cases. Marks: 3

#include<iostream>
using namespace std;
class B{
protected:
int b1;
public:
B(int b) : b1(b){}
};
class D{
protected:
int b2;
public:
D(int b) : b2(b){}
};
class DD : _____________________{ //LINE-1
int d;
public:
DD(int x) : ________________________{} //LINE-2
void show(){
cout << d << ", " << b1 << ", " << b2;
}
};
int main(){
int x;
cin >> x;
DD t1(x);
t1.show();
return 0;
}

Public 1
Input: 3
Output: 3, 6, 9

Public 2
Input: 10
Output: 10, 20, 30

10
Private 1
Input:5
Output: 5, 10, 15

Answer:
LINE-1: public B, public D
LINE-2: B(x*2), D(x*3), d(x)
Explanation:
The function show() of class DD is accessing protected member of both class B and class D.
This can be done when DD class is inherited from class B and D. So, LINE-1 will be filled as
public B, public D or protected inheritance in any order.
As per the test cases, the constructor at LINE-2 needs to be filled as B(x*2), D(x*3), d(x)
or in any order.

11
Question 2
Consider the following program with the following instructions.

• Fill in the blank at LINE-1 with appropriate keyword.

• Fill in the blanks at LINE-2 and LINE-3 to complete the constructor statements.

The program must satisfy the sample input and output. Marks: 3

#include<iostream>
using namespace std;
class Student{
string n;
string s;
int m;
protected:
Student(string _n, string _s, int _m) : n(_n), s(_s), m(_m) {}
public:
_______ void studDetails(const Student&); //LINE-1
};
class Bengali : public Student{
public:
Bengali(string n, int m) : ___________________{} //LINE-2
};
class English : public Student{
public:
English(string n, int m) : __________________{} //LINE-3
};
void studDetails(const Student &st){
cout << st.n << " --> (" << st.s << " - " << st.m << ")";
}
int main(){
string s;
int m, n;
Student *st;
cin >> m >> s >> n;
if (m==1)
st = new Bengali(s, n);
else if (m==2)
st = new English(s, n);
studDetails(*st);
return 0;
}

Public 1
Input: 1 50 65
Output: 50 --> (Bengali - 65)

Public 2
Input: 2 30 43
Output: 30 --> (English - 43)

12
Private
Input: 1 80 55
Output: 80 --> (Bengali - 55)

Answer:
LINE-1: friend
LINE-2: Student(n, "Bengali", m)
LINE-3: Student(n, "English", m)
Explanation:
The global function studDetails() needs to access private members of class Student. So, it
should be a friend function of class Student. The blank at LINE-1 must be filled with friend.
From LINE-2, constructor of class Student needs to be called with sub value as ”Bengali”. It
can be done as Student(n, "Bengali", m).
In the similar way, LINE-3 will be filled as Student(n, "English", m).

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 appropriate return statement,

• Fill in the blank at LINE-2 with appropriate return statement,

The program must satisfy the given test cases. Marks: 3

#include<iostream>
#define PI 3.14
using namespace std;
class Area{
public:
double getVal(int r){ return (4*PI*r*r); }
};
class Volume{
public:
double getVal(int r){ return (4*PI*r*r*r/3); }
};
class Sphere : public Area, public Volume{
int r;
public:
Sphere(int _r) : r(_r){ }
double getArea(){ return _______________; } //LINE-1
double getPerimeter(){ return ____________________; } //LINE-2
};
int main(){
int a;
cin >> a;
Sphere s(a);
cout << s.getArea() << ", " << s.getPerimeter();
return 0;
}

Public 1
Input: 2
Output: 50.24 33.4933

Public 2
Input: 4
Output: 200.96 267.947

Private
Input: 3
Output: 113.04 113.04

Answer:
LINE-1: Area::getVal(r)
LINE-2: Volume::getVal(r)
Explanation:

14
The class Circle must inherit from both Area and Volume classes.
The function getVal() is defined in both Area and Volume classes. To resolve the ambi-
guity, we need to use Area::getVal(r) at LINE-1 to call getVal() from class Area and
Volume::getVal(r) at LINE-3 to call getVal() from class Volume.

15

You might also like