Programming in Modern C++ Week 5 : Programming Assignment 1 2 and 3 Solutions

 

Week 5 : Programming Assignment 1

Due on 2022-03-03, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:

• at LINE-1 with appropriate initialization list to initialize the data members,
• at LINE-2 to call printContact(),

such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-02, 22:45 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Contact{
5
    private:
6
        int phone_no;
7
        string address;
8
    public:
9
        Contact(int phone_no_, string address_) : phone_no(phone_no_), 
10
                                                      address(address_){}
11
        void printContact(){
12
            cout << "phone: " << phone_no << endl;
13
            cout << "address: " << address << endl;
14
        }
15
};
16
17
class Student : private Contact{
18
    private:
19
        int roll_no;
20
        string name;
21
    public:
22
        Student(int roll_no_, string name_, int phone_no_, string address_) 
23
                        : Contact(phone_no_, address_){
24
                          roll_no=roll_no_;
25
                          name=name_;
26
                        }    //LINE-1
27
      void printContact()
28
      {
29
        Contact ::printContact(); //LINe-2
30
      }
0
        void print(){
1
            cout << "roll: " << roll_no << endl;
2
            cout << "name: " << name << endl;
3
        }
4
5
};
6
7
int main(){
8
    int a, b;
9
    string s1, s2;
10
    cin >> a >> b;
11
    cin >> s1 >> s2;
12
    Student s(a, s1, b, s2);
13
    s.print();
14
    s.printContact();
15
    return 0;
16
}
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
23 234529
Rajiv Delhi
roll: 23\n
name: Rajiv\n
phone: 234529\n
address: Delhi\n
roll: 23\n
name: Rajiv\n
phone: 234529\n
address: Delhi\n
Passed
Test Case 2
54 239990
Komal Mumbai
roll: 54\n
name: Komal\n
phone: 239990\n
address: Mumbai\n
roll: 54\n
name: Komal\n
phone: 239990\n
address: Mumbai\n
Passed



Week 5 : Programming Assignment 2

Due on 2022-03-03, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:

• at LINE-1 and LINE-3 with appropriate inheritance statement,
• at LINE-2 and LINE-4 with appropriate initialization lists,

such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-02, 22:51 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Vehicle{
5
    protected: 
6
        int no_of_wheels;
7
    public:
8
        Vehicle(int nw) : no_of_wheels(nw){}
9
        friend ostream& operator<<(ostream& os, const Vehicle& d); 
10
};
11
12
class Car : public Vehicle{    //LINE-1
13
    protected: 
14
        int no_of_passengers;
15
    public:
16
        Car(int nw, int np) : Vehicle(nw) {
17
          no_of_passengers=np;
18
        } //LINE-2
19
        friend ostream& operator<<(ostream& os, const Car& d); 
20
};
21
22
class Truck : public Vehicle {    //LINE-3
23
    protected: 
24
        int load_capacity;
25
    public:
26
        Truck(int nw, int lc) :  Vehicle(nw) {
27
        load_capacity=lc;
28
        } //LINE-4
0
        friend ostream& operator<<(ostream& os, const Truck& d); 
1
};
2
3
ostream& operator<<(ostream& os, const Vehicle& ob) { 
4
    os << "no_of_wheels: " << ob.no_of_wheels << endl;
5
    return os;
6
}
7
8
ostream& operator<<(ostream& os, const Car& ob) { 
9
    os << "no_of_wheels: " << ob.no_of_wheels << ", no_of_passengers: " 
10
                   << ob.no_of_passengers << endl;
11
    return os;
12
}
13
14
ostream& operator<<(ostream& os, const Truck& ob) { 
15
    os << "no_of_wheels: " << ob.no_of_wheels << ", load_capacity: " 
16
                << ob.load_capacity << endl;
17
    return os;
18
}
19
20
int main(){
21
    int i, j, k, l, m;
22
    cin >> i >> j >> k >> l >> m;
23
    Vehicle v(i);
24
    Car c(j, k);
25
    Truck t(l, m);
26
    cout << v << c << t;
27
    return 0;
28
}
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 4 6 20000
no_of_wheels: 2\n
no_of_wheels: 4, no_of_passengers: 4\n
no_of_wheels: 6, load_capacity: 20000\n
no_of_wheels: 2\n
no_of_wheels: 4, no_of_passengers: 4\n
no_of_wheels: 6, load_capacity: 20000\n
Passed
Test Case 2
3 4 6 8 50000
no_of_wheels: 3\n
no_of_wheels: 4, no_of_passengers: 6\n
no_of_wheels: 8, load_capacity: 50000\n
no_of_wheels: 3\n
no_of_wheels: 4, no_of_passengers: 6\n
no_of_wheels: 8, load_capacity: 50000\n
Passed







Week 5 : Programming Assignment 3

Due on 2022-03-03, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:

• at LINE-1, LINE-2 and LINE-3 with initialization list,
• at LINE-4, LINE-5 and LINE-6 with function definitions,

such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-03, 20:25 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 = 0);
8
        int sum(); 
9
};
10
11
class B : public A{
12
    int b;
13
    public:
14
        B(int _a = 0, int _b = 0);
15
        int sum(); 
16
};
17
18
class C : public B{
19
    int c;
20
    public:
21
        C(int _a = 0, int _b = 0, int _c = 0);
22
        int sum(); 
23
};
24
25
A::A(int _a) : a{_a}{}    //LINE-1
26
B::B(int _a, int _b) : A{_a}{b=_b;}  //LINE-2
27
C::C(int _a, int _b, int _c) : B{_a,_b}{c=_c;}    //LINE-3
28
int A::sum(){ return a;}    //LINE-4 
29
int B::sum(){ return(A::sum()+b) ;}    //LINE-5
30
int C::sum(){ return(B::sum()+c) ; }    //LINE-6
0
int main(){
1
    int a, b, c;
2
    cin >> a >> b >> c;
3
    A aObj(a);
4
    B bObj(a, b);
5
    C cObj(a, b, c);
6
    cout << aObj.sum() << ", " << bObj.sum() << ", " << cObj.sum();
7
    return 0;
8
}
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
10 20 30
10, 30, 60
10, 30, 60
Passed
Test Case 2
10 -10 10
10, 0, 10
10, 0, 10
Passed

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...