Exploring C++ Member Functions - CSU1287 - Shoolini U

Member Functions

Member functions

Member functions are special functions in C++ that are defined within a class and operate on objects of that class. They are an essential feature of object-oriented programming, as they allow you to encapsulate behavior and data within a single unit. Member functions can be used to manipulate the data members of an object, perform calculations, and interact with other objects.

Member functions are defined within the class definition using the function keyword. They can be declared in either the public or private sections of the class, depending on whether they should be accessible from outside the class or not. Member functions are called using the object name and the dot operator (.), just like accessing data members.

Here's an example of a class with member functions:

class Rectangle {
public:
  Rectangle(int l, int w) {
    length = l;
    width = w;
  }
  int area() {
    return length * width;
  }
  void setLength(int l) {
    length = l;
  }
  void setWidth(int w) {
    width = w;
  }
private:
  int length;
  int width;
};

In this example, the Rectangle class has a constructor that takes two integers as arguments and initializes the length and width data members. The Rectangle class also has three member functions: area(), setLength(), and setWidth().

The area() member function calculates the area of the rectangle by multiplying the length and width data members and returns the result as an integer. The setLength() and setWidth() member functions are used to set the length and width data members, respectively. They take an integer argument and assign the value to the corresponding data member.

Member functions can also be overloaded, just like regular functions, to provide multiple versions of the same function with different parameter lists. This allows you to provide more flexibility in how the function can be used.

Here's an example of a class with overloaded member functions:

class Math {
public:
  int add(int x, int y) {
    return x + y;
  }
  double add(double x, double y) {
    return x + y;
  }
};

In this example, the Math class has two member functions named add(). One takes two integers as arguments and returns their sum as an integer. The other takes two doubles as arguments and returns their sum as a double. This allows the add() function to be used with different data types, providing more flexibility and reusability.

Member functions can also be const, which means they do not modify the object's data members. Const member functions are declared using the const keyword after the function name and before the function body. Const member functions can be called on const objects, which ensures that the object's data members are not modified.

Here's an example of a class with const member functions:

class Person {
public:
  Person(const char* name) {
    this->name = name;
  }
  const char* getName() const {
    return name;
  }
private:
  const char* name;
};

In this example, the Person class has a constructor that takes a C-style string as an argument and initializes the name data member. The Person class also has a const member function named getName() that returns the value of the name data member. The const keyword after the function name indicates that the function does not modify the object's data members.

In summary, member functions are an essential feature of object-oriented programming in C++. They are defined within a class and operate on objects of that class, allowing you to encapsulate behavior and data within a single unit. Member functions can be used to manipulate data members, perform calculations, and interact with other objects. Member functions can also be overloaded and const, providing more flexibility and reusability.