Practical 2:program to access class members using a class object.
Problem Statement
Write a Python program to access class members using a class object.
Solution Code
class Employee:
company_name = "TechCorp" # Class variable
def greet(self):
return f"Welcome to {self.company_name}"
# Creating an object of Employee class
emp1 = Employee()
# Accessing class members using the object
print(emp1.company_name)
print(emp1.greet())
Output
The program should display the class variable company_name and the output of the method greet() using the object emp1.
Example Output
TechAmplifiers
Welcome to TechAmplifiers
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 5 Practical 2 solution where we have written Python program to access class members using a class object.
- Accessing Class Members: Both class variables and methods can be accessed using the class object (emp1).
- Instance Method: The greet() method is an instance method, which can be called using the object.
Key Concepts Learned
- Class Members (Variables and Methods)
- Both class variables and instance methods are accessible through objects.