NPTEL 4 min read

NPTEL Programming In Modern C++ Week10 Assignment Solution 2023

Faheem Ahmad

1 read

NPTEL-Programming-In-Modern-C-Week10-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++ Week10 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 in C++11/14 to convert between feet and inch. Fill in the blanks as per the instructions given below:
   • at LINE-1 with appropriate header to function convert,
   • at LINE-2 with appropriate return statement convert.
C++
#include <iostream>
    
class feet; 
class inch{
    public: 
        inch(double i) : i_(i){}
        feet getValue();
        void show(){ std::cout << i_ << " "; }
    private:
        double i_;
};

class feet{
    public: 
        feet(double f) : f_(f){}
        inch getValue();
        void show(){ std::cout << f_ << " "; }
    private:
        double f_;
};

feet inch::getValue(){
    feet t(i_ / 12.0);
    return t;
}

inch feet::getValue(){
    inch t(f_ * 12.0);
    return t;
}

template <typename T> 

decltype(auto) convert(T val){   // LINE-1 

    return val.getValue();;      // LINE-2
}

int main(){
    double a, b;
    std::cin >> a >> b;
    feet f(a);
    inch i(b);
    inch i1 = convert(f); 
    feet f1 = convert(i);
    i1.show();
    f1.show();
    return 0;
}

Course Name : Programming In Modern C++ July 2023

Programming Assignment : Q2

Consider the following program in C++11/14. Fill in the blanks as per the instructions given below:
   • at LINE-1 with appropriate header and initialization list for the copy constructor,
   • at LINE-2 with appropriate header for copy assignment operator overload,
   • at LINE-3 with appropriate header and initialization list for the move constructor,
   • at LINE-4 with appropriate header for move assignment operator overload.
C++
#include <iostream>
#include <vector>
class number { 
    public:
        number(){}
        number(int i) : ip_(new int(i)) { } 
        number(const number& n) : ip_(new int(*(n.ip_) * 10)) { } // LINE-1: copy constructor 
        number& operator=(const number& n) { // LINE-2: copy assignment 
            if (this != &n) { 
                delete ip_;  
                ip_ = new int(*(n.ip_) * 10); 
            } 
            return *this;
        }
        ~number() { delete ip_; } 
        number(number&& n) : ip_(n.ip_) { n.ip_ = nullptr; }  // LINE-3: move constructor 
        number& operator=(number&& d) {  // LINE-4: move assignment 
            if (this != &d) { 
                ip_ = d.ip_; 
                d.ip_ = nullptr; 
            } 
            return *this;
        }
        
void show(){
            if(ip_ == nullptr)
                std::cout << "moved : ";
            else
                std::cout << *ip_ << " : ";
        }
        private:
            int* ip_ {nullptr};
};

int main(){
    int a;
    std::cin >> a;
    number n1(a);
    number n2 = n1;
    number n3;
    n3 = n1;
    n1.show();
    n2.show();
    n3.show();
    
    number n4 = std::move(n1);
    number n5;
    n5 = std::move(n1);
    n1.show();
    n4.show();
    n5.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
144
Reads
202
Writing since
2020
All posts by Faheem