C++ Class Methods

Welcome to The Coding College! In this tutorial, we’ll dive into class methods in C++. Class methods are the functions that define the behavior of a class. They are used to perform actions on the data stored in class objects.

What Are Class Methods?

Class methods are functions declared inside a class that operate on the class’s attributes or perform tasks related to the class.

  • Member Functions: These are methods that belong to a class.
  • Methods can access the attributes of an object using the this pointer.

Declaring and Defining Methods

Class methods can be:

  1. Declared and defined inside the class.
  2. Declared inside the class and defined outside using the scope resolution operator (::).

Example: Declaring and Defining Inside the Class

#include <iostream>
using namespace std;

class Car {
public:
    string brand;
    int year;

    void displayInfo() {  // Method definition inside the class
        cout << "Brand: " << brand << ", Year: " << year << endl;
    }
};

int main() {
    Car car1;
    car1.brand = "Toyota";
    car1.year = 2021;

    car1.displayInfo();  // Call the method
    return 0;
}

Output:

Brand: Toyota, Year: 2021  

Example: Declaring Inside and Defining Outside the Class

#include <iostream>
using namespace std;

class Car {
public:
    string brand;
    int year;

    void displayInfo();  // Method declaration
};

// Method definition outside the class
void Car::displayInfo() {
    cout << "Brand: " << brand << ", Year: " << year << endl;
}

int main() {
    Car car1;
    car1.brand = "Honda";
    car1.year = 2020;

    car1.displayInfo();  // Call the method
    return 0;
}

Output:

Brand: Honda, Year: 2020  

Access Specifiers for Methods

Class methods can have public, private, or protected access specifiers:

  • Public: Accessible from outside the class.
  • Private: Can only be called within the class.
  • Protected: Accessible within the class and derived classes.

Example: Private Methods

#include <iostream>
using namespace std;

class Calculator {
private:
    int add(int a, int b) {  // Private method
        return a + b;
    }

public:
    void displaySum(int a, int b) {
        cout << "Sum: " << add(a, b) << endl;  // Access private method
    }
};

int main() {
    Calculator calc;
    calc.displaySum(5, 10);  // Calls the public method
    return 0;
}

Output:

Sum: 15  

Constructor as a Class Method

A constructor is a special method used to initialize objects when they are created.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    // Constructor
    Student(string n, int a) {
        name = n;
        age = a;
    }

    void displayInfo() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Student student1("Alice", 20);  // Constructor is called
    student1.displayInfo();
    return 0;
}

Output:

Name: Alice, Age: 20  

Static Methods

Static methods belong to the class rather than any specific object. They can only access static members of the class and are called using the class name.

Example:

#include <iostream>
using namespace std;

class Math {
public:
    static int add(int a, int b) {  // Static method
        return a + b;
    }
};

int main() {
    cout << "Sum: " << Math::add(10, 20) << endl;  // Call static method
    return 0;
}

Output:

Sum: 30  

Overloading Methods

C++ supports method overloading, where multiple methods have the same name but differ in the number or type of parameters.

Example:

#include <iostream>
using namespace std;

class Printer {
public:
    void print(int num) {
        cout << "Printing integer: " << num << endl;
    }

    void print(string text) {
        cout << "Printing string: " << text << endl;
    }
};

int main() {
    Printer p;
    p.print(42);           // Calls the method with an int parameter
    p.print("Hello World"); // Calls the method with a string parameter
    return 0;
}

Output:

Printing integer: 42  
Printing string: Hello World  

Constant Methods

Constant methods are declared with the const keyword and cannot modify the object’s data members.

Example:

#include <iostream>
using namespace std;

class Book {
private:
    string title;

public:
    Book(string t) {
        title = t;
    }

    void displayTitle() const {  // Const method
        cout << "Title: " << title << endl;
    }
};

int main() {
    Book book1("C++ Programming");
    book1.displayTitle();  // Call constant method
    return 0;
}

Output:

Title: C++ Programming  

Practical Example: Employee Management

#include <iostream>
using namespace std;

class Employee {
private:
    string name;
    double salary;

public:
    Employee(string n, double s) {  // Constructor
        name = n;
        salary = s;
    }

    void giveRaise(double percent) {
        salary += salary * (percent / 100);
    }

    void displayInfo() {
        cout << "Name: " << name << ", Salary: $" << salary << endl;
    }
};

int main() {
    Employee emp1("John Doe", 50000);
    emp1.displayInfo();

    emp1.giveRaise(10);  // Give a 10% raise
    emp1.displayInfo();

    return 0;
}

Output:

Name: John Doe, Salary: $50000  
Name: John Doe, Salary: $55000  

Advantages of Class Methods

  1. Encapsulation: Keep behavior closely tied to data.
  2. Modularity: Makes code more organized and reusable.
  3. Flexibility: Static, constant, and overloaded methods provide versatility.
  4. Abstraction: Hide implementation details and expose only necessary functionality.

Explore More at The Coding College

Learn more about advanced C++ topics and improve your coding skills at The Coding College.

Leave a Comment