C# And Python Compared For Object-oriented Programming

C# And Python Compared For Object-oriented Programming

Hello my name is Chet on your bail wall in this video we are going to look at object-oriented programming and how it works in c-sharp and Buyten so we are essentially going to build a simple program with two clauses which have the same functionality between c-sharp and python this is a program to do some it’s a class which.

Has some basic math functions and then there’s a derived class called C advanced math which implements a function for doing factorials but.

It derives from the base class so on the Left I have Visual Studio opened and on the right by charm so python will use.

For Python and Visual Studio for C sharp so as you can see let me expand the solution on Visual Studio there are three main files there’s a sea bass matte dot CSC advanced math dot CS and a.

Program dot C is so program that C is mainly instantiate object of the C advanced math class similarly we have the same structure in Python except there’s a there’s also concept of a solution per se.

So I’ve added all these files in a single project so the sea bass mat C advance mat and the programs are Python now let’s take a look at the code of see base mat so a double-click let me expand the code window and in Visual Studio so these.

Have C sharp on the right I have peyten are doing the same have the same functionality the init here.

Is the constructor in python similar to the constructor i declare in c-sharp which takes as input the significant digits in python i also have to pass an object of.

Self so it’s it automatically creates a private variable anything starting with two underscores in.

Python is treated is equivalent to a python it has most man most semantics to it but for the sake of this demo let’s consider this as a private variable similarly I have a function called private that’s a private function for setting the rounding I start the same function with underscore to underscores to use that in Python and I’m the conventions I am using in Python everything it’s kind of user camel case where it’s starting with a small alphabet while in c-sharp we start the function names starts with capital so this.

Is my private function in c-sharp we can just use the keyword private and public to differentiate in Python if it does not start with two underscores is automatically treated as a public so I have a function called sum that takes two doubles and calls the function rounding and basically does a sum operation similarly for subtract multiply and divide and we see the same thing here in Python of course we don’t use these braces because everything is.

Done by indentation so if I declared my function using the DEF keyword this is my function I pass the self object I need to pass that because I’m going to use the set rounding function here and then I simply return this value this internation single indentation here say is that this routine here is part of the function sum so pretty straightforward as you can see the Python code is.

A little bit compact this indentation has its positives and its negatives they had two spectrums but I won’t dive into that also use the use of the colon that denotes that we.

Use it both in the class and the function that denotes where the specific code for the respective class of the function starts so pretty straightforward right and here since basemath.

Is a is the foundation of class we are not referencing any other class here so there is no references here except in c-sharp I have to reference the system library because I’m using the mad static glass now let’s take a look at the second one the C.

Matte so the C at once mat actually derives from see base math now see base math and see at once math at the same namespace so in c-sharp I don’t need to explicitly include it derivation is pretty straightforward this is the common syntax that they borrowed from C++ now Java uses something different it you have to use the inherits keyword in Java so here’s my base class function sorry this is.

My t right class this is the constructor and in turn I call the constructor of the base class so this is in c-sharp i just declare a function here called factorial int just computes a factorial of any value.

Passed to it and then it returns the final value note that I use the function multiplied so multiplied in turn if I go to the definition I just go to the implementation it automatically uses the set rounding function so I don’t have to round the values now let’s take a look at Python for doing the same thing I of course have my constructor the Sigma I said the significant digits and I.

Call the base class so this is equivalent to this in sisha where I’m.

Calling the base class constructor if I use sea bass math dot in it one important thing to notice is the from see base math import see.

Base math what this is telling python since there is no direct concept of namespace it’s telling python that from the class c base math dot p i import the class c base math so here if i look at all the files i.

Have i have a file called see base math dot bi so this is a very important concept to understand this is how you link code in different files in Python and you know you if you’re any doing any commercial-grade application your code.

Has to be split in multiple files for ease of maintainability and reading and lot of other.

Factors so this is how you use declare a code in some and another file and use it in some other file in c-sharp we didn’t have to do that because it’s part of the same namespace but if we are using an external library like in the case.

Of C based math I have to use the using function so it’s kind of similar to that the function factorial in Python has the same functionality as we have in c-sharp basically we execute a loop and then it again calls the multiply function note that self not multiply since we inherited it.

It will be able to reference this function multiply in the base class one thing I forgot to show is how we use inheritance in Python is this is the syntax you declare this as the base class add within braces you declare the base class so this.

Is equivalent to using this syntax over here in c-sharp so that’s not too complex and again we.

Are using the indentation note how this code here is part of the factory line T and this code here is part of the for loop so rather than using.