Part-II- Python: Object Oriented Programming
In part-I of this series, we built a basic class in Python and compared the code with a similar class in C#. In this article, we derive the basic class built in part-I to create a new CAdvancedMath to which we add a function to find the factorial of an integer number.
The base class code for Python and C# that we used in part-I is given below:
We create a new class CAdvancedMath that derives from CBaseMath. Note that we use the base class 'as-is' without changing any code. Code for the C
The Python code clearly shows the different syntax for deriving from a base class. also note the way the base class constructor is called using CBaseMath.__init__(self,sigDigits).
The full code to instantiate the classes and call the functions of the derived CAdvancedMath class is:
The outputs for the respective codes is given as:
The printout of the 2nd and 3rd values are different between Python because of the way the round function works in both platforms.
If we change the significant digits to 3, the full code to instantiate the classes and call the functions is:
And the corresponding output is:
The outputs for the respective code bases is given as:
The following screen shows the PyCharm and Visual Studio IDE's with the solution containing this code.
As we can see from the screen captures, the code for both classes is contained in the same file. In part-III, we see how we can link code between different Python files.
Comments
Post a Comment