0% found this document useful (0 votes)
2 views

OOP lab 2

The document outlines the principles of object-oriented programming, focusing on the concepts of classes and objects. It explains how to define classes in Python, using examples like 'Car' and 'Student', and describes how objects are instances of these classes. Additionally, it includes lab tasks for creating a 'Complex' class for arithmetic with complex numbers and a 'Rational' class for arithmetic with fractions.

Uploaded by

my5718621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OOP lab 2

The document outlines the principles of object-oriented programming, focusing on the concepts of classes and objects. It explains how to define classes in Python, using examples like 'Car' and 'Student', and describes how objects are instances of these classes. Additionally, it includes lab tasks for creating a 'Complex' class for arithmetic with complex numbers and a 'Rational' class for arithmetic with fractions.

Uploaded by

my5718621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 02: Class and Object Implementation

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.

Creating an Object in Python


We saw that the class object could be used to access different attributes. It can also be used
to create new object instances (instantiation) of that class. The procedure to create an
object is similar to a function call.

>>> 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.

Task 1: Complex Class


Create a class called Complex for performing arithmetic with complex numbers.
Write a program to test your class. Complex numbers have the form
realPart + imaginaryPart * i

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy