SystemVerilog Datatype: Class (Part 2)
Previous: Doing more with constructors | Next: Inheritance and scope
Inheritance
It is quite common for a class definition to contain a property
that is another class type. For example, we can define a new
class Trapezoid that has two class properties - one is
an object of type Triangle and another a structure type
point.
class Trapezoid;
// class properties
Triangle t1;
point p4;
... // rest of the definition
endclass
While the above works, there is a better way of handling this,
which makes accessing t1 from a variable of type
Trapezoid much easier. To see how to do this, note that,
Trapezoid is a specialization of Triangle, i.e.,
Trapezoid contains all that Triangle does, and then
some more. Or, to put it differently, Trapezoid 'extends'
the class Triangle and 'inherits' its (i.e., Triangle's)
members. Triangle is said to be the parent (or, base) class
of Trapezoid and Trapezoid a subclass (or, derived
class) Triangle. This is how it looks in formal syntax:
class Trapezoid extends Triangle;
point p4;
... // rest of the definition
endclass
Previous: Doing more with constructors | Next: Inheritance and scope
|