WikiGalaxy

Personalize

C++ Class Methods

Introduction to Class Methods:

Class methods in C++ are functions that are defined within a class and are used to manipulate the data members of the class. They provide the functionality to encapsulate data and operations on data within a single unit.

Example 1: Defining a Simple Method

A simple method can be defined to perform operations like displaying a message or calculating a value. Here is an example:


class MyClass {
  public:
    void displayMessage() {
        std::cout << "Hello, World!" << std::endl;
    }
};
    

Explanation:

In this example, a class named `MyClass` has a method `displayMessage` that prints a message to the console. This method does not return any value and does not take any parameters.

Parameterized Methods

Example 2: Method with Parameters

Methods can accept parameters to work with different data inputs. Here's how you can define a method with parameters:


class Calculator {
  public:
    int add(int a, int b) {
        return a + b;
    }
};
    

Explanation:

The `add` method in the `Calculator` class takes two integer parameters and returns their sum. This allows the method to perform addition on any two integers provided as arguments.

Return Types in Methods

Example 3: Methods with Return Types

Methods can return values after performing specific operations. Here's an example:


class Rectangle {
  public:
    int area(int length, int width) {
        return length * width;
    }
};
    

Explanation:

The `area` method in the `Rectangle` class calculates and returns the area of a rectangle given its length and width.

Accessing Class Members

Example 4: Using Class Members

Methods often interact with class data members. Here's an example of how a method can access and modify class members:


class Circle {
  private:
    double radius;

  public:
    void setRadius(double r) {
        radius = r;
    }

    double getRadius() {
        return radius;
    }
};
    

Explanation:

The `Circle` class has a private member `radius`. The `setRadius` method assigns a value to `radius`, and the `getRadius` method returns the current value of `radius`.

Const Methods

Example 5: Const Methods

Const methods are those that do not modify any data members of the class. They are declared with the `const` keyword:


class Student {
  private:
    string name;

  public:
    Student(string n) : name(n) {}

    string getName() const {
        return name;
    }
};
    

Explanation:

The `getName` method in the `Student` class is a const method, meaning it cannot modify the member `name` and is safe to call on const objects.

Static Methods

Example 6: Static Methods

Static methods belong to the class rather than any object instance. They can be called without creating an object of the class:


class MathUtils {
  public:
    static int square(int x) {
        return x * x;
    }
};
    

Explanation:

The `square` method in the `MathUtils` class is static, allowing it to be called using the class name without instantiating an object.

Overloading Methods

Example 7: Method Overloading

Method overloading allows multiple methods to have the same name but different parameter lists:


class Print {
  public:
    void display(int i) {
        std::cout << "Integer: " << i << std::endl;
    }

    void display(double d) {
        std::cout << "Double: " << d << std::endl;
    }
};
    

Explanation:

The `Print` class has two overloaded `display` methods, one for integers and another for doubles. The appropriate method is called based on the argument type.

Inline Methods

Example 8: Inline Methods

Inline methods are defined inside the class definition and are expanded at the point where they are called:


class Box {
  public:
    inline int volume(int length, int width, int height) {
        return length * width * height;
    }
};
    

Explanation:

The `volume` method in the `Box` class is defined as inline, which suggests to the compiler to insert the method's code directly into the calling code to reduce function call overhead.

Virtual Methods

Example 9: Virtual Methods

Virtual methods allow derived classes to override methods in base classes, enabling polymorphism:


class Base {
  public:
    virtual void show() {
        std::cout << "Base class show function." << std::endl;
    }
};

class Derived : public Base {
  public:
    void show() override {
        std::cout << "Derived class show function." << std::endl;
    }
};
    

Explanation:

The `show` method in the `Base` class is virtual, allowing the `Derived` class to override it. This enables the correct method to be called based on the object type at runtime.

Pure Virtual Methods

Example 10: Pure Virtual Methods

Pure virtual methods define an interface in abstract base classes that must be implemented by derived classes:


class AbstractBase {
  public:
    virtual void pureVirtualFunction() = 0;
};

class ConcreteClass : public AbstractBase {
  public:
    void pureVirtualFunction() override {
        std::cout << "Implementation of pure virtual function." << std::endl;
    }
};
    

Explanation:

The `pureVirtualFunction` in `AbstractBase` is a pure virtual method, making `AbstractBase` an abstract class. The `ConcreteClass` provides an implementation for this method.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025