196k views
4 votes
Given a member function named set_age() with a parameter named age in a class named Employee, what code would you use within the function to set the value of the corresponding data member if the data member has the same name as the parameter

User Yeeling
by
5.0k points

1 Answer

2 votes

Answer:

Step-by-step explanation:

This would be considered a setter method. In most languages the parameter of the setter method is the same as the variable that we are passing the value to. Therefore, within the function you need to call the instance variable and make it equal to the parameter being passed. It seems that from the function name this is being written in python. Therefore, the following python code will show you an example of the Employee class and the set_age() method.

class Employee:

age = 0

def __init__(self):

pass

def set_age(self, age):

self.age = age

As you can see in the above code the instance age variable is targeted using the self keyword in Python and is passed the parameter age to the instance variable.

User Lonzo
by
4.4k points