NPTEL programming in c++ Week 7 assignment Prpgrams 2021

 NPTEL programming in c++ Week 7 assignment Prpgrams 2021

Que-1-Consider the following program. Fill in the blanks at LINE-1 and LINE-2 with appropriate function headers such that it would satisfy the given test cases.

Code- 
#include<iostream>
#include<cstring>
#include<malloc.h>
using namespace std;
class StringType{
    char* _str;
    public:
    StringType(char* str) : _str(str){}    //LINE-1
    operator char*() {                //LINE-2
    char* t_str = (char*)malloc(sizeof(_str) + 7);
            strcpy(t_str, "hello ");
            strcat(t_str, _str);
            return t_str;
        }
};
int main(){
    char s[20];
    cin >> s;
    StringType st = static_cast<StringType>(s);
    cout << static_cast<char*>(st);
    return 0;
}

Que-2-Consider the following code snippet. Fill in the blanks at LINE-1, LINE-2 and LINE-3 with appropriate header such that it matches the given test cases.
code- 
#include<iostream>
#include<cctype>
using namespace std;

class Char{
    char ch;
    public:
        Char(char _ch): ch(toupper(_ch)){}    //LINE-1
        operator char(){ return ch; }        //LINE-2
        operator int(){ return ch - 'A'; }  //LINE-3
};

int main(){
    char c;
    cin >> c;
    Char cb = c;
    cout << (char)cb << ": position is " << int(cb);
    return 0;
}
Que-3- Consider the following program. Fill in the blank at LINE-1, LINE-2, LINE-3 and LINE-4 with appropriate inheritance statements such that it satisfies the given test cases.
code-
#include<iostream>
using namespace std;
class base {
    public:
        base(int i) { cout << 50 * i << " "; }
        base() { cout << 1 << " "; }
};
class derived1 : virtual public base {    //LINE-1
    public:
        derived1(int i);
};
class derived2 :virtual  public base {     //LINE-2
    public:
        derived2(int i);
};
class derived3 : virtual public base {    //LINE-3
    public:
        derived3(int i);
};

class derived_derived      
    :  public derived2, public derived1, public derived3{    //LINE-4
public:
        derived_derived (int i) : derived1(i), derived2(i), derived3(i) {
            cout << 10 * i << " ";
        }
};
derived1::derived1(int i) : base(i) {
    cout << 40 * i << " ";
}
derived2::derived2(int i) : base(i) {
    cout << 30 * i << " ";
}
derived3::derived3(int i) : base(i) {
    cout << 20 * i << " ";
}
int main() {
    int i = 0;
    cin >> i;
    derived_derived dd(i);
    return i;
}

Que-4-Consider the following program. Fill in the blank at LINE-1 with declaration of pure virtual function calculate(). Fill in the blanks at LINE-2, LINE-3 and LINE-4 with appropriate inheritance statement such that it satisfies the given test cases.
code-
#include<iostream>
using namespace std;

class math{
    protected:
        int _v1, _v2;
    public:
        math(int v1, int v2) : _v1(v1), _v2(v2){}
virtual int calculate(){}    //LINE-1
};

class sum :virtual public math{    //LINE-2
    public:
        sum(int v1, int v2) : math(v1, v2){}
        int calculate() { return (_v1 + _v2); }
};

class subtraction : virtual public math{     //LINE-3
    public:
        subtraction(int v1, int v2) : math(v1, v2) {}
        int calculate() { return (_v1 - _v2); }
};

class multiplication : virtual public math{     //LINE-4
 public:
        multiplication(int v1, int v2) : math(v1, v2) {}
        int calculate() { return (_v1 * _v2); }
};
int main(){
    int a, b;
    cin >> a >> b;
    math* m[3];
    m[0] = dynamic_cast<math*>(new sum(a, b));
    m[1] = dynamic_cast<math*>(new subtraction(a, b));
    m[2] = dynamic_cast<math*>(new multiplication(a, b));
    for(int i = 0; i < 3; i++){
        cout << m[i]->calculate() << " ";
    }
    return 0;
}

No comments

Powered by Blogger.