NPTEL Programming In Modern C++ Week6 Assignment Solution 2023

NPTEL-Programming-In-Modern-C-Week6-Assignment-Solution-2023

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++ Week6 Programming Assignment July 2023

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

Course Name : Programming In Modern C++ July 2023

Programming Assignment : Q1

Consider the following program. Fill in the blanks as per the instructions given below:
   • Complete the destructor statement,
   • Complete the constructor statement
				
					#include<iostream>
using namespace std;
class B{
public:
    B(){ cout << "1 "; }
    B(double n){ cout << n << " "; }

    virtual ~B(); //LINE-1
};

class D : public B{
public:

    D(double n) : B(n)     //LINE-2
{ cout << n * 3 << " "; } 
    D(){ cout << "3 "; }
    virtual ~D(){ cout << "4 "; }
};
B::~B(){ cout << "2"; }
int main(){
    int i;
    cin >> i;
    B *pt = new D(i);
    delete pt;
    return 0;
}
				
			

Course Name : Programming In Modern C++ July 2023

Programming Assignment : Q2

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
				
					#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 << " ";
    }
};

void ReTest1::fun(){
    cout << d1 << " ";
}
int main(){
    int i;
    cin>>i;
    Test *t1 = new ReTest1(i);
    Test *t2 = new ReTest2(i);
    t1->fun();
    t2->fun();
    return 0;
}
				
			

Course Name : Programming In Modern C++ July 2023

Programming Assignment : Q3

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
				
					#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 << " ";
}

void triangle::Area(){ cout << (0.5 * a * b); }
int main(){
    int a, b;
    cin >> a >> b;
    shape *sp = new triangle(a, b);
    sp->show();
    return 0;
}