# OOPS%2B

course: Academy — 65-GENAI-for-Engineers
module: Academy/65-GENAI-for-Engineers
type: notebook
source_url: https://personal-learn.armco.dev/files/Academy/65-GENAI-for-Engineers/folders/Live_Intervention_OOPs_and_Numpy_Folder/OOPS%2B.ipynb

---
[cell 1 markdown]
Creat a class 'Car' which has attributes such as 'Brand' and 'Color'

[cell 2 code]
#definining a class
class Car:
    def __init__(self,brand,color): #brand and color are my attributes, init is a constructor and self is a pointer
        self.brand=brand #initializing brand attribute
        self.color=color #initializing color attribute

#defining objects
obj1=Car('Mercedes','Black')
obj2=Car('Porche','Yellow')
obj3=Car('Ferrari','Red')

[cell 3 code]
obj1.brand

[cell 4 code]
obj1.color

[cell 5 code]
obj3.color

[cell 6 code]
obj3.brand

[cell 7 code]
obj2.brand

[cell 8 code]
obj2.color

[cell 9 markdown]
Create a class 'Person' with attributes such as 'Name','Age','Qualification' and 'Hobby'

[cell 10 code]
#definining a class
class Person:
    def __init__(self,name,age,qualification,hobby):
        self.name=name
        self.age=age
        self.qualification=qualification
        self.hobby=hobby

#defining objects
obj1=Person('Aashutosh',99,'MTech','Teaching')

[cell 11 code]
obj1.name

[cell 12 code]
obj1.hobby

[cell 13 code]
obj1.qualification

[cell 14 markdown]
Inheritance

Polymorphism

Encapsulation

Abstraction

[cell 15 markdown]
Inheritance

- Single Level Inheritance

- Multi Level Inheritance

- Multiple Inheritance

[cell 16 code]
#Single Level Inheritance

class Animal: #parent class

    def __init__(self,name):
        self.name=name

    def eat(self):
        print(self.name+"likes to eat")

class Dog(Animal): #child class

    def __init__(self,name,breed):
        super().__init__(name) #super() will call the __init__ method of the Animal class
        self.breed=breed

[cell 17 code]
obj1=Dog("Buddy","GoldenRetriever")

[cell 18 code]
obj1.breed

[cell 19 code]
obj1.eat()

[cell 20 code]
class Employee: #Parent class

    def __init__(self,name,salary):
        self.name=name
        self.salary=salary

    def get_details(self):
        print("Hi",self.name,"your salary is",self.salary)

class Manager(Employee): #child class

    def __init__(self,name,salary,department):
        super().__init__(name,salary)
        self.department=department

[cell 21 code]
a=Employee("John",50000)

[cell 22 code]
a.salary

[cell 23 code]
a.get_details()

[cell 24 code]
p=Manager('Alex',20000,'IT')

[cell 25 code]
p.department

[cell 26 code]
p.get_details()

[cell 27 code]
q=Manager('Catherine',40000,'HumanResource')

[cell 28 code]
q.name

[cell 29 code]
q.get_details()

[cell 30 markdown]
- Create a parent class "Person" with attributes such as "name" and "age" and a method which displays the name and age of the person

- Create a child class "Student" which inherits the methods from the parent class and has attributes such as "name","age" and "student_id"

[cell 31 code]
class Person:

    def __init__(self,name,age):
        self.name=name
        self.age=age

    def details(self):
        print("Hi",self.name," your age is",self.age)

class Student(Person):

    def __init__(self,name,age,student_id):
        super().__init__(name,age)
        self.student_id=student_id

[cell 32 code]
s1=Student('Marco',19,2036)

[cell 33 code]
s1.student_id

[cell 34 code]
s1.details()

[cell 35 code]
#Multi Level Inheritance

class Animal: #parent class of Dog

    def __init__(self,name):
        self.name=name

    def eat(self):
        print(self.name+"likes to eat")

class Dog(Animal): #child class of Animal and Parent class of Pet

    def __init__(self,name,breed):
        super().__init__(name) #super() will call the __init__ method of the Animal class
        self.breed=breed

class Pet(Dog):

    def __init__(self,name,breed,ownername):
        super().__init__(name,breed)
        self.ownername=ownername

