Programming in Modern C++ | Week 4
Session: JULY-DEC 2023
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 4 Answers
Programming
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:
#include<iostream>
using namespace std;
class Employee
{
const int id;
string name;
mutable int salary;
public:
Employee(int a, string b, int c) : id(a), name(b), salary(c) {}
void updateSal(int x) const
{
salary += x;
}
void print() const
{
cout << id << " : " << name << " : " << salary;
}
};
These are Nptel Programming in Modern C++ Assignment 4 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 B;
class A
{
int a_ = 0;
public:
A(int x) : a_(x) {}
int mulB (B&);
int subtractB (B&);
};
class B
{
int b_;
public:
B(int y) : b_(y) { }
friend int A::mulB(B&), A::subtractB(B&);
};
These are Nptel Programming in Modern C++ Assignment 4 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:
#include<iostream>
using namespace std;
class Singleton{
int data;
static Singleton *ins;
Singleton(int i) : data(i) {}
public:
int get(){ return data; }
static Singleton* createIns(int i)
{
if(!ins)
ins = new Singleton(i);
return ins;
}
~Singleton(){ cout << data; }
};
These are Nptel Programming in Modern C++ Assignment 4 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++ Assignment 4 Answers
Quiz
Q1. Consider the following code segment.
What will be the output/error?
a) 15
b) 20
c) 30
d) Compilation Error : myClass::s is a static data member; it can only be initialized at its definition
Answer: d) Compilation Error : myClass::s is a static data member; it can only be initialized at its definition
Q2. Consider the following code segment.
What will be the output?
a) 2,5 4,6
b) 6,5 2,11
c) 6,11 2,5
d) 2,5 6,11
Answer: d) 2,5 6,11
These are Nptel Programming in Modern C++ Assignment 4 Answers
Q3. Consider the following code segment.
What will be the output/error?
a) 0 1 2
b) 0 2 2
c) 2 0 1
d) Compilation Error: reference to ‘var’ is ambiguous
Answer: a) 0 1 2
Q4. Consider the following code segment.
Fill in the blank at LINE-1 such that the output will be 4 5.
a) mutable
b) static
c) const
d) friend
Answer: b) static
These are Nptel Programming in Modern C++ Assignment 4 Answers
Q5. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print 15.
a) using e::e1 : : x
b) using e::e1
c) using namespace e::e1
d) using namespace e
Answer: a) using e::e1 : : x
Q6. Consider the following code segment.
Fill in the blank at LINE-1 such that the output is 10.
a) int mutable
b) mutable int
c) volatile int
d) const int
Answer: a, b
These are Nptel Programming in Modern C++ Assignment 4 Answers
Q7. Consider the following code segment.
Fill in the blank at LINE-1 such that the output is 5.
a) name::Student s(5)
b) Students (5)
c) name. Student s(5)
d) using name::Student s(5)
Answer: a) name::Student s(5)
These are Nptel Programming in Modern C++ Assignment 4 Answers
Q8. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print (2,5).
a) Point& operator<<(ostream&, Point&)
b) friend Point& operator<<(ostream&, Point&)
c) Point& friend operator<<(ostream&, Point&)
d) const Point& operator<<(ostream&, Point&)
Answer: b) friend Point& operator<<(ostream&, Point&)
These are Nptel Programming in Modern C++ Assignment 4 Answers
Q9. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print 5 15.
a) static class B
b) friend class B
c) class friend B
d) using class B
Answer: b) friend class B
These are Nptel Programming in Modern C++ Assignment 4 Answers
Assignment Questions
Question 1
Consider the program below which defines a class Database. Complete the program with the following instructions.
• Fill in the blank at LINE-1 to complete the declaration of member variable ins.
• Fill in the blank at LINE-2 to specify the return type of createIns() function.
• Fill in the blank at LINE-3 to call the createIns() function with parameter i.
The program must satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class Database{
int id;
static Database *ins;
Database(int i) : id(i) {}
public:
int getIns(){ return id; }
static Database *createIns(int i){
if(!ins)
ins = new Database(i);
return ins;
}
~Database();
};
Database *Database::ins = 0;
void fun(int i){
Database *s = Database::createIns(i);
These are Nptel Programming in Modern C++ Assignment 4 Answers
Question 2
Consider the following program.
• Fill in the blanks at LINE-1 to complete forward declaration.
• Fill in the blank at LINE-2 with appropriate function declaration so that function
calculate can access private data member of TotalAmount class.
The program must satisfy the sample input and output.
Solution:
#include<iostream>
using namespace std;
class Interest; //LINE-1
class TotalAmount{
double prinAmt;
double amt = 0;
public:
TotalAmount(double p) : prinAmt(p){}
double calculate(Interest&);
};
class Interest{
double in;
public:
Interest(double i) : in(i){ }
friend class TotalAmount; //LINE-2
};
These are Nptel Programming in Modern C++ Assignment 4 Answers
Question 3
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 to complete operator overload function,
• at LINE-2 and LINE-3 to calculate subtraction of two position class.
such that it will satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class position{
int x, y;
public:
position(int a, int b) : x(a), y(b) {}
position operator-(const position& p1){
position p(0,0);
p.x = x-p1.x;
p.y = y-p1.y;
return p;
}
These are Nptel Programming in Modern C++ Assignment 4 Answers
These are Nptel Programming in Modern C++ Assignment 4 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.