Loading...

Introduction to Classes in C++

Learn the fundamentals of classes in C++, including class definitions, objects, members, and object-oriented programming concepts.

The most fundamental building block of object-oriented programming (OOP) is the class. In C++, classes group data (properties) and the behaviors belonging to that data (methods) under a single structure. In this article, we’ll learn how to define a class, create objects, access members, and build classes that relate to each other.


1. What Is a Class?

A class is an abstract model of a concept or entity. You can think of it as the program’s representation of a real-world object. For example, a “Book” object has properties such as author, publisher, and page count.

A class in C++ is defined like this:


class ClassName {
public:
    // Fields (data members)
    // Methods (member functions)
};

2. Book Class: A Simple Example

In the example below, a Book class is defined. This class contains the basic properties and behaviors of a book.


#include <iostream>
#include <string>
using namespace std;

class Book {
private:
    string title;
    string author;
    int pageCount;
    double price;
    bool available;

public:
    // Constructor
    Book(string t, string a, int p, double pr, bool av)
        : title(t), author(a), pageCount(p), price(pr), available(av) {}

    // Getter and setter methods
    string GetTitle() const { return title; }
    void SetTitle(string t) { title = t; }

    string GetAuthor() const { return author; }
    void SetAuthor(string a) { author = a; }

    int GetPageCount() const { return pageCount; }

    void DisplayInfo() const {
        cout << "Book: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Pages: " << pageCount << endl;
        cout << "Price: " << price << " TL" << endl;
        cout << (available ? "In stock" : "Out of stock") << endl;
    }
};

private members can only be accessed inside the class. This feature is called encapsulation. public methods, on the other hand, can be called from outside the class.


3. Using the Book Class

The defined class can now be used like a data type. We can create objects of type Book and call their methods.


int main() {
    Book b1("C++ Programming", "Bjarne Stroustrup", 1350, 240.0, true);

    b1.DisplayInfo();

    b1.SetTitle("Modern C++ Programming");
    cout << "\nNew Title: " << b1.GetTitle() << endl;

    return 0;
}

Output:


Book: C++ Programming
Author: Bjarne Stroustrup
Pages: 1350
Price: 240 TL
In stock

New Title: Modern C++ Programming

4. Related Classes: Author and Publisher

In the real world, a book doesn’t only have a title — it also has an author and a publisher. In this case, we can use other classes inside the Book class through composition.


class Author {
private:
    string name;
    string country;

public:
    Author(string n, string c) : name(n), country(c) {}
    string GetInfo() const { return name + " (" + country + ")"; }
};

class Publisher {
private:
    string name;
    string city;

public:
    Publisher(string n, string c) : name(n), city(c) {}
    string GetInfo() const { return name + " - " + city; }
};

class Book {
private:
    string title;
    Author author;
    Publisher publisher;
    int pageCount;

public:
    Book(string t, Author a, Publisher p, int pages)
        : title(t), author(a), publisher(p), pageCount(pages) {}

    void DisplayInfo() const {
        cout << "Book: " << title << endl;
        cout << "Author: " << author.GetInfo() << endl;
        cout << "Publisher: " << publisher.GetInfo() << endl;
        cout << "Page Count: " << pageCount << endl;
    }
};

Here, the Book class contains both Author and Publisher. In other words, “A book has an Author and a Publisher.” — this is a has-a relationship.


5. Practical Example: Book Information System

The following example shows how these three classes can be used together.


#include <iostream>
#include <string>
using namespace std;

class Author {
private:
    string name;
    string country;

public:
    Author(string n, string c) : name(n), country(c) {}
    string GetName() const { return name; }
    string GetCountry() const { return country; }
};

class Publisher {
private:
    string name;
    int foundedYear;

public:
    Publisher(string n, int year) : name(n), foundedYear(year) {}
    string GetInfo() const {
        return name + " (" + to_string(foundedYear) + ")";
    }
};

class Book {
private:
    string title;
    Author author;
    Publisher publisher;
    int pageCount;
    double price;

public:
    Book(string t, Author a, Publisher p, int pages, double pr)
        : title(t), author(a), publisher(p), pageCount(pages), price(pr) {}

    void DisplayInfo() const {
        cout << "=== Book Info ===" << endl;
        cout << "Title: " << title << endl;
        cout << "Author: " << author.GetName()
             << " (" << author.GetCountry() << ")" << endl;
        cout << "Publisher: " << publisher.GetInfo() << endl;
        cout << "Page Count: " << pageCount << endl;
        cout << "Price: " << price << " TL" << endl;
    }
};

int main() {
    Author a("George Orwell", "United Kingdom");
    Publisher p("Penguin Books", 1935);
    Book b("1984", a, p, 328, 89.90);

    b.DisplayInfo();
    return 0;
}

Output:


=== Book Info ===
Title: 1984
Author: George Orwell (United Kingdom)
Publisher: Penguin Books (1935)
Page Count: 328
Price: 89.9 TL

This structure is an example of object composition, where one object (Book) can contain other objects (Author, Publisher). In real-world applications, such classes are often used as database models or API data structures.


6. TL;DR

  • Classes keep data (properties) and behavior (methods) together.
  • private members are only accessible inside the class (encapsulation).
  • public methods provide controlled access to the outside world (getters/setters).
  • In the Book, Author, and Publisher example, a “has-a” relationship was used.
  • Constructors are used to initialize properties when creating an object.
  • All examples can be run on Visual Studio 2022 or GCC 11+ compilers.

Related Articles