Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

Programming in Modern C++ Week 7 Programming Assignments [ Jan-2022  | NPTEL ]

Programming in Modern C++ Week 7 Programming Assignments [ Jan-2022 | NPTEL ]

  NPTEL » Programming in Modern C++



Subscribe to our YouTube Channel :  Swayam Solver



Week 7: Programming Assignment 1

Due on 2022-03-17, 23:59 IST
Consider the following program. Fill in the blanks at LINE-1, 2, and 3 with appropriate inheritance statement 
such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-17, 08:00 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
class Base {
4
    public:
5
        Base(int x = 0) { cout << x << " "; }
6
        void fun(int i){ cout << i << " "; }
7
};
8
class D1 : virtual public Base { // LINE-1 : Inherit from class Base
    public:
        D1(int x = 0) : Base(x+1) { fun(x+1); }
};
class D2 : virtual public Base { // LINE-2 : Inherit from class Base
public:
    D2(int x = 0) : Base(x+2) { fun(x+2); }
};
class DD : public D1, public D2 { // LINE-3 : Inherit from class D1 and D2
    public:
        DD(int i = 0) : D1(i*2), D2(++i) { fun(i); }
};
0
int main() {
1
    int i;
2
    cin >> i;
3
    DD obj(i);
4
    return 0;
5
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   

 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
0 5 5 3 
0 5 5 3 
Passed
Test Case 2
3
0 7 6 4 
0 7 6 4 
Passed


Private Test cases used for EvaluationStatus
Test Case 1
Passed





Week 7: Programming Assignment 2

Due on 2022-03-17, 23:59 IST
Consider the following program. Fill in the blanks at LINE-1 and 2 with appropriate cast statement,
such that it will satisfy the given test cases. 
Your last recorded submission was on 2022-03-17, 08:06 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
class Base{
4
    public:
5
        virtual void f(int a){
6
            cout << a+1 << " ";
7
        }
8
};
9
class Derived : public Base{
10
    public:
11
        virtual void f(int a){
12
            cout << a+2 << " ";
13
        }
14
};
15
int main() {
16
    int x;
17
    cin >> x;
18
    Base b;
19
    Derived d;
20
   try{
        Base& ra1 = static_cast<Base&>(d); //LINE-1
        ra1.f(x);
        Derived& rb1 = static_cast<Derived&>(b); //LINE-2
        rb1.f(x);
    }
0
    catch(exception& e){
1
2
        cout << e.what();
3
    }
4
    return 0;
5
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
4 3 
4 3 
Passed
Test Case 2
10
12 11 
12 11 
Passed



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 7: Programming Assignment 3

Due on 2022-03-17, 23:59 IST
Consider the following program. Fill in the blanks at LINE-1 and 2 with appropriate casting operator such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-17, 08:22 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class A{
5
    int a;
6
    public:
7
        A(int _a) : a(_a) { }
8
        void print(){
9
            cout << a << " ";
10
        }
11
};
12
class B{
13
    int b;
14
    public:
15
        B(int _b) : b(_b) { }
16
        void print(){
17
            cout << b;
18
        }
19
        void operator=(int x){ 
20
            b = b + x;
21
        }
22
};
23
void fun(const A &t){
    int x;
    cin >> x;
    A &u = const_cast<A &>(t); //LINE-1
    u.print();
    B &v = reinterpret_cast<B &>(u); //LINE-2
    v = x;
    v.print();
}
0
int main(){
1
    int a;
2
    cin >> a;
3
    A t1(a);
4
    fun(t1);
5
    return 0;
6
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   

 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2 4
2 6
2 6
Passed
Test Case 2
5 1
5 6
5 6
Passed



Private Test cases used for EvaluationStatus
Test Case 1
Passed


Please subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to continue making quality content...

Have a great day !

Watch the video to see programs in motion


1 comment:

  1. please post the answers for week 8 programming assignments

    ReplyDelete

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.