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

worksession_Advanced Python_UHasselt

The document outlines exercises on Object-Oriented Programming (OOP) focusing on inheritance, polymorphism, operator overloading, and data classes. It includes the definition of classes such as Book, ResearchBook, ChildrenBook, and Point, along with their attributes, methods, and operator overloads. Additionally, it contrasts traditional class definitions with data classes using the Person example, highlighting differences in equality and string representation outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

worksession_Advanced Python_UHasselt

The document outlines exercises on Object-Oriented Programming (OOP) focusing on inheritance, polymorphism, operator overloading, and data classes. It includes the definition of classes such as Book, ResearchBook, ChildrenBook, and Point, along with their attributes, methods, and operator overloads. Additionally, it contrasts traditional class definitions with data classes using the Person example, highlighting differences in equality and string representation outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

APPY Exercises on OOP

Part II

Inheritance and Polymorphism


1. Define a class Book that has the following attributes:

• isbn: is a unique identifier for books made of 10-digits


• title: is the title of the book
• authors: is the list of authors’ names
• production_price: is the price paid to produce a copy of the book

2. Define a class ResearchBook which is a subclass of class Book that has an extra attribute
field which is a string that represents the scientific field of that research book.

3. Define a class ChildrenBook which is a subclass of class Book that has two extra attributes:
age_limit which a number representing the minimum age, profit_percentage which is
a number from 0 to 100 that represents the percentage of profit that will be added to the
production price to form the purchase price.

4. Include __init__ and __repr__ methods for the three classes.

5. Add setters and getters for the attributes in the three classes.

6. Add a polymorphic method get_purchase_price that returns the price required to pur-
chase a book. This value is calculated differently in the case of a children book. In general,
this purchase price equals the production price of the book doubled, however, the purchase
price of a children book equals the production price incremented by the profit percent-
age. For example, of the production price is 10 and the profit percentage is 20, then the
purchase price is 12.

Operator Overloading
7. Define a class Point that has two integer attributes: x and y.

8. Define __init__ and __repr__ for that class.

9. Overload the definition of the operators == , < , <= , + , += for this class. We consider
two points equal when they have the same coordinates. We also consider a point less than
another point when the distance from origin is less. Moreover, adding two points results
in a point whose x-coordinate (y-coordinate) value is the summation of the x-coordinates
(y-coordinate) of the two input points.

1
Data Classes
The aim of this question is to know the differences between defining classes in the traditional
way and defining them as data classes.

10. Define a class Person that has three attributes: a string first_name, string last_name,
and int birth_year. Define __init__ for this class.

11. Define a data class PersonDC that has the same attributes as class Person.

12. Create two Person objects, person_1 and person_2 with the same attribute values.

13. Create two PersonDC objects, person_dc_1 and person_dc_2 with the same attribute
values.

14. What will person_1 == person_2 and person_dc_1 == person_dc_2 return? Why the
results differ?

15. What will print(person_1) and print(person_dc_1) print? Why the results differ?

You might also like