Programming in Modern C++ | Week 7

Session: JULY-DEC 2023

Course Name: Programming in Modern C++

Course Link: Click Here

These are Nptel Programming in Modern C++ Assignment 7 Answers


Programming

Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
   • at LINE-1, complete the constructor statement,
   • at LINE-2 and LINE-3, complete the operator overloading function header,
such that it will satisfy the given test cases.

Solution:

#include<iostream>
#include<cctype>
using namespace std;
class Char{
    char ch;
    public:
        Char(char _ch) : ch(tolower(_ch)){}

        operator char(){ return ch; }

        operator int(){ return ch - 'a' + 1; }
};

These are Nptel Programming in Modern C++ Assignment 7 Answers


Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
  • at LINE-1, complete the constructor definition,
  • at LINE-2, complete the overload function header,
such that it will satisfy the given test cases.

Solution:

#include<iostream>
#include<cstring>
#include<malloc.h>
using namespace std;
class String{
    char* _str;
    public:
        String(char* str) : _str(str){}
        operator char*() {
            char* t_str = (char*)malloc(sizeof(_str) + 7);
            strcpy(t_str, "Coding is ");
            strcat(t_str, _str);
            return t_str;
        }
};

These are Nptel Programming in Modern C++ Assignment 7 Answers


Question 3
Consider the following program. Fill in the blanks as per the instructions given below:
  • at LINE-1, complete the overload function header,
  • at LINE-2, complete the casting operator statement,
  • at LINE-3, complete the casting operator statement,
such that it will satisfy the given test cases.

Solution:

class ClassB{
    int b = 20;
    public:
        void display(){
            cout << b;
        }
        ClassB operator=(int x){ //LINE-1
            b = b + x;
        }
};


void fun(const ClassA &t, int x){
    ClassA &u = const_cast<ClassA&>(t); //LINE-2
    u.display();
    ClassB &v = reinterpret_cast<ClassB&>(u); //LINE-3
    v = x;
    v.display();
}

These are Nptel Programming in Modern C++ Assignment 7 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 7 Answers


Quiz

Q1. Consider the following code segment.
Fill in the blank at LINE-1 and LINE-2 with the same statement such that the program will print Rajan : 5000.

a) const_cast (this)
b) static_cast (this)
c) dynamic_cast (this)
d) (employee*) (this)

Answer: a, d


Q2. Consider the following code segment.
What will be virtual function table (VFT) for the class C?

a) C::f(C* const)
B::g(B* const)
C::h(C* const)
B::i(B* const)
b) A::f(A* const)
B::g(B* const)
C::h(C* const)
B::i(B* const)
c) A::f(A* const)
B::g(B* const)
B::h(B* const)
C::i(C* const)
d) A::f(A* const)
B::g(B* const)
C::h(C* const)
C::i(C* const)

Answer: a) C::f(C* const)
B::g(B* const)
C::h(C* const)
B::i(B* const)


These are Nptel Programming in Modern C++ Assignment 7 Answers


Q3. Consider the following code segment.
Which line/s will give compilation error?

a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4

Answer: c, d


Q4. Consider the following code segment.
Which of the following type-casting is permissible?

a) t2 = static_cast< Test2* >(t1);
b) t2 = dynamic_cast< Test2* >(t1);
c) t2 = reinterpret_cast< Test2* >(t1);
d) t2 = const_cast< Test2* >(t1);

Answer: c) t2 = reinterpret_cast< Test2* >(t1);


These are Nptel Programming in Modern C++ Assignment 7 Answers


Q5. Consider the following code segment.
What will be the output?

a) 0101
b) 0111
c) 0110
d) 0010

Answer: b) 0111


Q6. Consider the following code segment.
What will be the output?

a) 0101
b) 0111
c) 0110
d) 0010

Answer: c) 0110


These are Nptel Programming in Modern C++ Assignment 7 Answers


Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 9.81.

a) const_cast< double* >
b) static_cast< double* >
c) dynamic_cast< double* >
d) (const doubles)

Answer: a) const_cast< double* >


Q8. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print “C++”.

a) reinterpret._cast< C2* >
b) static_cast< C2* >
c) dynamic_cast< C2* >
d) (C2%)

Answer: a, d


These are Nptel Programming in Modern C++ Assignment 7 Answers


Q9. Consider the following code segment below.
How many virtual function table (VFT) will be created?

a) 1
b) 2
c) 3
d) 4

Answer: c) 3


Programming Assignment of Programming in Modern C++

Question 1

Complete the program with the following instructions.
• Fill in the blank at LINE-1 to complete operator overloading for assignment operator.
• Fill in the blanks at LINE-2 and LINE-3 to complete the type casting statements.
The program must satisfy the given test cases.

Solution:

void operator=(int x)
{
  b = b + x;
}
};
void fun(const Test1 &t, int x)
{
  Test1 &u = const_cast<Test1&>(t);
  u.show();
  Test2 &v = reinterpret_cast<Test2&>(u);

These are Nptel Programming in Modern C++ Assignment 7 Answers


Question 2

Consider the following program with the following instructions.
• Fill in the blank at LINE-1 to complete constructor definition.
• Fill in the blank at LINE-2 to complete assignment operator overload function signature.
• Fill in the blank at LINE-3 to complete integer cast operator overload function signature.
The program must satisfy the sample input and output.

Solution:

#include<iostream>
using namespace std;
class intString{
  int *arr;
  int n;
    mutable int index;
  public:
      intString(int k):n(k),arr(new int[k]),index(k-1){}
  operator int()const
  {
    return arr[index--];
  }
  intString& operator=(int &k){

These are Nptel Programming in Modern C++ Assignment 7 Answers


Question 3

Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 to complete the constructor definition,
• at LINE-2 to complete the function header to overload operator for type-casting to char,
• at LINE-3 to complete the function header to overload operator for type-casting to int,
such that it will satisfy the given test cases.

Solution:

#include<iostream>
#include<cctype>
using namespace std;
class classChar
{
  char ch;
  public:
  classChar(char _ch) : ch(tolower(_ch)){}
  operator char() const
  {
    return ch;
  }
  operator int() const
  {
    return ch - 'a' + 1;
  }

These are Nptel Programming in Modern C++ Assignment 7 Answers



These are Nptel Programming in Modern C++ Assignment 7 Answers

More Solutions of Programming in Modern C++: Click Here

More NPTEL Solutions: https://progiez.com/nptel/


These are Nptel Programming in Modern C++ Assignment 7 Answers

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