Skip to content

ikhlasafzaal/python_assignment-06

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Python OOP Assignment

This repository contains solutions for 20 Python Object-Oriented Programming (OOP) tasks, covering concepts like self, cls, public/private/protected variables, inheritance, abstraction, decorators, static/class methods, MRO, custom exceptions, and more.


Table of Contents


1. Using self

Created a Student class using self to initialize attributes name and marks through a constructor. A method display() prints the student details.

2. Using cls

Created a Counter class to track how many objects are created using a class variable and a @classmethod.

3. Public Variables and Methods

Built a Car class with a public variable brand and a public method start() accessible outside the class.

4. Class Variables and Class Methods

Developed a Bank class with a class variable bank_name and a class method change_bank_name() to change it.

5. Static Variables and Static Methods

Created MathUtils with a static method add(a, b) that performs addition without using class or instance variables.

6. Constructors and Destructors

Designed a Logger class that prints messages when an object is created and destroyed.

7. Access Modifiers: Public, Private, and Protected

Made an Employee class with public (name), protected (_salary), and private (__ssn) attributes.

8. The super() Function

Implemented inheritance where Teacher class inherits from Person and uses super() to call the parent constructor.

9. Abstract Classes and Methods

Utilized the abc module to define an abstract class Shape with an abstract method area(), and implemented it in Rectangle.

10. Instance Methods

Built a Dog class with instance variables and an instance method bark().

11. Class Methods

Created a Book class to track the number of books using a class method increment_book_count().

12. Static Methods

Developed a TemperatureConverter class with a static method celsius_to_fahrenheit().

13. Composition

Used composition by embedding an Engine object inside a CarWithEngine class.

14. Aggregation

Showed aggregation through Department and EmployeeAgg classes where the Employee exists independently.

15. Method Resolution Order (MRO) and Diamond Inheritance

Demonstrated MRO with classes A, B, C, and D, where D inherits from both B and C.

16. Function Decorators

Built a decorator log_function_call to log a message before executing a function.

17. Class Decorators

Created a class decorator add_greeting to dynamically add a greet() method to a class.

18. Property Decorators

Designed a Product class with a private attribute _price using @property, @setter, and @deleter.

19. callable() and call()

Made a Multiplier class implementing __call__() to allow objects to behave like functions.

20. Creating a Custom Exception

Defined a custom exception InvalidAgeError and raised it from a check_age(age) function if age < 18.


How to Run

  1. Make sure you have Python 3.x installed.
  2. Save the code into a file named `assignment.py

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages