[ XF9 ]

Class Definitions (C++)

Posted on in C++.

If you want to use OOP with C++ class definitions are a must. This is a short walk-through how to define your own class.

A class definition usually consists of two files: a .h file and .cpp file. The .h file, also known as header file, contains the basic definitions of the class the .cpp file the actual implementation.

Let’s start with the header file right away:

#pragma once
#include <string>

class Book
{
public:
	Book(std::string title, std::string author);
	~Book();

	std::string getTitle();
	std::string getAuthor();
private:

protected:
	std::string title;
	std::string author;
};

#pragma once is a so called include guard. It prevents the compiler from including this header file multiple times, removing unneeded overhead and preventing ring includes. A ring include would occur if you have a class A which includes class B which includes class A again, which includes B which includes A ..
So #pragma once prevents that – but keep in mind it isn’t standardised, which means in some rare cases it won’t work and you need to include a include guard in another way.

The real class definition starts with the keyword class followed by the class name which usually starts with an upper case letter.

Next are the access specifiers which define who can access the different properties of this class. There are three possible specifiers:

  • public – this property can be accessed by every instance
  • private – this property can be accessed by this instance only
  • protected – this property can be accessed by this instance and any instance inheriting from this class

As you can see we have four function definitions and two variable definitions. Note that all variable definitions are private and there are two public function to get there values. This way we can limit the write access to these variables so nobody can mess with us. If you want to set the value of these variables you could use another public function like void setValue(Type value). This way you can add different checks before setting the variable e.g. limiting the value to a certain range.

The other two functions are constructor (the first) and destructor (the second). These are specific functions which are called once you create an instance of a class or destroy an instance. Note that these two functions don’t have return values!

Now let’s have a look at the .cpp file:

#include "Book.h"

Book::Book(std::string title, std::string author)
{
	this->title = title;
	this->author = author;
}


Book::~Book() { }

std::string Book::getTitle(){
	return this->title;
}

std::string Book::getAuthor(){
	return this->author;
}

First thing we want to do is include the header file we just wrote. Note the use of quote marks: these tell the compiler to look for the to be included file in your working directory instead of his default include location.

The definition of functions is pretty straight forward:
[return type] [class name]::[function name]([arguments])

Let’s have a closer look at the constructor: he gets two arguments which then he assigns to the variables of the instance that is currently created. this is a pointer, pointing at the instance to be created – so we are accessing the instance and assign the value of the argument title to the instance variable title.

The destructor is left empty since there is nothing to do when deleting this objects since all properties will be deleted automatically.


Write a reply or comment

Your email address will not be published.