The Wayback Machine - https://web.archive.org/web/20240213201413/https://www.geeksforgeeks.org/cpp-class-methods/
Open In App
Related Articles

C++ Class Methods

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisite: Class in C++

Class is a blueprint of an object, which has data members and member functions also known as methods. A method is a procedure or function in the oops concept. A method is a function that belongs to a class.

There are two ways to define a procedure or function that belongs to a class:

  • Inside Class Definition
  • Outside Class Definition

1. Inside Class Definition

The member function is defined inside the class definition it can be defined directly. Similar to accessing a data member in the class we can also access the public member functions through the class object using the dot operator (.).

Syntax:

class class_name{
public:
return_type Method_name()  // method inside class definition
{
  // body of member function
}
};

Example:

C++

// C++ program for Inside Class Definition
#include <iostream>
using namespace std;
 
class rectangle {
private:
    int length;
    int breadth;
 
public:
    // Constructor
    rectangle(int length, int breadth)
    {
        this->length = length; // this pointer
        this->breadth = breadth;
    }
 
    // area() function inside class
    int area() { return (length * breadth); }
 
    // perimeter() function outside class
    int perimeter() { return 2 * (length + breadth); }
};
 
int main()
{
    // Creating object
    rectangle r(2, 3);
    cout << "perimeter: " << r.perimeter() << endl;
    cout << "area: " << r.area() << endl;
    return 0;
}

                    

Output
perimeter: 10
area: 6

2. Outside Class Definition

The member function is defined outside the class definition it can be defined using the scope resolution operator. Similar to accessing a data member in the class we can also access the public member functions through the class object using the dot operator (.).

Syntax:

class Class_name{

public:

return_type Method_name(); // method outside class definition

};

// Outside the Class using scope resolution operator

return_type Class_name :: Method_name() {

  // body of member function

}

Example: In the following code, a rectangle class, in which member function area() and member function perimeter() is defined outside the class by using the scope resolution operator.

C++

// C++ program for Outside the Class Definition
#include <iostream>
using namespace std;
 
class rectangle {
private:
    int length;
    int breadth;
 
public:
    // Constructor
    rectangle(int length, int breadth)
    {
        this->length = length; // this pointer
        this->breadth = breadth;
    }
 
    // area() function defined outside the class
    int area();
 
    // perimeter() function defined outside the class
    int perimeter();
};
 
// function defining using scope resolution operator "::"
int rectangle::area() { return (length * breadth); }
 
int rectangle::perimeter()
{
    return 2 * (length + breadth);
}
 
int main()
{
    // Creating object
    rectangle r(2, 3);
    cout << "perimeter: " << r.perimeter() << endl;
    cout << "area: " << r.area() << endl;
    return 0;
}

                    

Output
perimeter: 10
area: 6

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 19 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials