Geek Slack

Learn C++
    About Lesson


    C++ Class Methods


    C++ Class Methods

    1. Basic Class Method Example

    Example of defining and using class methods:

    #include <iostream>
    #include <string>
    
    // Class declaration
    class Circle {
    private:
        double radius;
    
    public:
        // Constructor
        Circle(double r) {
            radius = r;
        }
    
        // Member function to calculate area
        double calculateArea() {
            return 3.14159 * radius * radius;
        }
    
        // Member function to calculate circumference
        double calculateCircumference() {
            return 2 * 3.14159 * radius;
        }
    };
    
    int main() {
        // Create object of class Circle
        Circle circle1(5.0);
    
        // Call member functions to calculate area and circumference
        double area = circle1.calculateArea();
        double circumference = circle1.calculateCircumference();
    
        // Display results
        std::cout << "Area of circle: " << area << std::endl;
        std::cout << "Circumference of circle: " << circumference << std::endl;
    
        return 0;
    }

    2. Static Methods

    Example of static methods in a class:

    #include <iostream>
    #include <string>
    
    // Class declaration
    class MathUtility {
    public:
        // Static method to calculate factorial
        static int factorial(int n) {
            if (n == 0 || n == 1) {
                return 1;
            } else {
                return n * factorial(n - 1);
            }
        }
    
        // Static method to calculate square
        static int square(int num) {
            return num * num;
        }
    };
    
    int main() {
        // Call static methods directly using class name
        int fact = MathUtility::factorial(5);
        int squareNum = MathUtility::square(7);
    
        // Display results
        std::cout << "Factorial of 5: " << fact << std::endl;
        std::cout << "Square of 7: " << squareNum << std::endl;
    
        return 0;
    }

    3. Constant Methods

    Example of constant methods in a class:

    #include <iostream>
    #include <string>
    
    // Class declaration
    class Person {
    private:
        std::string name;
        int age;
    
    public:
        // Constructor
        Person(std::string n, int a) : name(n), age(a) {}
    
        // Constant member function to display person's info
        void displayInfo() const {
            std::cout << "Name: " << name << ", Age: " << age << std::endl;
        }
    };
    
    int main() {
        // Create object of class Person
        const Person person("Alice", 30);
    
        // Call constant member function
        person.displayInfo();
    
        return 0;
    }

    Conclusion

    Class methods in C++ are member functions that operate on class objects, allowing manipulation of object data and behavior. This chapter covered basic class methods for calculating area and circumference, static methods for factorial and square calculations, and constant methods for accessing object data without modification. Understanding class methods enables you to design and implement efficient and maintainable C++ classes.