[cell 36 code]
p1=Pet('Spike','Labrador','Mike')

[cell 37 code]
p1.breed

[cell 38 code]
p1.ownername

[cell 39 code]
p1.name

[cell 40 code]
p1.eat()

[cell 41 code]
obj1.eat()

[cell 42 markdown]
- Create a class "Vehicle" with attribute "brand" and a method which talks about the transmission type - automatic or manual

- Create child class "Car" with attributes "brand" and "Model"

- Create another child class "SportsCar" with attributes "brand", "model", "color" and "doors"

[cell 43 code]
class Vehicle:

    def __init__(self,brand):
        self.brand=brand

    def transmission(self):
        print("the transmission type is automatic")

class Car(Vehicle):

    def __init__(self,brand,model):
        super().__init__(brand)
        self.model=model

class SportsCar(Car):

    def __init__(self,brand,model,color,doors):
        super().__init__(brand,model)
        self.color=color
        self.doors=doors

[cell 44 code]
s1=SportsCar('Audi','A6','White',2)

[cell 45 code]
s1.model

[cell 46 code]
s1.transmission()

[cell 47 code]
#Multiple Inheritance

class A: #parent class

    def method(self):
        print("Hello from A")

class B: #parent class

    def method(self):
        print("Hello from B")

class C(A,B): #child class

    def method(self):
        print("Hello from C")

[cell 48 code]
c=C()

[cell 49 code]
c.method() #M.R.O - Method Resolution Order

[cell 50 code]
#Multiple Inheritance

class A: #parent class

    def method(self):
        print("Hello from A")

class B: #parent class

    def method(self):
        print("Hello from B")

class C(A,B): #child class

    def method_(self):
        print("Hello from C")

[cell 51 code]
c=C()

[cell 52 code]
c.method()

[cell 53 code]
#Multiple Inheritance

class A: #parent class

    def method__(self):
        print("Hello from A")

class B: #parent class

    def method(self):
        print("Hello from B")

class C(A,B): #child class

    def method_(self):
        print("Hello from C")

[cell 54 code]
c=C()

[cell 55 code]
c.method()

[cell 56 code]
C.__mro__

[cell 57 markdown]
Encapsulation

[cell 58 code]
class Person:

    def __init__(self,name,age,salary):
        self.name=name #public attribute
        self.age=age #pubic attribute
        self.__salary=salary #private attribute .__

    def salary(self):
        print("Your salary is",self.__salary)

[cell 59 code]
obj1=Person('Aashutosh',99,10000)

[cell 60 code]
obj1.age

[cell 61 code]
obj1.salary

[cell 62 code]
obj1.__salary

[cell 63 code]
obj1.salary() #able to access the private attribute only via the salary() method

[cell 64 markdown]
- Create a class 'BankAccount' which takes two attributes 'a/c number' and 'pin code'

- Make 'pin code' as a private method and fetch the value using a separate method

[cell 65 code]
class BankAccount:

    def __init__(self,ac_number, pin):
        self.ac_number=ac_number
        self.__pin=pin

    def my_pin(self):
        print("My pin code is",self.__pin)

[cell 66 code]
obj1=BankAccount(12344321,3456)

[cell 67 code]
obj1.ac_number

[cell 68 code]
obj1.pin

[cell 69 code]
obj1.__pin

[cell 70 code]
obj1.my_pin()

[cell 71 code]
class BankAccount:

    def __init__(self,owner,balance):
        self.owner=owner
        self.__balance=balance #private attribute

    def deposits(self,amount):
        if amount>0:
            self.__balance=self.__balance+amount
        else:
            print("Invalid deposit amount")

    def withdraw(self,amount):
        if 0<amount<=self.__balance:
            self.__balance=self.__balance-amount
        else:
            print("Invalid withdraw amount")

    def get_balance(self):
        print("your current balance is",self.__balance)

[cell 72 code]
ac=BankAccount("Aashutosh",10000)

[cell 73 code]
ac.owner

[cell 74 code]
ac.__balance

[cell 75 code]
ac.deposits(1000)

[cell 76 code]
ac.get_balance() #updated balance = 10000+1000=11000

[cell 77 code]
ac.withdraw(500)

[cell 78 code]
ac.get_balance() #updated balance = 11000-500=10500

