Programming in Modern C++ Week 5 Assignment Answers
Are you looking for the NPTEL Programming in Modern C++ Week 5 Assignment Answers? You’ve come to the right place! This resource provides comprehensive solutions to all the questions from the Week 5 assignment, helping you navigate through the complexities of modern C++ programming.
Table of Contents

Programming in Modern C++ Week 5 Assignment Answers (July-Dec 2025)
Question 1. Consider the code segment below. What will be the output?
#include <iostream>
using namespace std;
class A {
protected:
int x;
public:
A(int val) : x(val) {}
virtual void show() { cout << "A: " << x << endl; }
};
class B : public A {
public:
B(int val) : A(val + 1) {}
void show() override { cout << "B: " << x << endl; }
};
int main() {
A* aPtr = new B(5);
aPtr->show();
return 0;
}
a) A: 6
b) B: 6
c) A: 5
d) B: 5
Question 2. Consider the following code segment. What is the output?
#include <iostream>
using namespace std;
class X {
public:
X() { cout << "X-ctor, "; }
~X() { cout << "X-dtor, "; }
};
class Y {
X x;
public:
Y() { cout << "Y-ctor, "; }
~Y() { cout << "Y-dtor, "; }
};
int main() {
Y y;
return 0;
}
a) X-ctor, Y-ctor, Y-dtor, X-dtor,
b) Y-ctor, X-ctor, Y-dtor, X-dtor,
c) X-ctor, Y-ctor, X-dtor, Y-dtor,
d) Y-ctor, X-ctor, X-dtor, Y-dtor
Question 3. Fill in the blank so that the output is Base::fun().
#include <iostream>
using namespace std;
class Base {
public:
void fun() { cout << "Base::fun()"; }
};
class Derived : public Base {
public:
void fun() { cout << "Derived::fun()"; }
};
int main() {
Derived d;
// _________
return 0;
}
a) d.fun();
b) Base::fun();
c) d.Base::fun();
d) Base::d.fun();
Question 4. What will be the output?
#include <iostream>
using namespace std;
int data = 5;
class Base {
protected:
int data;
public:
Base() : data(10) {}
};
class Derived : Base {
protected:
int data;
public:
Derived() : data(15) {}
void print() {
cout << data << " " << Base::data << " " << ::data << endl;
}
};
int main() {
Derived d;
d.print();
return 0;
}
a) 15 10 5
b) 10 5 15
c) 5 10 15
d) Compiler Error
Question 5. Consider the following classes. Which statements is/are correct?
class A {
public:
void f1(int);
void f2();
};
class B : public A {
public:
void f1();
void f1(int);
void f3();
};
a) B::f1() overrides A::f1(int)
b) B::f1(int) overrides A::f1(int)
c) B::f1() overloads B::f1(int)
d) B::f3() overrides A::f2()
Question 6. What will be the output?
#include <iostream>
using namespace std;
int gdata = 10;
class gmClass {
int mdata;
public:
gmClass(int mdata_ = 0) : mdata(++mdata_) { ++gdata; }
~gmClass() { mdata = 0; gdata = 0; }
void print() { cout << "mdata = " << mdata << ", gdata = " << gdata << endl; }
};
void fun() {
gmClass ob;
ob.print();
}
int main() {
gmClass ob;
fun();
ob.print();
return 0;
}
a) mdata = 9, gdata = 11 / mdata = 1, gdata = 12
b) mdata = 9, gdata = 12 / mdata = 1, gdata = 0
c) mdata = 1, gdata = 11 / mdata = 2, gdata = 12
d) mdata = 1, gdata = 12 / mdata = 1, gdata = 0
Question 7. Consider the following program. What will be the output/error?
#include<iostream>
using namespace std;
class Account {
int acc_no;
string acc_type;
double acc_balance;
public:
Account(int acc_no_, string acc_type_, double acc_balance_)
: acc_no(acc_no_), acc_type(acc_type_), acc_balance(acc_balance_) {}
void printStatus() {
cout << acc_no << ":" << acc_type << ":" << acc_balance << ":";
}
};
class Customer : private Account {
int cust_id;
string cust_name;
public:
Customer(int cust_id_, string cust_name_, int acc_no_, string acc_type_, double acc_balance_)
: Account(acc_no_, acc_type_, acc_balance_), cust_id(cust_id_), cust_name(cust_name_) {}
void printStatus() {
Account::printStatus();
cout << cust_id << ":" << cust_name;
}
};
void print(Account* ac) {
ac->printStatus(); // LINE-1
}
int main() {
Customer* cObj = new Customer(101, "Rajiv", 2002023, "current", 20000.00);
print(cObj); // LINE-2
return 0;
}
a) 2002023:current:20000:
b) 2002023:current:20000:101:Rajiv
c) Compiler error at LINE-1: inaccessible printStatus()
d) Compiler error at LINE-2: Account is an inaccessible base of Customer
Question 8. What will be the output/error?
#include <iostream>
using namespace std;
class PCP {
public:
PCP() {}
~PCP() {}
private:
PCP(const PCP& obj) {}
PCP& operator=(const PCP&) { return *this; }
};
class IntData : public PCP {
int i;
public:
IntData() {}
IntData(const int& i_) : i(i_) {}
void print() { cout << i << " "; }
};
int main() {
IntData i01(10);
IntData i02(20);
i01 = i02;
i01.print();
i02.print();
return 0;
}
a) 20 10
b) 20 20
c) Compiler error: operator= is private
d) Compiler error: copy constructor is private
Question 9. Consider the following program. What will be the output/error?
class A {
public:
int a;
};
class B : protected A {
public:
int b;
};
class C : public B {
public:
int c;
C(int a_, int b_, int c_) {
a = a_; // LINE-1
b = b_; // LINE-2
c = c_;
}
};
int main() {
C cObj(10, 20, 30);
cout << cObj.a << " "; // LINE-3
cout << cObj.b << " ";
cout << cObj.c;
return 0;
}
a) 10 20 30
b) Compiler error at LINE-1: a is not accessible
c) Compiler error at LINE-2: a is not accessible
d) Compiler error at LINE-3: b is not accessible
Programming in Modern C++ Week 5 Assignment Answers (Jan-Apr 2025)
Course Link: Click Here
Q1. Consider the following code segment:
#include <iostream>
using namespace std;
class myClass1 {
static int x1;
double x2;
public:
void fun() { cout << "fun" << endl; }
};
class myClass2 : public myClass1 {
double dt;
};
int myClass1::x1 = 0;
int main() {
myClass2 d;
cout << sizeof(d);
return 0;
}
What will be the output? (Assume sizeof(double) = 8 and sizeof(int) = 4)
a) 16
b) 12
c) 20
d) 24
Q2. Consider the following code segment:
#include <iostream>
using namespace std;
class base {
protected:
int m1;
public:
base(int x) : m1(x) { }
void f1() { cout << m1 << endl; }
};
class derived : public base {
public:
derived(int x) : base(x) { }
void f1(int a) { cout << a << endl; }
};
int main() {
derived d(10);
d.f1(); // LINE-1
return 0;
}
What will be the output/error?
a) 10
b) 44
c) 12
d) Compilation Error: no matching function for call to derived::f1()
Q3. Consider the following code segment:
#include <iostream>
using namespace std;
class base {
public:
void print() { cout << "C" << " "; }
};
class derived : public base {
public:
void print() { cout << "CH" << " "; }
};
int main() {
base *a1 = new base();
base *b1 = new derived();
a1->print();
b1->print();
return 0;
}
What will be the output?
a) C
b) C CH
c) CH
d) CH CH
Q4. Consider the following code segment:
#include<iostream>
using namespace std;
class A {
public:
int a;
A(int x) : a(x) { }
};
class B : protected A {
int b;
public:
B(int x, int y) : A(x), b(y) {}
};
int main() {
B t1(1,2);
A t2(5);
cout << t1.a; // LINE-1
cout << t2.a; // LINE-2
return 0;
}
Which line will generate a compilation error in the main() function?
a) LINE-1
b) LINE-2
c) Both LINE-1 and LINE-2
d) No compilation error
Q5. Consider the following code segment:
#include<iostream>
using namespace std;
class A {
public:
A() { cout << "A "; }
~A() { cout << "~A "; }
};
class B : public A {
public:
B() { cout << "B "; }
~B() { cout << "~B "; }
};
class C : public B {
public:
C() { cout << "C "; }
~C() { cout << "~C "; }
};
int main() {
C obj;
return 0;
}
What will be the output?
a) A B C ~C ~B ~A
b) A A B C ~C ~B ~A
c) A B A B C ~C ~B ~A ~A
d) A A B C A ~A ~A
Q6. Consider the following code segment:
#include <iostream>
using namespace std;
class base {
public:
static void func() { cout << "C++" << endl; }
};
class derived : private base {
public:
derived () {
// LINE-1
}
};
int main() {
derived d;
return 0;
}
Fill in the blank at LINE-1 such that the output is "C++"
a) (new base)->func()
b) base::func()
c) base.func()
d) base::func
Q7. Consider the following code segment:
#include <iostream>
using namespace std;
class A1 {
public:
int t1;
};
class A2 : private A1 {
public:
int t2;
int sum() { return t1 + t2; }
};
int main() {
A1 b;
A2 d;
b.t1 = 10; // LINE-1
d.t1 = 20; // LINE-2
d.t2 = 30; // LINE-3
cout << d.sum(); // LINE-4
return 0;
}
Which line/s in the main() function will generate a compilation error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Q8. Consider the following code segment:
#include<iostream>
using namespace std;
class B {
int x;
public:
B(int _x) : x(_x) {}
int fun() { return x; }
};
class D : public B {
int y;
public:
D(int _x, int _y) : B(_x), y(_y) {} // LINE-1
void fun() { cout << B::fun() << y; }
};
int main() {
D *b2 = new D(1,0);
b2->fun();
return 0;
}
Fill in the blank at LINE-1 such that the program will print 10.
a) B(x), y(y)
b) B(y), y(y)
c) y(y), B(y)
d) y(x), B(y)
Q9. Consider the following code segment:
#include<iostream>
using namespace std;
class myClass1 {
int x;
public:
myClass1(int a) : x(a) {}
void fun() {
cout << x;
}
};
class myClass2 : public myClass1 {
int y;
public:
myClass2(int a, int b) : myClass1(a), y(b) {}
void fun() {
cout << y;
}
};
int main() {
myClass2 m(1,2);
// LINE-1
return 0;
}
Fill in the blank at LINE-1 such that the program will print 4.
a) myClass1.m::fun()
b) m.myClass1::fun()
c) m.myClass1.fun()
d) myClass1.m.fun()
Programming in Modern C++ Week 5 Assignment Answers (July-Dec 2024)
Q1.Consider the following code segment.
What will be the output?
a)1, 2,3
b) 1, 2, 3-3, 1, 2, 3-3,
c) 1,2, 3-3, 4, 5, 6-6,
d) 1-1, 2, 3, 4-4, 5, 6,
Answer: d) 1-1, 2, 3, 4-4, 5, 6,
Q2 Consider the following code segment.
What will be the output?
a) AB C- C -B -A
b)A A B C -C A -A
c) A C -C -A
d) A A B C -C -B- A- A
Answer: d) A A B C -C -B- A- A
For answers or latest updates join our telegram channel: Click here to join
Q3.Consider the following code segment.
What will be the output?
a) Vehicle created, Car created, Vehicle created, Bike created, SportsCar created, SportsCar destroyed, Bike destroyed, Vehicle destroyed, Car destroyed, Vehicle
destroyed,
b) Vehicle created, Car created, SportsCar created, Vehicle created, Bike created, Bike destroyed, Vehicle destroyed, SportsCar destroyed, Car destroyed, Vehicle
destroyed,
c) Vehicle created, Car created, SportsCar created, Bike created, Bike destroyed, SportsCar destroyed, Car destroyed, Vehicle destroyed,
d) Vehicle created, Car created, Sports Car created, Bike created, Vehicle destroyed, Car destroyed, Sports Car destroyed, Bike destroyed,
Answer: a) Vehicle created, Car created, Vehicle created, Bike created, SportsCar created, SportsCar destroyed, Bike destroyed, Vehicle destroyed, Car destroyed, Vehicle
destroyed,
Q4.Consider the following code segment.
Choose the appropriate option to fill in the blank at LINE-1 such that the output of the code becomes Instrument: :play().
a) g.play()
b) Instrument: :play()
¢) g.Instrument: :play()
d) Instrument::g.play()
Answer: ¢) g.Instrument: :play()
For answers or latest updates join our telegram channel: Click here to join
Q5.Consider the following code segment.
Choose the appropriate option(s) to fill in the blank at LINE-1 such that the output becomes |
25 15 5
a) this->value << ” ” << Parent::value << ” ” << value |
b) Child::value << ” ” << Parent::value << ” ” << value |
c) value << ” ” << Parent::value << ” ” << ::value |
d) Child: :value << ” ” << Parent::value << ” ” << ::value |
Answer: c) value << ” ” << Parent::value << ” ” << ::value |
d) Child: :value << ” ” << Parent::value << ” ” << ::value |
Q6. Consider the following code segment.
a) memberVar = 6, globalVar = 7
memberVar = 1, globalVar = 8
b) memberVar = 6, globalVar = 8
memberVar = 1, globalVar = 0
c) memberVar = 1, globalVar = 7
memberVar = 2, globalVar = 8
d) memberVar = 1, globalVar = 7
memberVar = 1, globalVar = 0
Answer: d) memberVar = 1, globalVar = 7
memberVar = 1, globalVar = 0
For answers or latest updates join our telegram channel: Click here to join
Q7. Consider the following code segment.
‘What will be the output /error?
a) 12345:Sedan:
b) 12346:Sedan:101
c) compiler error at LINE-1: Vehicle is an inaccessible base of Car
d) compiler error at LINE-2: ‘display’ was not declared in this scope
Answer: d) compiler error at LINE-2: ‘display’ was not declared in this scope
Q8. Consider the following code segment.
‘What will be the output /error?
a) 40 30
b) 40 40
c) Compiler error: ’Basek operator=(const Basek)’ is private
d) Compiler error: ’Base(const Basek obj)’ is private
Answer: c) Compiler error: ’Basek operator=(const Basek)’ is private
For answers or latest updates join our telegram channel: Click here to join
Q9.Consider the following code segment.
What will be the output /error(s)?
a) 10 20 30
b) compiler error at LINE-1: X::x is not accessible
c) compiler error at LINE-2: X::x is not accessible
d) compiler error at LINE-3: Y::y is not accessible
Answer: c) compiler error at LINE-2: X::x is not accessible
For answers or latest updates join our telegram channel: Click here to join
For answers to additional Nptel courses, please refer to this link: NPTEL Assignment
Programming in Modern C++ Week 5 Assignment Answers (JAN-APR 2024)
Course Name: Programming in Modern C++
Course Link: Click Here
For answers or latest updates join our telegram channel: Click here to join
Quiz
Q1. Consider the following code segment.
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) Compilation error: no matching function for call to ‘Derived::calculate()’
Q2. Consider the following code segment.
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) C Programming
C Programming
Q3. Consider the following code segment.
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) 1 2 1 2 3 -3 -2 -1 -2 -1
For answers or latest updates join our telegram channel: Click here to join
These are Programming in Modern C++ Week 5 Answers
Q4. Consider the following code segment.
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)
Q5. Consider the following code segment.
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) obj. Base::f()
Q6. Consider the following code segment.
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)
For answers or latest updates join our telegram channel: Click here to join
These are Programming in Modern C++ Week 5 Answers
Q7. Consider the following code segment.
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)
Q8. Consider the following code segment.
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) Compilation error: int A::x is inaccessible
Q9. Consider the following code segment.
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) public
For answers or latest updates join our telegram channel: Click here to join
These are Programming in Modern C++ Week 5 Answers
Programming Assignment
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.
Solution:
class DD : public B, public D {
int d;
public:
DD(int x) : B(x * 2), D(x * 3), d(x * 1) {}
void show() {
cout << d << ", " << b1 << ", " << b2;
}
};
For answers or latest updates join our telegram channel: Click here to join
These are Programming in Modern C++ Week 5 Answers
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.
Solution:
#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:
friend void studDetails(const Student&);
};
class Bengali : public Student{
public:
Bengali(string n, int m) : Student(n, "Bengali", m){}
};
class English : public Student{
public:
English(string n, int m) : Student(n, "English", m){}
};
For answers or latest updates join our telegram channel: Click here to join
These are Programming in Modern C++ Week 5 Answers
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.
Solution:
class Sphere : public Area, public Volume{
int r;
public:
Sphere(int _r) : r(_r){ }
double getArea(){ return Area::getVal(r); }
double getPerimeter(){ return Volume::getVal(r); }
};
For answers or latest updates join our telegram channel: Click here to join
These are Programming in Modern C++ Week 5 Answers
More Weeks of Programming in Modern C++: Click here
More Nptel Courses: Click here
Programming in Modern C++ Week 5 Assignment Answers (JULY-DEC 2023)
Course Name: Programming in Modern C++
Course Link: Click Here
These are Programming in Modern C++ Week 5 Answers
Programming Assignment
Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• Complete the variable declaration at LINE-1,
• Complete the function prototype at LINE-2 and LINE-3 with appropriate keywords
such that it will satisfy the given test cases.
Solution:
class Cube : public Volume, public SurfaceArea { //LINE-1
int _a;
public:
Cube(int a) : _a(a) { }
double getVolume() { return Volume::getValue(_a); } //LINE-2
double getSurfaceArea() { return SurfaceArea::getValue(_a); } //LINE-3
};
These are Programming in Modern C++ Week 5 Answers
Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1 with appropriate forward declaration,
• at LINE-2 with appropriate statement
such that it will satisfy the given test cases
Solution:
#include <iostream>
using namespace std;
class Vehicle{
string vehicleName;
int noOfWheels;
protected:
Vehicle(string s, int w) : vehicleName(s), noOfWheels(w) { }
public:
friend void vehicleDetails(const Vehicle&);
};
class Twowheeler : public Vehicle{
public:
Twowheeler(string n) : Vehicle(n, 2) { }
};
class Fourwheeler : public Vehicle{
public:
Fourwheeler(string n) : Vehicle(n, 4) { }
};
These are Programming in Modern C++ Week 5 Answers
Question 3
Consider the following program. Fill in the blanks at LINE-1, LINE-2 and LINE-3 with appropriate statements such that it will satisfy the given test cases.
Solution:
class D : public B1, public B2 {
int d;
public:
D(int x) : B1(x+5), B2(x+10), d(x) {}
These are Programming in Modern C++ Week 5 Answers
More Weeks of Programming in Modern C++: Click here
More Nptel Courses: Click here
Session: JULY-DEC 2022
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Week 5 Answers
Quiz
Q1. Consider the following code segment.
What will be the output /error?
a) 6.75
b) 0
c) 675
d) Compilation error: no matching function for call to ’FDInterest::calculate()’
Answer: d) Compilation error: no matching function for call to ’FDInterest::calculate()’
Q2. Consider the following code segment.
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) C Programming
C Programming
These are Nptel Programming in Modern C++ Week 5 Answers
Q3. Consider the following code segment.
What will be the output?
a) 1 2 3 -3 -2 -1
b) 1 1 2 3 -3 -1 -1
c) 1 3 -3 -1
d) 1 1 2 3 -3 -2 -1 -1
Answer: d) 1 1 2 3 -3 -2 -1 -1
Q4. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print Base.
a) Base::print();
b) Base.print();
c) (new Base)->print();
d) Base->print();
Answer: a, c
These are Nptel Programming in Modern C++ Week 5 Answers
Q5. Consider the following code segment.
What will be the output /error?
a) 5 10
b) 0 10
c) Compilation error generated from LINE-1
d) Compilation error generated from LINE-2
Answer: d) Compilation error generated from LINE-2
Q6. Consider the following code segment.
What will be the output?
a) A::2 B::2 C::2 A::3 B::3 C::3 A::1 B::1 C::1 ~C ~B ~A ~C ~B ~A ~C ~B ~A
b) A::2 B::2 C::2 A::3 B::3 C::3 ~C ~B ~A A::1 B::1 C::1 ~C ~B ~A ~C ~B ~A
c) C::2 B::2 A::2 C::3 B::3 A::3 C::1 B::1 A::1 ~C ~B ~A ~A ~B ~A ~C ~B ~A
d) C::2 B::2 A::2 C::3 B::3 A::3 C::1 B::1 A::1 ~A ~B ~C ~A ~B ~C ~A ~B ~C
Answer: b) A::2 B::2 C::2 A::3 B::3 C::3 ~C ~B ~A A::1 B::1 C::1 ~C ~B ~A ~C ~B ~A
These are Nptel Programming in Modern C++ Week 5 Answers
Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print Base::£().
a) Base.obj.f()
b) Base.obj::f()
c) obj.Base::f()
d) Base::0bj.f()
Answer: c) obj.Base::f()
Q8. Consider the following code segment.
What will be the output /error?
a) Partha CSE
b) unknown CSE
c) Compilation error at LINE-1: void ’Staff::print1()’ is inaccessible in this context
d) Compilation error at LINE-2: void ’Staff::print2()’ is inaccessible in this context
Answer: c) Compilation error at LINE-1: void ’Staff::print1()’ is inaccessible in this context
These are Nptel Programming in Modern C++ Week 5 Answers
Q9. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print 10 20.
a) A2(_t1, _t2)
b) A2(_t2, _t1)
c) AL(_t1), A2(_t2)
d) A2(_t1), A2(_t1, _t2)
Answer: a) A2(_t1, _t2)
Programming Assignment
Question 1
Complete the program with the following instructions.
• Fill in the blank at LINE-1 with appropriate initializer list,
• Fill in the blanks at LINE-2 and LINE-3 to complete the return statements.
The program must satisfy the given test cases.
Solution:
class Sphere:public Volume,public Area{
public:
Sphere(int _r):Volume(_r), Area(_r){}
double getVolume(){
return Volume::getVal();
}
double getArea(){
return Area::getVal();
}
};
These are Nptel Programming in Modern C++ Week 5 Answers
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.
Solution:
#include <iostream>
using namespace std;
class Vehicle
{
public:
string vehicleName;
int noOfWheels;
Vehicle(string s,int w):vehicleName(s),noOfWheels(w)
{}
void vehicleDetails(const Vehicle&);
};
class Twowheeler:public Vehicle
{
public:
Twowheeler(string n):Vehicle(n,2)
{}
};
class Fourwheeler:public Vehicle
{
public:
Fourwheeler(string n):Vehicle(n, 4)
{}
};
These are Nptel Programming in Modern C++ Week 5 Answers
Question 3
Consider the following program. Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 to complete the inheritance statement.
• Fill in the blank at LINE-2 with the appropriate initializer list,
The program must satisfy the given test cases.
Solution:
class ReTest:public Test1,public Test2
{
int t;
public:
ReTest(int x):Test1(x+5),Test2(x+10),t(x)
{}
void show()
{
cout << t << ", " << t1 << ", " << t2;
}
}
These are Nptel Programming in Modern C++ Week 5 Answers








