Template problem

Im getting errors at line 32 saying expected class name, and another error at line 53 saying use of class b requires template argument. How can i Fix this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  /*
 Modify the definitions of the above two classes to template classes, i.e., “int valuea;”   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:
 
 You should use cout to print out all the results on computer screen and make corresponding screen shots.
 */
#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():
    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;
    //(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;
}
closed account (SECMoG1T)
Line 15 should return a while 38 should return b.

1
2
3
4
5
6
B obj;///line 57 creating this object calls thr default constructor which in your case is on line
   ///44 , your default constructor calls cout of the template variable valueb which isnt  yet provided
   /// so your default constructor will never let you declare a class B object without a template <type>
   /// make you d constructor intialize the object instead plus 
   B <type> obj;/// this is how you should create your objs where type can be int, string etc.
 
Last edited on
Topic archived. No new replies allowed.