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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143`` ``````// experimenting with ADT :D :D #include 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

 ``12345678910111213141516171819202122232425262728293031323334353637`` `````` #include 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:

 ``12345678910111213141516171819202122232425262728293031`` ``````#include 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.