Programming in Modern C++ | Week 6
Session: JULY-DEC 2023
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 6 Answers
Programming
Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• Complete the destructor statement,
• Complete the constructor statement,
such that it will satisfy the given test cases
Solution:
#include<iostream>
using namespace std;
class B{
public:
B(){ cout << "1 "; }
B(double n){ cout << n << " "; }
virtual ~B();
};
class D : public B{
public:
D(double n) : B(n)
These are Nptel Programming in Modern C++ Assignment 6 Answers
Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1, define fun as pure abstract function,
• at LINE-2, complete constructor definition,
• at LINE-3, complete constructor definition,
such that it will satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class Test{
public:
virtual void fun()=0; //Line-1
};
class ReTest1 : public Test{
int d1;
public:
ReTest1(int n) : d1(n*2){ } //Line-2
void fun();
};
class ReTest2 : public Test{
int d2;
public:
ReTest2(int n) : d2(n*3){ } //Line-3
void fun(){
cout << d2 << " ";
}
};
These are Nptel Programming in Modern C++ Assignment 6 Answers
Question 3
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1, declare the function show() as pure virtual function,
• at LINE-2, with appropriate function call,
• at LINE-3, with appropriate function header,
such that it will satisfy the given test cases.
Solution:
#include <iostream>
using namespace std;
class shape{
protected:
int a,b;
shape(int x, int y) : a(x), b(y) {}
public:
virtual void show() = 0; //LINE-1
};
class triangle : public shape{
public:
triangle(int x, int y) : shape(x,y){}
void Area();
void show(){
cout << a + b << " "; //LINE-2
Area();
}
};
void shape::show(){ //LINE-3
cout << a+b << " ";
}
These are Nptel Programming in Modern C++ Assignment 6 Answers
More Weeks of Programming in Modern C++: Click here
More Nptel Courses: Click here
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 6 Answers
Quiz
Q1. Consider the following code segment.
What will be the output?
a) 13
b) 14
c) 23
d) 24
Answer: b) 14
Q2. Consider the following code segment.
What will be the output/error?
a) 10 6
b) 10 4
c) 8 6
d) 8 4
Answer: a) 10 6
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q3. Consider the following code segment.
What will be the output?
a) ABC ~C ~B ~A
b) ABC ~C ~B
c) ABC~B~A
d) ABC ~A
Answer: d) ABC ~A
Q4. Consider the following code segment.
Which line/s will give you error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: b, c
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q5. Consider the following code segment.
Which line/s will give you error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: a, d
Q6. Consider the following code segment.
What will be the output?
a) A::f(0) B::g() C::h()
b) C::f() C::g() B::h()
c) C::f() B::g() B::h()
d) C::f() C::g() C::h()
Answer: b) C::f() C::g() B::h()
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 2.
a) cb->B::fun()
b) B::fun()
c) B::cb->fun()
d) cb->fun()
Answer: a) cb->B::fun()
Q8. Consider the following code segment.
Identify the abstract classes.
a) Vehicle, Car, MotorCycle
b) Vehicle, Car, SUV
c) Vehicle, Car
d) Vehicle, Car, SportsCar, SUV
Answer: d) Vehicle, Car, SportsCar, SUV
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q9. Consider the following code segment.
What will be the output?
a) 0
b) 1
c) 2
d) Garbage
Answer: a) 0
Programming Assignment of Programming in Modern C++
Question 1
Complete the program with the following instructions.
• Fill in the blank at LINE-1 with proper access specifier.
• Fill in the blanks at LINE-2 to declare area() as a pure virtual function.
The program must satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class Shape
{
protected:
double ar;
public:
virtual void area()=0;
void show()
{
cout << ar << " ";
}
};
These are Nptel Programming in Modern C++ Assignment 6 Answers
Question 2
Consider the following program with the following instructions.
• Fill in the blank at LINE-1 with appropriate destructor declaration.
• Fill in the blank at LINE-2 with appropriate initialization list of derived class constructor.
The program must satisfy the sample input and output.
Solution:
#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "1 ";
}
Base(int n)
{
cout << n << " ";
}
virtual ~Base()
{
cout << "2 ";
};
};
class Derived : public Base
{
int num;
public:
Derived()
{
cout << "3 ";
}
Derived(int n) : Base(n), num(n*3)
{
cout << num << " ";
}
virtual ~Derived()
{
cout << "4 ";
}
};
These are Nptel Programming in Modern C++ Assignment 6 Answers
Question 3
Consider the following program. Fill in the blanks as per the instructions given below:
• Fill in the blanks at LINE-1 with an appropriate keyword.
• Fill in the blank at LINE-2 with an appropriate function declaration.
• Fill in the blank at LINE-3 with an appropriate statement.
such that it will satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class B
{
protected:
int s;
public:
B(int i=0) : s(i)
{}
virtual ~B()
{}
virtual int fun(int x)=0;
};
class D : public B
{
public:
D(int i) : B(i)
{}
~D();
int fun(int x)
{
return s+x;
}
};
class Z
{
public:
void fun(int a, int b)
{
B *t = new D(a);
int i = t->fun(b);
cout << i << " ";
delete t;
}
};
These are Nptel Programming in Modern C++ Assignment 6 Answers
These are Nptel Programming in Modern C++ Assignment 6 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: https://progiez.com/nptel/

This content is uploaded for study, general information, and reference purpose only.