1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Object-Oriented Programming in Python

Connected

Exercise

Add a class constructor

In this exercise, you'll continue working on the Employee class. Instead of using the methods like set_salary() that you wrote in the previous lesson, you will introduce a constructor that assigns name and salary to the employee at the moment when the object is created.

Initializing attributes in the constructor is a good idea because this ensures that the object has all the necessary attributes the moment it is created.

Instructions 1/2

undefined XP
    1
    2
  • Define the Employee class with a constructor that accepts two arguments, name and salary (the latter with a default value of 0).
  • Create two attributes, also called name and salary, setting their values to the corresponding arguments.