NPTEL 4 min read

NPTEL Programming In Modern C++ Week3 Programming Assignment 2024

Faheem Ahmad

0 reads

NPTEL-Programming-In-Modern-C-Week3-Programming-Assignment-2024

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++ Week3 Programming Assignment 2024

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 In Modern C++ 2024

Programming Assignment Q1 :

Consider the program below.
• Fill in the blank at LINE-1 to complete the parameterized constructor.
• Fill in the blank at LINE-2 to complete the copy constructor.
• Fill in the blank at LINE-3 to complete the appropriate function header.
The program must satisfy the given test cases.

C++
#include<iostream>
#include<cmath>
using namespace std;
class Complex_num{
    const int x,y;
    public:
        Complex_num(int _x=0, int _y=0) : x(_x), y(_y) {} //LINE-1
        Complex_num(const Complex_num& c) : x(c.x), y(c.y) {} //LINE-2
        Complex_num addition(const Complex_num& c) const {
            return Complex_num(x+c.x, y+c.y); //LINE-3
        }
        
        void print(){ cout << "(" << x << ", " << y << ")" << endl; }
};
int main(){
    int x1,x2,y1,y2;
    cin >> x1 >> y1 >> x2 >> y2;
    Complex_num c1(x1,y1), c2(x2,y2);
    Complex_num c3 = c1.addition(c2);
    c3.print();
    return 0;
}

Programming In Modern C++ 2024

Programming Assignment Q2 :

Consider the following program.
• Fill in the blank at LINE-1 with the appropriate constructor statement.
• Fill in the blank at LINE-2 with the appropriate destructor statement so that the dynamic memory allocated in the constructor can be properly deleted.
The program must satisfy the sample input and output.

C++
#include<iostream>

using namespace std;

class CharArray{
    char *arr;
    int size;

    public:
        CharArray(int n) : size(n), arr(new char[n]){} //LINE-1

        ~CharArray(){ delete[]arr; } //LINE-2
        
        void EnterEle(){
            for(int i=0;i<size;i++)
                cin >> arr[i];
        }
        void FindMax(){
            char max = -1;
            for(int i=0;i<size;i++){
                if(max < arr[i])
                    max = arr[i];
            }
            cout << "Max: " << max;
        }
};
int main(){
    int n;
    cin >> n;
    CharArray a(n);
    a.EnterEle();
    a.FindMax();
    return 0;
}

Programming In Modern C++ 2024

Programming Assignment Q3 :

Consider the following program.
• Fill in the blanks at LINE-1 to declare the data member z.
• Fill in the blanks at LINE-2 to complete the change() function header.
• Fill in the blanks at LINE-3 to complete the print() function header.
The program must satisfy the sample input and output.

C++
#include<iostream>
using namespace std;
class Constant{
    int x, y;
    mutable int z; //LINE-1
    public:
        Constant(int _x, int _y) : x(_x), y(_y){}

        void change() const { z = x * y; }; //LINE-2

        void print() const { //LINE-3
            cout << "x = " << x << ", y = " << y << ", z = " << z;
        }
};

int main(){
    int i, j;
    cin >> i >> j;
    const Constant m(i, j);
    m.change();
    m.print();
    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
120
Writing since
2020
All posts by Faheem