Get Set Functions for variables in a class

Hi,
I am trying to write a get_set function which will return the value of the variable or set the variable to a value.
For example :


           class test;
                int var1;
                bit [3:0]var2;

                function int get_var1();
                    return   var1;
                endfunction

                function bit get_var2();
                    return var2;
                endfunction

                function void set_var1(int var1_value);
                    var1 = var1_value;
                endfunction  

               function void set_var2(bit [3:0] var2_value);
                    var2 = var2_value;
               endfunction
           endclass 

and so on

I want to generalize the get and set functions as the number of functions keep increasing , the number of get and set functions would keep increasing.
But the problem in that case would be the return type and type of the arguments being passed to the function, as the return type and type of arguments would vary with the variables.

Can anyone suggest a workaround for this, how get and set can be achieved in a generic way for different variables of a class?

Thanks in advance!

In reply to Katri:

In your code everything is fine except that “function bit get_var2()” type is not matched to the set_var2 and declaration of that variable…so take care of types then you can write any number of functions it wont effect.

In reply to venky.bollepally:

Yea, i will check the return type. But i don’t want to write many get set functions. I want to know if there is a way to write a “single function/task or anything similar to get and set values” for all the variables.

In reply to Katri:

Use ‘let’.

It does not make much sense to write individual set/get functions for every single variable member in your class. Variables that you want user to be able set/get directly should be kept public, which is the default in SystemVerilog. There is no need for set/get functions with public variables - just read or write them directly.

However, one of the 4 key principles of object oriented programming is Encapsulation. Encapsulation is the practice of hiding the implementation of class data and providing a public interface (API) to the required functionality.

For example, if you were to implement the SystemVerilog mailbox class on your own, you would implement the mailbox with a protected queue variable, and define access methods to push or pop data into the queue, but the user would never be able to see the contents of the queue directly.