how are ADTs useful?

I am a total beginner at C++ and I recently came across ADTs in polymorphism. Now, to professional programmers, this might sound like a stupid question, but here is confusion.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// experimenting with ADT :D :D
#include<iostream>
using namespace std;

class calculate { //ADT
      
      public:
             
      virtual int area() = 0;
};

class quad : public calculate { // calculating quadrilaterals.
      
      int width;
      int length;
      
      public:
      
      void set(int w, int l) { width = w;  length = l; }
      int area() { return( width * length); } 
      
};

class trap  : public calculate { // calculating trapeziums/trapezoid


       int base;
       int height;
       int measure;
       
       public:
              
              
       void set(int a,int b) { measure = a; base = b; }
         
       int area() { return ((measure + base) / 2 )* height ; }
       
};


class triangle  : public calculate {
      

      int base;
      int height; 
      
      public:
             
      void set(int h, int b) { height = h; base = b; }
             
      int area() { return( ( height * base ) / 2 ); }
};


      

int main() {
    
    
    
    
    int choice;
    
    cout << "1.Rectangle" << endl;
    cout << "2.Trapezium" << endl;
    cout << "3.Triangle, rhombus, kite" << endl;
    cin >> choice;
    
    if( choice == 1) { // if the user picks '1'
        
        int dimention_1;
        int dimention_2;
        
        quad shape1;
        
        cout << "please type in the width of this rectangle." << endl;
        cin >> dimention_1;
        
        cout << "please type in the length of this rectangle." << endl;
        cin >> dimention_2;
        
        shape1.set(dimention_1, dimention_2);
        calculate * ppt_shape1 = &shape1;
        
        cout << ppt_shape1 -> area() << endl;
        }
        
        
           
  else  if( choice == 2) { // if the user picks '2'.
        
        int dimention_1;
        int dimention_2;
        int dimention_3;
        
        trap shape1;
        
        cout << "please type in the 'a' of this trapezium." << endl;
        cin >> dimention_1;
        
        cout << "please type in the 'height' of this trapezium." << endl;
        cin >> dimention_2;
        
        cout << "please type in the 'base' of this trapezium." << endl;
        cin >> dimention_3;
        
        
        shape1.set(dimention_1, dimention_2);
        calculate * ppt_shape1 = &shape1;
        
        cout << ppt_shape1 -> area() << endl;
        }
         
                   
 else   if( choice == 3) { // if the user picks '3'
        
        int dimention_1;
        int dimention_2;

        triangle shape1;
        
        cout << "please type in the 'base' of this triangle/rhombus/kite." << endl;
        cin >> dimention_1;
        
        cout << "please type in the 'height' of this triangle/rhombus/kite." << endl;
        cin >> dimention_2;
        
     
        
        
        shape1.set(dimention_2,dimention_1);
        calculate * ppt_shape1 = &shape1;
        
        cout << ppt_shape1 -> area() << endl;
        }
    
        
    cin.get();
    cin.get();
    return 0;
}
       


- code with ADTs and polymorphism
now, I get the concept off overriding classes, but what about this code-


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

#include<iostream>
using namespace std;

class cat {
      
      public:
             
           void cry() { cout << "MEOOOOOOOOOOOOOOOOOOW!" << endl; }
             
};

class dog {
      
      public:
             
           void cry() { cout << "WOOOOOOOOOOOOOOOOOOOF!" << endl; } // you can see 'cry' in both classes doesn't cause a compiler error.
             
};

int main() {
    
    
    cat kitty;
    dog puppy;
    
    kitty.cry();
    puppy.cry();
    
cin.get();
cin.get();
return 0;
}




- you can see by this code that you can have functions of same name within different classes and it wouldn't cause a complier error. So then, do we actually need to use ADTs and polymorphism? why can't we just name 'set' and 'getarea' same with all classes without using ADT? would it cause problems?

I know I seem very ignorant, but please, enlighten me.
A stands for Abstract: it is useful when there is an abstraction.

Given your cat and dog, a cat is a pet. A dog is also a pet. If you have no "just" pets, every pet is actually a cat or a dog, then "pet" is an abstraction.

You can then write "class cat : public pet" and "class dog : public pet", and if, in your universe, every pet can cry, then you can define "virtual void cry() = 0" as a public member function of class pet.

Then, if you have a part of the program that uses the abstraction, e.g. some function, say hurt(), that takes a pet and makes it cry, you'd have to write it once, without having to write an individual hurt() function for every kind of pet you have:

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
#include<iostream>

using namespace std;

class pet {
 public:
    virtual void cry() = 0;
};

class cat : public pet {
 public:
    void cry() { cout << "MEOOOOOOOOOOOOOOOOOOW!\n"; }
};

class dog : public pet {
 public:
    void cry() { cout << "WOOOOOOOOOOOOOOOOOOOF!\n"; }
};

void hurt(pet& p)
{
    p.cry();
}

int main()
{
    cat kitty;
    dog puppy;
    hurt(kitty);
    hurt(puppy);
}
Last edited on
ohhh so it makes things easier?
Topic archived. No new replies allowed.