Friend functions In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not apply to "friends". Friends are functions or classes declared with the friend keyword. A non-member function can access the private and protected members of a class … Continue reading Friendship and inheritance
Special members
[NOTE: This chapter requires proper understanding of dynamically allocated memory] Special member functions are member functions that are implicitly defined as member of classes under certain circumstances. There are six: Member function typical form for class C: Default constructor C::C(); Destructor C::~C(); Copy constructor C::C (const C&); Copy assignment C& operator= (const C&); Move constructor … Continue reading Special members
Classes (II)
Overloading operators Classes, essentially, define new types to be used in C++ code. And types in C++ not only interact with code by means of constructions and assignments. They also interact by means of operators. For example, take the following operation on fundamental types: 1 2 int a, b, c; a = b + c; … Continue reading Classes (II)
Classes (I)
Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are defined using either keyword … Continue reading Classes (I)
Other data types
Type aliases (typedef / using) A type alias is a different name by which a type can be identified. In C++, any valid type can be aliased so that it can be referred to with a different identifier. In C++, there are two syntaxes for creating such type aliases: The first, inherited from the C … Continue reading Other data types
Data structures
Data structures A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures can be declared in C++ using the following syntax: struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; Where type_name … Continue reading Data structures
Dynamic memory
In the programs seen in previous chapters, all memory needs were determined before program execution by defining the variables needed. But there may be cases where the memory needs of a program can only be determined during runtime. For example, when the memory needed depends on user input. On these cases, programs need to dynamically … Continue reading Dynamic memory
Pointers
In earlier chapters, variables have been explained as locations in the computer's memory which can be accessed by their identifier (their name). This way, the program does not need to care about the physical address of the data in memory; it simply uses the identifier whenever it needs to refer to the variable. For a … Continue reading Pointers
Character sequences
The string class has been briefly introduced in an earlier chapter. It is a very powerful class to handle and manipulate strings of characters. However, because strings are, in fact, sequences of characters, we can represent them also as plain arrays of elements of a character type. For example, the following array: char foo … Continue reading Character sequences
Arrays
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its … Continue reading Arrays