Posts

Showing posts from September, 2021

Polymorphism examples

Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or object to take on multiple forms. In a programming language exhibiting polymorphism, class objects belonging to the same hierarchical tree (inherited from a common parent class) may have functions with the same name, but with different behaviors. Example The classic example is of the Shape class and all the classes that are inherited from it, such as: Rectangle Triangle Circle This Code is written in C++ class Shape {   public :   Shape (){}   // defining a virtual function called Draw for shape class   virtual void Draw (){ cout << " Drawing a Shape " << endl; } }; class Rectangle : public Shape {   public :   Rectangle (){}   // Draw function defined for Rectangle class   virtual void Draw (){ cout << " Drawing a Rectangle " << endl; } }; class Triangle : public Shape {   public :   Triangle ...

Super() and Overriding In python

  class A: classvar1= "I am a class a variable in class A" def __init__ ( self ): self .var1= "I am inside class A's Constructor" self .classvar1= "I m a instance variable in class A" self .sp= "Hello kartik" class B(A): classvar2= "I am in class B" def __init__ ( self ): super (). __init__ () self .var1= "I am inside class B's Constructor" self .classvar1= "I m a instance variable in class B" a=A() b=B() print (b.sp)

Decorators In Pythons

https://hindi.goforgyan.com/python/function-decorators-and-decorators-in-python-in-hindi/   def upper (function): def wraper (): func=function() makeUpper=func.upper() return makeUpper return wraper def say (): return "Hello hi" deco=upper(say) print (deco())

Abstract Method Example

from abc import ABC , abstractmethod class Shape(ABC): @abstractmethod def printAria ( self ): # ab sabhi cls ko printAria funtion apne andr define kr na hi hoga return 0 class rectangle(Shape): type= "rectangle" side= 4 def __init__ ( self ): self .lenght= 5 self .wirdth= 6 # def printAria(self): # return self.lenght*self.wirdth rect=rectangle() print (rect.printAria())