Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

program2 code: #include <iostream> #include <string> using namespace std; templa

ID: 3762319 • Letter: P

Question

program2 code:

#include <iostream>
#include <string>
using namespace std;

template <class a>
class A
{
a valuea;
public:
int getValuea() const
{
return valuea;
}
void setValuea (a x)
{
valuea = x;
}
A()
{
cout << valuea;
}; // copy constructor
struct data
{
int day;
int month;
int year;
};
};

template <class b>
class B :public A
{
b valueb;
public:
int getValueb() const
{
return valueb;
}
void setValueb (b x)
{
valueb = x;
};
B()
{
cout << valueb;
};// copy constructor
struct data
{
int day;
int month;
int year;
};
};

int main()
{
//B obj;
B <> obj;
//(1)   Create an instance of class B with float data type (valuea = 1.34 and valueb = 3.14)
cout << "1. " << obj.getvaluea(1.34) << ", " << obj.getvalueb(3.14) << endl;

//(2)   Create an instance of class B with integer data type (valuea = 1 and valueb = 3)
cout << "2. " << obj.getvaluea(1) << ", " << obj.getvalueb(3) << endl;

//(3)   Create an instance of class B with char data type (valuea = ‘a’ and valueb = ‘c’)
cout << "3. " << obj.getvaluea('a') << ", " << obj.getvalueb('c') << endl;

//(4)   Create an instance of class B with string data type (valuea = “good” and valueb = “morning”)
cout << "4. " << obj.getvaluea("good") << ", " << obj.getvalueb("morning") << endl;

/*(5)   Create an instance of class B with Date data type (valuea = {27,10,2014} an valueb{2,11,2014}).
Date is a struct and defined as
struct Date
{
int day;
int month;
int year;
};
*/

cout << "5. " << obj.data(27,10,2014) << ", " << obj.data(2,11,2014) << endl;
return 0;
}

Explanation / Answer

In copy constructor you have to use a reference varibale for the class object