Methods - Rybd04/2143-OOP GitHub Wiki

Definition

Methods

A method is a function that is defined inside a class and is used to perform operations using the data (attributes or member variables) of that class.

Explanation

Methods define behavior. They are like how your brain knows and performs step by step processes.

Basic Code Example

class Person:
    species = "Human"  # class variable

    def __init__(self, name):
        self.name = name  # instance variable

    def greet(self):  # instance method
        return f"Hello, my name is {self.name}."

    @classmethod
    def get_species(cls):  # class method
        return cls.species

    @staticmethod
    def say_hello():  # static method
        return "Hello!"

greet() is an instance method.

get_species() is a class method.

say_hello() is a static method.

Image

image

Additional Resources

https://www.w3schools.com/cpp/cpp_class_methods.asp https://www.geeksforgeeks.org/cpp-class-methods/