class advanced new

20
Class Advanced

Upload: gokul-t

Post on 07-Jul-2016

219 views

Category:

Documents


5 download

DESCRIPTION

Introduction to Systemverilog classes.

TRANSCRIPT

Page 1: Class Advanced New

Class Advanced

Page 2: Class Advanced New

Polymorphism Abstract Class Scope resolution operator External declaration Multiple Inheritance

Page 3: Class Advanced New

Polymorphism allows an entity to take a variety of representations.

Polymorphism means the ability to request that the same Operations be performed by a wide range of different types of things.

Effectively, this means that you can ask many different objects to perform the same action.

Polymorphism allows the redefining of methods for derived classes while enforcing a common interface.

To achieve polymorphism the 'virtual' identifier must be used when defining the base class and method(s) within that class.

Polymorphism

Page 4: Class Advanced New

class A ; task disp (); $display(" This is class A "); endtask endclass

class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass

program main ; EA my_ea; A my_a;

initial begin my_a = new(); my_a.disp();

my_ea = new();

my_a = my_ea;

my_a.disp(); end endprogram RESULTS

This is class A This is class A

Class Advanced

Without virtual

Page 5: Class Advanced New

class A ; virtual task disp (); $display(" This is class A "); endtask endclass

class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass

program main ; EA my_ea; A my_a;

initial begin my_a = new(); my_a.disp();

my_ea = new();

my_a = my_ea; my_a.disp(); end endprogram

RESULTS

This is class A This is Extended class A

With Virtual

Class Advanced

Page 6: Class Advanced New

The methods which are added in the subclasses which are not in the parent class canot be accessed using the parent class handle.

This will result in a compilation error. The compiler check whether the method is existing the parent class definition or not.

Note:

Page 7: Class Advanced New

class A ; endclass

class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass

program main ; EA my_ea; A my_a;

initial begin my_ea = new(); my_a = my_ea; my_ea.disp(); my_a.disp(); end endprogram

Member disp not found in class A

example

Class Advanced

Page 8: Class Advanced New

With inheritance we are able to force a subclass to offer the same properties like their superclasses. Consequently, objects of a subclass behave like objects of their superclasses.

Sometimes it make sense to only describe the properties of a set of objects without knowing the actual behaviour beforehand.

The need for abstract classes is that you can generalize the super class from which child classes can share its methods. The subclass of an abstract class which can create an object is called as "concrete class".

Abstract Classes

Page 9: Class Advanced New

virtual class A ; virtual task disp (); $display(" This is class A "); endtask endclass

class EA extends A ; task disp (); $display(" This is Extended class A "); endtask endclass

program main ; EA my_ea; A my_a;

initial begin my_ea = new(); my_a = my_ea; my_ea.disp(); my_a.disp(); end endprogram

RESULT

This is Extended class A This is Extended class A

example

Class Advanced

Page 10: Class Advanced New

virtual class A ; virtual task disp (); $display(" This is class A "); endtask endclass

program main ; A my_a;

initial begin my_a = new(); my_a.disp(); end endprogram

RESULT

Abstract class A cannot be instantiated

creating object of virtual class

Class Advanced

Page 11: Class Advanced New

Virtual keyword is used to express the fact that derived classes must redefine the properties to fulfill the desired functionality.

Thus from the abstract class point of view, the properties are only specified but not fully defined.(base class)

The full definition including the semantics of the properties must be provided by derived classes.

Virtual Keyword

Page 12: Class Advanced New

class Base; typedef enum {bin,oct,dec,hex} radix;

task print( radix r, integer n ); $display(" Enum is %s ",r.name()); $display(" Val is %d",n); endtask endclass

program main; initial begin Base b = new; int bin = 123; b.print( Base::bin, bin ); // Base::bin and bin are different end endprogram

RESULT:

Enum is bin Val is 123

Scope resolution operator

Page 13: Class Advanced New

The class scope resolution operator enables the following: Access to static public members (methods and class properties) from outside the class hierarchy. Access to public or protected class members of a superclass from within the derived classes.

Note:

Page 14: Class Advanced New

Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass.

Multiple inheritance allows a class to take on functionality from multiple other classes, such as allowing a class named D to inherit from a class named A, a class named B, and a class named C.

Multiple Inheritance

Class Advanced

Page 15: Class Advanced New

class A; ..... endclass

class B; ...... endclass

class C; ...... endclass

class D extends A,B,C; ..... endclass

example

Class Advanced

Page 16: Class Advanced New

Multiple inheritance is not implemented well in SystemVerilog languages . Implementation problems include:

1. Increased complexity 2. Not being able to explicitly inherit from 3. multiple times from a single class Order of inheritance changing class semantics.

Page 17: Class Advanced New

Method overloading is the practice of declaring the same method with different signatures. The same method name will be used with different data type . This is Not Supported by SystemVerilog as of 2008.

EXAMPLE: task my_task(integer i) { ... } task my_task(string s) { ... }

program test { integer number = 10; string name = "vera"; my_task(number); my_task(name); }

Method Overloading

Page 18: Class Advanced New

Class methods and Constraints can be defined in the following places:

1. inside a class. 2. outside a class in the same file. 3. outside a class in a separate file.

The process of declaring an out of block method involves: 1.declaring the method prototype or constraint within the class declaration with extern qualifier.

The extern qualifier indicates that the body of the method (its implementation) or constraint block is to be found outside the declaration.

NOTE : class scope resolution operator :: should be used while defining.

External declaration

Page 19: Class Advanced New

class B; extern task printf();

endclass

task B:: printf(); $display(" Hi ");

endtask

program main; initial begin B b; b = new(); b.printf(); end endprogram

RESULT:

Hi

example

Class Advanced

Page 20: Class Advanced New

Thank You!