[cell 79 markdown]
Getter() - to access, Setter() - to modify

[cell 80 code]
class Car:

    def __init__(self,model,color,price):
        self.model=model
        self.color=color
        self.__price=price

    def get_price(self): #getter() - allows us to fetch the private attributes
        print("the price of this car is",self.__price)

    def set_price(self,price): #setter() - allows us to modify the value associates with private attributes
        if price>0:
            self.__price=price
        else:
            print("Invalid price")

[cell 81 code]
car=Car('Tesla','Red',50000)

[cell 82 code]
car.get_price()

[cell 83 code]
car.set_price(1000000)

[cell 84 code]
car.get_price()

[cell 85 markdown]
- Create a class 'Employee' with attributes such as 'name' and 'salary'

- Keep the 'salary' as a private attribute and create a method to view the private attribute (getter())

- Create a method to give a raise in the salary to the employee if the salary is valid (setter())

[cell 86 code]
class Employee:

    def __init__(self,name,salary):
        self.name=name
        self.__salary=salary

    def show_salary(self): #getter()
        print("your salary is",self.__salary)

    def raise_salary(self,amount):
        if amount>0:
            self.__salary=self.__salary+amount
        else:
            print("invalid salary")

[cell 87 code]
emp=Employee("Aashutosh",10000)

[cell 88 code]
emp.show_salary()

[cell 89 code]
emp.raise_salary(5000)

[cell 90 code]
emp.show_salary() #incremented salary=10000+5000=15000

[cell 91 markdown]
Polymorphism - having multiple forms

[cell 92 code]
class Cat:
    def sound(self):
        print("Meow")

class Dog:
    def sound(self):
        print("Bark")

class Bird:
    def sound(self):
        print("chirp")

def animal_sound(animal):
    print(animal.sound())

[cell 93 code]
cat=Cat()

[cell 94 code]
dog=Dog()

[cell 95 code]
animal_sound(cat)

[cell 96 code]
animal_sound(dog)

[cell 97 code]
bird=Bird()

[cell 98 code]
animal_sound(bird)

[cell 99 markdown]
- Define a class Car and a class Bike. Both classes should have a wheels() method which prints how many wheels do they have

- Write a method which accepts the name of any object and calls the wheels() method using Polymorphism

[cell 100 code]
class Car:
    def wheels(self):
        print("Car has 4 wheels")

class Bike:
    def wheels(self):
        print("Bike has 2 wheels")

class Truck:
    def wheels(self):
        print("Truck has 18 wheels")

def transport(vehicle):
    vehicle.wheels()

[cell 101 code]
car=Car()
bike=Bike()
truck=Truck()

[cell 102 code]
transport(car)

[cell 103 code]
transport(truck)

[cell 104 markdown]
Create a class Shopping_Cart which has:

- a method to add items to the cart, these items should be private to us
- a method to remove items from the cart
- a method to calculate the total cost of items added to the cart
- a method to access the items

[cell 105 code]
class Shopping_Cart:
    def __init__(self):
        self.__items={} #private empty dictionary that store the items and their price

#method to add items to the cart
    def add_item(self,item,price):
        if price>0:
            self.__items[item]=price
            print("added",item,"to the cart for",price)
        else:
            print("price should be valid")

#method to remove items from the cart
    def remove_item(self,item):
        if item in self.__items:
            del self.__items[item]
            print("removed",item,"from the cart")
        else:
            print("item not found in cart")

#method to calculate the total cost
    def total_cost(self):
        print(sum(self.__items.values()))

#method to access the items (getter())
    def get_items(self):
        print(self.__items)

[cell 106 code]
cart=Shopping_Cart()

[cell 107 code]
cart.add_item("Laptop",100000)
cart.add_item("Phone",50000)
cart.add_item("IPad",44000)

[cell 108 code]
cart.get_items()

[cell 109 code]
cart.total_cost()

[cell 110 code]
cart.remove_item("IPad")

[cell 111 code]
cart.get_items()

[cell 112 code]
cart.total_cost()

[cell 113 markdown]
Create a Student_Record class with features like:

- a method for adding student details like name, age and grades
- a method to remove a student from the records
- a method to calculate the average marks of all the students

[cell 114 code]
#HW