232k views
1 vote
Define the method object inc_num_kids() for PersonInfo. inc_num_kids increments the member data num_kids. Sample output for the given program with one call to inc_num_kids(): Kids: 0 New baby, kids now: 1

User SebastianK
by
5.3k points

1 Answer

1 vote

Answer:

The answer to this question can be given as:

Program:

#define class.

class PersonInfo:

def __init__(self): #constructor

self.num_kids = 0

def inc_num_kids(self): #define function inc_num_kids()

self.num_kids = self.num_kids + 1

return self.num_kids #return value.

p= PersonInfo() # creating object

print('Kids:', p.num_kids) #print value

p.inc_num_kids() #call function

print('New baby, kids now:', p.num_kids) #print value

Output:

Kids: 0

New baby, kids now: 1

Explanation:

The description of the above python program as follows:

  • In this program firstly we define the class that is PersonInfo. In this class we define the constructor that is def __init__() and one function that is def inc_num_kids().
  • The constructor is called automatically when the class object is created. In this constructor, we use the self as a parameter that is used to access variables that belong to class.
  • In the constructor, we define the variable that is "num_kids". We assign value to a variable that is "0" and use the self for holding reference to the variable.
  • Then we define the function. In this function, we increment the value of the variable by 1 and return its value.
  • Then we create a class object that is p and call the function and print its value.

User Ezequiel Jadib
by
6.3k points