Q:

Python | Create Employee Class with Constructor and Destructor

belongs to collection: Python class & object programs

0

Python | Create Employee Class with Constructor and Destructor

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python - employee class using Constructor and Destructor code

# employee class code in Python
# class definition
class Employee:
    def __init__(self): #Constructor
        self.__id = 0
        self.__name = ""
        self.__gender = ""
        self.__city = ""
        self.__salary = 0
        print("Object Initialized.")
    def __del__(self): #Destructor
        print("Object Destroyed.")
    def setData(self):
        self.__id=int(input("Enter Id\t:"))
        self.__name = input("Enter Name\t:")
        self.__gender = input("Enter Gender:")
        self.__city = input("Enter City\t:")
        self.__salary = int(input("Enter Salary:"))
    def __str__(self):
        data = "["+str(self.__id)+","+self.__name+","+self.__gender+","+self.__city+","+str(self.__salary)+"]"
        return data
    def showData(self):
        print("Id\t\t:",self.__id)
        print("Name\t:", self.__name)
        print("Gender\t:", self.__gender)
        print("City\t:", self.__city)
        print("Salary\t:", self.__salary)


def main():
    #Employee Object
    emp=Employee()
    emp.setData()
    emp.showData()
    print(emp)

if __name__=="__main__":
    main()

Output

Enter Name:     pankaj
Enter Gender:   male
Enter City:     delhi
Enter Salary:   55000
Id              : 1
Name    : pankaj
Gender  : male
City    : delhi
Salary  : 55000	

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Example of single inheritance in Python... >>
<< Python | Create Employee Class...