OOP lab 2
OOP lab 2
Overview:
The following discussion briefly outlines the principles of object-oriented
programming.
Class:
A class is a real or a virtual entity, which has relevance to the problem at hand and
has sharp physical boundaries. A class can be a real entity. For example, when one
develops software for a car wash company, then 'Car', is central to the software and
therefore there will be a class called 'Car'.
A class can also be a virtual entity. Another example is that when developing a
student management system, a 'student' class is crafted which is a virtual entity. In
both the examples, the entity was crafted as it was important to the problem at
hand.
The example of the 'student' class can be taken further. The class will have attributes,
which are needed in the program. The selection of attributes will decide on the
physical boundaries of the class. The fact of the matter is we will not be needing
unnecessary details like the number of cars a student has or where he went last
evening in an educational institute for which we are making the student
management system, so there is no point storing those details.
Like function definitions begin with the keyword def, in Python, we define a class
using the keyword class. The first string is called docstring and has a brief description
about the class. Although not mandatory, this is recommended.
Here is a simple class definition.
class MyNewClass:
'''This is a docstring. I have created a new class'''
pass
Example:
class MyClass:
"This is my second class"
a = 10
def func(self):
print('Hello')
Object:
Consider a student management system that stores the data of each student of a school.
Note that while entering data the operator would deal with an individual student, not the
idea of a student. The idea of a student is a class, whereas each student is an instance of
class or an object.
An object is an instance of a class. The objects interact with each other and get the work
done. Generally, a class can have any number of objects. One can even form an array of
objects. As a matter of fact, we make an object and call the methods of a class (those which
can be called).
In object-oriented paradigm, the program revolves around an object and therefore the type
of programming is termed as an object-oriented program. Calling a method of an object is
equivalent to sending message to an object.
>>> ob = MyClass()
Example:
class MyClass:
a = 10
def func(self):
print('Hello')
ob = MyClass()
print(ob.func)
# Output: Hello
LAB TASKS
You are required to complete the following tasks in the Lab and submit
your solution on LMS in PDF file format.
where i is -1
Use double variables to represent the member data of the class. Provide a
constructor that enables an object of this class to be initialized when it’s declared.
The constructor should contain default values in case no initializers are provided.
Provide methods that perform the following tasks:
a) Adding two Complex numbers: The real parts are added together and the
imaginary parts are added together.
b) Subtracting two Complex numbers: The real part of the right operand is
subtracted from the real part of the left operand, and the imaginary part of
the right operand is subtracted from the imaginary part of the left operand.
c) Printing Complex numbers in the form (a, b), where a is the real part and b is
the imaginary part.
Task 2: Rational Class
Create a class called Rational for performing arithmetic with fractions. Use integer
variables to represent the private data of the class—the numerator and the
denominator. Provide a constructor that enables an object of this class to be
initialized when it’s declared. The constructor should contain default values in case
no initializers are provided and should store the fraction in reduced form. For
2
example, the fraction 4 would be stored in the object as 1 in the numerator and 2 in
the denominator.
Write class methods to perform the following tasks:
a) Adding two Rational numbers. The result should be stored in reduced form.
b) Subtracting two Rational numbers. The result should be stored in reduced
form.
c) Multiplying two Rational numbers. The result should be stored in reduced
form.
d) Dividing two Rational numbers. The result should be stored in reduced form.
e) Printing Rational numbers in the form a/b, where a is the numerator and b is
the denominator.
f) Printing Rational numbers in floating-point format.