DEV Community

Jess
Jess

Posted on

C++ Class Basics

C++ Class Basics

Classes are a user defined data type that contain components known as members. These members can be private or public variables or functions. By default, all members of a class are private, which means they cannot be accessed outside of the class.

Classes are used as blueprints to create objects, or instances, of a class. For example, if you had a class Dog, you can create many instances of Dog.

class Dog
{
private:
  // private member variables cannot be accessed outside of the class
  std::string m_name;
public:
  void speak()
  {
    std::cout << "Woof! I'm " << m_name << "!\n" ;
  }
  void setName(std::string name)
  {
    // since m_name cannot be accessed directly, a member function can be used to indirectly set a Dog's name
    m_name = name;
  }
}; // <- don't forget the semi-colon

int main() {
  Dog penny;

  // members are accessed using the 'dot operator', or member access operator
  penny.setName("Penny");
  penny.speak();

  Dog pixel;
  pixel.setName("Pixel");
  pixel.speak();

  return 0;
}

// output
Woof! I'm Penny!
Woof! I'm Pixel!
Enter fullscreen mode Exit fullscreen mode

You can initialize member variables upon instantiation by using a constructor function. A constructor has no type and uses the same name as the class. You can also set default parameters which will be set if no other value is provided. A default constructor is automatically executed when a class object is declared, whether you explicitly create one or not.

class Dog
{
// private members ...
public:
  Dog(std::string name = "Gremlin")
  {
    m_name = name;
  }

// more public members ...
};

int main()
{

  // using setName
  Dog penny;
  penny.setName("Penny");
  penny.speak();

  // using constructor
  Dog pixel("Pixel");
  pixel.speak();

  // using default parameter
  Dog unnamed;
  unnamed.speak()
}

// output
Woof! I'm Penny!
Woof! I'm Pixel!
Woof! I'm Gremlin!
Enter fullscreen mode Exit fullscreen mode

Classes also have functions called destructors, that are automatically invoked when the object goes out of scope or is destroyed.

class Dog()
{
  // ...
public:
  Dog(); // constructor
  ~Dog(); // destructor
};
Enter fullscreen mode Exit fullscreen mode

Like a constructor, a destructor is automatically created if not explicitly defined and has the same name as the class, but it's preceded by a tilde (~). A destructor takes no arguments.

Moving a class into separate files

You will want to move a class into its own files in order to keep the implementation private, keep your main file clean, and allow the class to be used in other files/programs.

There are 2 types of files that need to be created: the implementation file (.cpp) and header file (.h or .hpp). I wrote about header files here if you want to check that out.

The implementation file contains the definitions of the class' member functions and the header file contains the function prototypes and member variables.

// Dog.hpp


#ifndef Dog_hpp
#define Dog_hpp
#include <string>

class Dog
{
private:
  std::string m_name; // variable to store name
  int m_age;          // variable to store age
public:
  // constructor function
  Dog(std::string name = "Gremlin");

  // function to output dog speaking its name to the console
  void speak();

  // function to set the dog's name
  void setName(std::string name);
};

#endif /* Dog_hpp */
Enter fullscreen mode Exit fullscreen mode
// Dog.cpp

#include "Dog.hpp"
#include <iostream>

Dog::Dog(std::string name)
{
  m_name = name;
}

void Dog::speak()
{
  std::cout << "Woof! I'm " << m_name << "!\n" ;
}

void Dog::setName(std::string name)
{
  m_name = name;
}
Enter fullscreen mode Exit fullscreen mode
// main.cpp

#include "Dog.hpp"

int main() {
  Dog penny;
  penny.setName("Penny");
  penny.speak();

  Dog pixel("Pixel");
  pixel.speak();

  Dog unnamed;
  unnamed.speak();

  return 0;
}

// output
Woof! I'm Penny!
Woof! I'm Pixel!
Woof! I'm Gremlin!
Enter fullscreen mode Exit fullscreen mode

Further Reading / References

Top comments (0)