What Are Member Functions

beginner c++11 classes

In C++ a member function (also known as a method) is a function declared in a class’s scope. Non-static member functions have access to all of a class’s public, protected, and private data as well as public and protected data from any base classes. Non-static member functions also each have a special this variable for referring to the object the member function was called on.

#include <iostream>
#include <string>

class Dog {
public:
  // A member function called "set_name"
  void set_name(std::string name) {
    // Use `this` to make it clear we're assigning the name field
    // and not the name parameter
    this->name = std::move(name);
  }

  std::string name;
};

int main() {
  Dog d;
  d.set_name("Doge");

  std::cout << d.name << "\n";
}
Doge

In This Article

Const Member Functions

Member functions that only read data but do not modify it can be marked const. Compilers will verify at compile-time that const member functions do not modify internal data and will raise errors if they do.

#include <iostream>
#include <string>

class Dog {
public:
  void set_name(std::string name) {
    this->name = std::move(name);
  }

  // get_name can be const because it does not modify name
  std::string get_name() const {
    return name;
  }

private:
  std::string name;
};

int main() {
  Dog d;
  d.set_name("Doge");

  std::cout << d.get_name() << "\n";
}
Doge

Static Member Functions

Member functions can also be declared static. Static member functions cannot access individual object’s data but they can access class static data. That is, static in the context of classes in C++ means something is “class scope” instead of “object scope”. Because the scope of static member functions is the class level we use the class’s name to access static member functions, much like a namespace.

#include <iostream>
#include <string>

class Dog {
public:
  Dog() {
    dog_count++;
  }

  ~Dog() {
    dog_count--;
  }

  // A member function called "set_name"
  void set_name(std::string name) {
    // Use `this` to make it clear we're assigning the name field
    // and not the name parameter
    this->name = std::move(name);
  }

  std::string get_name() const {
    return name;
  }

  static int get_dog_count() {
    return dog_count;
  }

private:
  std::string name;

  // Count how many dog objects are in the program.
  static int dog_count;
};

// Static class data must be initialized out-of-line
int Dog::dog_count = 0;

int main() {
  Dog d;
  d.set_name("Doge");

  std::cout << d.get_name() << "\n";
  std::cout << Dog::get_dog_count() << " dog\n";

  Dog dog2;
  d.set_name("Good Boyo");
  
  std::cout << d.get_name() << "\n";
  std::cout << Dog::get_dog_count() << " dogs\n";
}
Doge
1 dog
Good Boyo
2 dogs

Virtual Member Functions

Virtual member functions enable polymorphism in C++. Virtual member functions in base classes can be overridden in derived classes by using the override keyword. Virtual member functions can be const just like regular member functions. When a const virtual member function is overridden the override must also be const.

#include <iostream>
#include <string>

class Animal {
public:
  virtual int get_number_of_legs() const {
    return 0;
  }
};

class Dog : public Animal {
public:
  void set_name(std::string name) {
    this->name = std::move(name);
  }

  std::string get_name() const {
    return name;
  }

  int get_number_of_legs() const override {
    return 4;
  }

private:
  std::string name;
};

class Centipede : public Animal {
public:
  int get_number_of_legs() const override {
    return 100;
  }
};

int main() {
  Animal a;
  std::cout << a.get_number_of_legs() << "\n";

  Dog d;
  d.set_name("Doge");

  std::cout << d.get_name() << " has ";
  std::cout << d.get_number_of_legs() << " legs.\n";

  Centipede c;
  std::cout << c.get_number_of_legs() << "\n";
}
0
Doge has 4 legs.
100

Abstract Member Functions

Abstract member functions are virtual member functions without an implementation. Any class with an abstract member function is called an “abstract class”. To denote an abstract virtual member function we use the = 0 syntax.

#include <iostream>
#include <string>

class Animal {
public:
  virtual int get_number_of_legs() const = 0;
};

class Dog : public Animal {
public:
  void set_name(std::string name) {
    this->name = std::move(name);
  }

  std::string get_name() const {
    return name;
  }

  int get_number_of_legs() const override {
    return 4;
  }

private:
  std::string name;
};

class Centipede : public Animal {
public:
  int get_number_of_legs() const override {
    return 100;
  }
};

int main() {
  Dog d;
  d.set_name("Doge");

  std::cout << d.get_name() << " has ";
  std::cout << d.get_number_of_legs() << " legs.\n";

  Centipede c;
  std::cout << c.get_number_of_legs() << "\n";
}
Doge has 4 legs.
100

Final Member Functions

Final member functions are virtual functions that are not allowed to be overridden by future derived classes. Because final can only be applied to virtual functions that are being overridden you can omit the override keyword.

#include <iostream>
#include <string>

class Animal {
public:
  virtual int get_number_of_legs() const = 0;
};

class Dog : public Animal {
public:
  void set_name(std::string name) {
    this->name = std::move(name);
  }

  std::string get_name() const {
    return name;
  }

  int get_number_of_legs() const final {
    return 4;
  }

private:
  std::string name;
};

class Centipede : public Animal {
public:
  int get_number_of_legs() const {
    return 100;
  }
};

class StoneCentipede : public Centipede {
public:
  int get_number_of_legs() const final {
    return 15;
  }
};

int main() {
  Dog d;
  d.set_name("Doge");

  std::cout << d.get_name() << " has ";
  std::cout << d.get_number_of_legs() << " legs.\n";

  Centipede c;
  std::cout << "Centipedes have ";
  std::cout << c.get_number_of_legs() << " legs.\n";

  StoneCentipede sc;
  std::cout << "Stone Centipedes have ";
  std::cout << sc.get_number_of_legs() << " legs.\n";
}
Doge has 4 legs.
Centipedes have 100 legs.
Stone Centipedes have 15 legs.


For more C++ By Example, click here.