Object Oriented Programming Lab 4
Object Oriented Programming Lab 4
Lab Objectives:
Program 1: Write a Python program to create variable inside the class with string value and
accessing it.
Code:
# Python program to show that the variables with a value assigned in
class declaration, are class variables and variables inside methods
and constructors are instance variables.
# Class Variable
trade = 'Computer Science Software Engineering'
# Instance Variable
self.roll = roll
print(Faisal.trade)
print(Faisal.roll)
print(Faisal.trade)
print(Farhan.roll)
Program 2: Write a Python program to create instance variable inside method and accessing it.
Code:
# Python program to show that the variables with a value assigned in
class declaration, are class variables and variables inside methods
and constructors are instance variables.
# Class Variable
trade1 = 'Computer Science'
trade2 = 'Software Engineering'
# Instance Variable
self.roll = roll
2. Data Hiding
In Python, we use double underscore (Or ) before the attributes name and those attributes will
not be directly visible outside.
Code:
class MyClass:
Program 4: Write a Python program which can create a hidden variable inside the class. Then try to
access it by using tricky method.
Code:
# Executing code
myObject = MyClass()
print(myObject._MyClass hiddenVariable)
It does not have to be named self, you can call it whatever you like, but it has to be the first
parameter of any function in the class:
Program 5: Write a Python program which use other than self keyword for reference .
Code:
class Person:
def init (a, name, age, salary, profession):
a.name = name
a.age = age
a.salary = salary
a.profession = profession
def mydetail(a):
print("Assalam o Alekum, my name is " + a.name +", my age is
:" + str(a.age) +". Now a days earning :"+ str(a.salary) + ", its
really lovely to be a " + a.profession )
Program 6: Write a Python program which can modify the attributes inside the class.
Code:
class Person:
def init (a, name, age, salary, profession):
a.name = name
a.age = age
a.salary = salary
a.profession = profession
def mydetail(a):
print("Assalam o Alekum, my name is " + a.name +", my age is
:" + str(a.age) +". Now a days earning :"+ str(a.salary) + ", its
really lovely to be a " + a.profession )
Program 7: Write a Python program which uses the child class to call its own method and then parent
class method.
Code:
def parentMethod(self):
print ('Calling parent method')
def getAttr(self):
print ("Parent attribute :", Parent.parentAttr)
def childMethod(self):
print ('Calling child method')
Task1: Define what you understand by the classes, objects and functionalities for the following
scenarios.
Task 2: Make a class for insurance which can have multiple insurance policies. Design its UML and
Case diagram before coding. Later modify the attributes in child classes.
Task 3: Create a bike class and its components in light with the concept of Object Oriented. Later
create multiple bikes with different attributes based on customer requirements.
Task 7: You are working in Galaxy Computer as Programmer. The main business is selling computer,
laptops its accessories, used computer etc. Details can be find from https://galaxy.pk/. Your task is
to analyze the website, define which classes will be require and create the complete program which
can take user name and its requirements then give the total amount of computer/ laptop or accessories.