NPTEL 3 min read Updated Sep 2023

NPTEL Programming In Modern C++ Week 7 Programming Assignment

Faheem Ahmad

1 read

Programming-In-Modern-C-Week7-Programming-Assignment-Solutions

There has been a continual debate on which programming language/s to learn, to use. As the latest TIOBE Programming Community Index for August 2021 indicates – C (13%), Python (12%), C++ (7%), Java (10%), and C#(5%) together control nearly half the programming activities worldwide. Further, C Programming Language Family (C, C++, C#, Objective C etc.) dominate more than 25% of activities. Hence, learning C++ is important as one learns about the entire family, about Object-Oriented Programming and gets a solid foundation to also migrate to Java and Python as needed. C++ is the mother of most general purpose of languages. It is multi-paradigm encompassing procedural, object-oriented, generic, and even functional programming. C++ has primarily been the systems language till C++03 which punches efficiency of the code with the efficacy of OOP. Then, why should I learn it if my primary focus is on applications? This is where the recent updates of C++, namely, C++11 and several later offer excellent depths and flexibility for C++ that no language can match. These extensions attempt to alleviate some of the long-standing shortcomings for C++ including porous resource management, error-prone pointer handling, expression semantics, and better readability. The present course builds up on the knowledge of C programming and basic data structure (array, list, stack, queue etc.) to create a strong familiarity with C++98 / C++03. Besides the constructs, syntax and semantics of C++ (over C), we also focus on various idioms of C++ and attempt to go to depth with every C++ feature justifying and illustrating them with several examples and assignment problems. On the way, we illustrate various OOP concepts. The course also covers important advances in C++ 11 and later released features..

Programming In Modern C++ Week 7 Programming Assignment.

INTENDED AUDIENCE :  Any interested audience
PREREQUISITES :  10th standard/high school
INDUSTRY SUPPORT :  Programming in C++ is so fundamental that all companies dealing with systems as well as application development (including web, IoT, embedded systems) have a need for the same. These include – Microsoft, Samsung, Xerox, Yahoo, Oracle, Google, IBM, TCS, Infosys, Amazon, Flipkart, etc. This course would help industry developers to be up-to-date with the advances in C++ so that they can remain at the state-of-the-art.

Course Layout

Week 1: Programming in C++ is Fun.
Week 2: C++ as Better C.
Week 3: OOP in C++.
Week 4: OOP in C++.
Week 5: Inheritance.
Week 6: Polymorphism.
Week 7: Type Casting.
Week 8: Exceptions and Templates.
Week 9: Streams and STL.
Week 10: Modern C++.
Week 11: Lambda and Concurrency.
Week 12: Move, Rvalue and STL Containers

Programming Assignment Q1

Complete the program with the following instructions:
  • Fill in the blank at LINE-1 to complete the parameterized constructor definition,
  • Fill in the blanks at LINE-2 to complete cast operator overloading function to typecast to IP1 type.
C++
#include <iostream>
using namespace std;
class IP1 {
    int i;

    public:
        IP1(int ai) : i(ai) {}
        int get() const { return i; }
        void update() { i *= 10; }
};

class IP2 {
    int i;
    public:
        IP2(int ai) : i(ai) {}
        int get() const { return i; }

        IP2(IP1& a) : i(a.get()) {} //LINE-1

        operator IP1() { return IP1(i); } //LINE-2

        void update() { i *= 20; }
};

int main() {
    int i;
    cin >> i;
    IP1 a(i+2);
    IP2 b(i);
    const IP2 &r = static_cast<IP2>(a);
    a.update();
    cout << a.get() << ":";    
    cout << r.get() << ":";
    const IP1 &s = static_cast<IP1>(b);
    b.update();
    cout << b.get() << ":";
    cout << s.get() << ":";
    return 0;
}

Programming Assignment Q2

Consider the following program with the following instructions.
  • Fill in the blank at LINE-1 to complete assignment operator overload function header.
  • Fill in the blanks at LINE-2 and LINE-3 to complete statements with the appropriate casting operator.
C++
#include<iostream>
using namespace std;
class myClassA{
    int a = 5;

    public:
        void print(){
            cout << a << " ";
        }
};

class myClassB{
    int b = 10;
    public:
        void print(){
            cout << b;
        }

        myClassB operator=(int x){ //LINE-1
            b = b * x;
        }
};
void fun(const myClassA &t){
    int x;
    cin >> x;
    myClassA &u = const_cast<myClassA&>(t); //LINE-2
    u.print();
    myClassB &v = reinterpret_cast<myClassB&>(u); //LINE-3
    v = x;
    v.print();
}

int main(){
    myClassA t1;
    fun(t1);
    return 0;
}

Programming Assignment Q3

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 char* operator overload function header
C++
#include<iostream>
#include<cstring>
#include<malloc.h>
using namespace std;
class String{
    char* _str;
public:

    String (char* str) : _str(str){} //LINE-1

    operator char *(){ //LINE-2

        char* t_str = (char*)malloc(sizeof(_str) + 7);
        strcpy(t_str, "Welcome ");
        strcat(t_str, _str);
        return t_str;
    }
};

int main(){
    char s[15];
    cin >> s;
    String st = static_cast<String>(s);
    cout << static_cast<char*>(st);
    return 0;
}

// written by

Faheem Ahmad

Writer at Techies Talk

Faheem writes about NPTEL, Coursera, and How To on Techies Talk, turning tricky topics into guides you can follow step by step.

Writes about #NPTEL117 #Coursera13 #How To5

Articles
144
Reads
202
Writing since
2020
All posts by Faheem