NOTE : -
THIS CONTENT IS NOT ORIGINAL
29. Class derivation allows you to write generic methods
Class derivation allows you to construct more complex classes built from base classes. There is another application of class derivation: allowing the programmer to write generic functions.
Suppose you define a base class with no variables. It makes no sense to use instances of that class inside your program. But then you write a function whose purpose it is to sort instances of that class. That function will be able to sort any type of object provided it belongs to a class derived from that base class! The only condition is that inside of each derived class definition, all methods that the sort function needs are correctly defined:
Perhaps you think "okay, that's a good idea to derive classes from the class octopus because that way I can apply it to instances of my class's methods and function that were designed in a generic way for the octopus class. But what if there's another base class, named cuttlefish, which has very interesting methods and functions too? Do I have to make my choice between octopus and cuttlefish when I want to derive a class?" No, of course not. A derived class can be derived from bothoctopus and cuttlefish. That's POLYMORPHISM. The derived class simply has to define the methods necessary for octopus together with the methods necessary for cuttlefish:
Class derivation allows you to construct more complex classes built from base classes. There is another application of class derivation: allowing the programmer to write generic functions.
Suppose you define a base class with no variables. It makes no sense to use instances of that class inside your program. But then you write a function whose purpose it is to sort instances of that class. That function will be able to sort any type of object provided it belongs to a class derived from that base class! The only condition is that inside of each derived class definition, all methods that the sort function needs are correctly defined:
using namespace std;
#include
#include
class octopus
{
public:
virtual double module() = 0; // = 0 implies function is not
// defined. This makes instances
// of this class cannot be declared.
};
double biggest_module (octopus &a, octopus &b, octopus &c)
{
double r = a.module();
if (b.module() > r) r = b.module();
if (c.module() > r) r = c.module();
return r;
}
class vector: public octopus
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x * x + y * y);
}
};
class number: public octopus
{
public:
double n;
number (double a = 0)
{
n = a;
}
double module()
{
if (n >= 0) return n;
else return -n;
}
};
int main ()
{
vector k (1,2), m (6,7), n (100, 0);
number p (5), q (-3), r (-150);
cout << biggest_module (k, m, n) << endl;
cout << biggest_module (p, q, r) << endl;
cout << biggest_module (p, q, n) << endl;
return 0;
}
| Output |
| 100 150 100 |
Perhaps you think "okay, that's a good idea to derive classes from the class octopus because that way I can apply it to instances of my class's methods and function that were designed in a generic way for the octopus class. But what if there's another base class, named cuttlefish, which has very interesting methods and functions too? Do I have to make my choice between octopus and cuttlefish when I want to derive a class?" No, of course not. A derived class can be derived from bothoctopus and cuttlefish. That's POLYMORPHISM. The derived class simply has to define the methods necessary for octopus together with the methods necessary for cuttlefish:
class octopus
{
virtual double module() = 0;
};
class cuttlefish
{
virtual int test() = 0;
};
class vector: public octopus, public cuttlefish
{
double x;
double y;
double module ()
{
return sqrt (x * x + y * y);
}
int test ()
{
if (x > y) return 1;
else return 0;
}
}
BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:
Post a Comment