NPTEL 3 min read Updated Sep 2023

NPTEL Programming In Modern C++ Week5 Assignment Solution 2023

Faheem Ahmad

0 reads

NPTEL-Programming-In-Modern-C-Week5-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++ Week5 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 inheritance statement at LINE-1,
   • Complete the return statement at LINE-2 and LINE-3 to calculate the volume and surface area of a cube.
C++
#include <iostream>
using namespace std;
class Volume {
    public:
        double getValue(int a) { return (a * a * a); }
};
class SurfaceArea {
    public:
        double getValue(int a) { return 6 * a * a; }
};

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

int main() {
    int a;
    cin >> a;
    Cube c(a);
    cout << c.getVolume() << ", " << c.getSurfaceArea();
    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 with appropriate keyword,
   • at LINE-2 and LINE-3 with appropriate constructor statements
C++
#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&); //LINE-1
};
class Twowheeler : public Vehicle{
    public:
        Twowheeler(string n) : Vehicle(n, 2) { } //LINE-2
};
class Fourwheeler : public Vehicle{
    public:
        Fourwheeler(string n) : Vehicle(n, 4) { } //Line-3
};

void vehicleDetails(const Vehicle &v){
    cout << v.vehicleName << ": ";
    if(v.noOfWheels == 2)
        cout << "Two Wheeler";
    else if(v.noOfWheels == 4)
        cout << "Four Wheeler";
}
int main(){
    string s;
    int n;
    Vehicle *v;
    cin >> s >> n;
    if(n==2)
        v = new Twowheeler(s);
    else if(n==4)
        v = new Fourwheeler(s);
    vehicleDetails(*v);
    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 with appropriate inheritance statement,
    • at LINE-2 with appropriate constructor statement
C++
#include<iostream>
using namespace std;
class B1{
    protected:
        int b1;
    public:
        B1(int b) : b1(b){}
};
class B2{
    protected:
        int b2;
    public:
        B2(int b) : b2(b){}
};

class D : public B1, public B2 { //LINE-1
    int d;
    public:
        D(int x) : B1(x+5), B2(x+10), d(x) {} //LINE-2
    void show(){
            cout << d << ", " << b1 << ", " << b2;
        }
};
int main(){
    int x;
    cin >> x;
    D t1(x);
    t1.show();
    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
145
Reads
344
Writing since
2020
All posts by Faheem