Please follow all instructions on the picture. Thank you in advance. Given the f
ID: 3801840 • Letter: P
Question
Please follow all instructions on the picture. Thank you in advance.
Given the following segments of code, add constructor in the derived class B and the base class A, and test it using a main routine. Class A {int valuea: public: int getvaluea () const {return valuea;) void setvaluea (int x) {value = x;)//copy constructor}; class B: public A {int valueb public: int getvalueb () const return value;) void setvalueb (int x) {valueb = x;}//copy constructor}; Modify the definitions of the above two classes to template classes, i.e int value a; and "int valueb should be changed to a generic data type. After the modification, you need to test these two template classes with the following test cases: Create an instance of class B with float data type (valuea = 1.34 and valueb = 3.14) Create an instance of class B with integer data type (valuea = 1 and valueb = 3) Create an instance of class B with char data type (valuea = 'a' and valueb = c') Create an instance of class B with string data type (valuea = "good" and valueb = "morningExplanation / Answer
#include <iostream>
using namespace std;
struct Date //definition of structure
{
int day;
int month;
int year;
};
//template base and derived classes
template <class type> class A
{
private:
type valuea;
public:
A(){}
type getValuea()const {return valuea; }
void setValuea(type x) {valuea = x;}
A(type obj)
{
valuea = obj.valuea;
}
};
template <class type> class B:public A<type>
{
private:
type valueb;
public:
type getValueb()const {return valueb; }
void setValueb(type x) {valueb = x;}
B(){}
B(type obj)
{
valueb = obj.valueb;
}
};
int main()
{
//objects of different types
B<float> obj1;
obj1.setValuea(1.34);
obj1.setValueb(3.14);
cout<<" obj1 : valuea = "<<obj1.getValuea()<<" valueb : "<<obj1.getValueb();
B<int> obj2;
obj2.setValuea(1);
obj2.setValueb(3);
cout<<" obj2 : valuea = "<<obj2.getValuea()<<" valueb : "<<obj2.getValueb();
B<char> obj3;
obj3.setValuea('a');
obj3.setValueb('c');
cout<<" obj3 : valuea = "<<obj3.getValuea()<<" valueb : "<<obj3.getValueb();
B<string> obj4;
obj4.setValuea("good");
obj4.setValueb("morning");
cout<<" obj4 : valuea = "<<obj4.getValuea()<<" valueb : "<<obj4.getValueb();
struct Date d1 = {27,10,2015};
struct Date d2 = {2,11,2015};
struct Date d3,d4;
B<struct Date> obj5;
obj5.setValuea(d1);
obj5.setValueb(d2);
d3 = obj5.getValuea();
d4 = obj5.getValueb();
cout<<" obj5 : valuea = "<<d3.day<<" "<<d3.month<<" "<<d3.year<<" valueb : "<<d4.day<<" "<<d4.month<<" "<<d4.year;
return 0;
}
Output:
obj1 : valuea = 1.34 valueb : 3.14
obj2 : valuea = 1 valueb : 3
obj3 : valuea = a valueb : c
obj1 : valuea = good valueb : morning
obj1 : valuea = 27 10 2015 valueb : 2 11 2015