Tutorial by Examples

A class is a user-defined type. A class is introduced with the class, struct or union keyword. In colloquial usage, the term "class" usually refers only to non-union classes. A class is a collection of class members, which can be: member variables (also called "fields"), mem...
There are three keywords that act as access specifiers. These limit the access to class members following the specifier, until another specifier changes the access level again: KeywordDescriptionpublicEveryone has accessprotectedOnly the class itself, derived classes and friends have accessprivate...
Classes/structs can have inheritance relations. If a class/struct B inherits from a class/struct A, this means that B has as a parent A. We say that B is a derived class/struct from A, and A is the base class/struct. struct A { public: int p1; protected: int p2; private: int p3;...
When using inheritance, you can specify the virtual keyword: struct A{}; struct B: public virtual A{}; When class B has virtual base A it means that A will reside in most derived class of inheritance tree, and thus that most derived class is also responsible for initializing that virtual base: ...
Aside from single inheritance: class A {}; class B : public A {}; You can also have multiple inheritance: class A {}; class B {}; class C : public A, public B {}; C will now have inherit from A and B at the same time. Note: this can lead to ambiguity if the same names are used in multipl...
To access member variables and member functions of an object of a class, the . operator is used: struct SomeStruct { int a; int b; void foo() {} }; SomeStruct var; // Accessing member variable a in var. std::cout << var.a << std::endl; // Assigning member variable b in v...
Private inheritance is useful when it is required to restrict the public interface of the class: class A { public: int move(); int turn(); }; class B : private A { public: using A::turn; }; B b; b.move(); // compile error b.turn(); // OK This approach efficiently pr...
C++11 Deriving a class may be forbidden with final specifier. Let's declare a final class: class A final { }; Now any attempt to subclass it will cause a compilation error: // Compilation error: cannot derive from final class: class B : public A { }; Final class may appear anywhere in cl...
The friend keyword is used to give other classes and functions access to private and protected members of the class, even through they are defined outside the class`s scope. class Animal{ private: double weight; double height; public: friend void printWeight(Animal animal); fr...
A class or struct can also contain another class/struct definition inside itself, which is called a "nested class"; in this situation, the containing class is referred to as the "enclosing class". The nested class definition is considered to be a member of the enclosing class, b...
A class or struct can also define member type aliases, which are type aliases contained within, and treated as members of, the class itself. struct IHaveATypedef { typedef int MyTypedef; }; struct IHaveATemplateTypedef { template<typename T> using MyTemplateTypedef = std::v...
A class is also allowed to have static members, which can be either variables or functions. These are considered to be in the class' scope, but aren't treated as normal members; they have static storage duration (they exist from the start of the program to the end), aren't tied to a particular inst...
A class can have non-static member functions, which operate on individual instances of the class. class CL { public: void member_function() {} }; These functions are called on an instance of the class, like so: CL instance; instance.member_function(); They can be defined either ins...
Unnamed struct is allowed (type has no name) void foo() { struct /* No name */ { float x; float y; } point; point.x = 42; } or struct Circle { struct /* No name */ { float x; float y; } center; // but a member name float...

Page 1 of 1