Object oriented concepts -Classes and objects

 Everything is an object in real world. In object-oriented programming, basic term is classes and objects. Class is a collection of objects and methods. Each class has some properties and functionalities.

For eg, A car is a class. Name of the car is like the classname. Brand of the car, color and size are the properties of the class. Running is the functionality of a car. It is the method.

Let us create a class in python. Here, the code begins…

Syntax:  class classname:

                   Variablename

Eg: class SampleClass:

               S = 10

Next, an object is created. The object accesses the Sum variable and it gets printed.

        Ob = SampleClass()

        Print(Ob.sum)

Add this code in a file “SampleClass.py”. While executing this, we get the following output.

Next, Average of three numbers using class is given below.

AvgClass.py

class AvgClass:

  a = 10

  b = 20

  c = 30

ob = AvgClass()

print((ob.a+ob.b+ob.c)/3)

Executing this code, it will give the Average of the three numbers as output.


 This is the first concept in object oriented programming.

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.