C++
incorporates the option to use standard operators to perform operations with
classes in addition to with fundamental types. For example:
|
1 |
int
|
This is
obviously valid code in C++, since the different variables of the addition are
all fundamental types. Nevertheless, it is not so obvious that we could perform
an operation similar to the following one:
|
1 |
struct
|
In fact,
this will cause a compilation error, since we have not defined the behavior our
class should have with addition operations. However, thanks to the C++ feature
to overload operators, we can design classes able to perform operations using
standard operators. Here is a list of all the operators that can be overloaded:
|
Overloadable
operators |
+ - * / = < > += -= *= /= << >><<= >>= == != <= >= ++ -- % & ^ ! |~ &= ^= |= && || %= [] () , ->* -> new delete new[] delete[] |
To
overload an operator in order to use it with classes we declare operator
functions, which are regular functions whose names are the operator keyword followed by the operator sign
that we want to overload. The format is:
type operator sign (parameters) { /*...*/
}
Here you
have an example that overloads the addition operator (+). We are going to create a class to store
bidimensional vectors and then we are going to add two of them: a(3,1) and b(1,2).
The addition of two bidimensional vectors is an operation as simple as adding
the two x coordinates to obtain the
resulting x coordinate and adding the
two y coordinates to obtain the
resulting y. In this case the result
will be (3+1,1+2) = (4,3).
|
1 |
// vectors: overloading operators example#include <iostream>using class
int
|
4,3 |
It may be
a little confusing to see so many times the CVector
identifier. But, consider that some of them refer to the class name (type) CVector and some others are functions with that
name (constructors must have the same name as the class). Do not confuse
them:
|
1 |
|
The
function operator+ of class CVector is the one that is in charge of
overloading the addition operator (+). This
function can be called either implicitly using the operator, or explicitly
using the function name:
|
1 |
|
Both
expressions are equivalent.
Notice
also that we have included the empty constructor (without parameters) and we
have defined it with an empty block:
|
|
|
This is
necessary, since we have explicitly declared another constructor:
|
|
|
And when
we explicitly declare any constructor, with any number of parameters, the
default constructor with no parameters that the compiler can declare
automatically is not declared, so we need to declare it ourselves in order to
be able to construct objects of this type without parameters. Otherwise, the
declaration:
|
|
|
included in main()
would not have been valid.
Anyway, I have to warn you that an empty
block is a bad implementation for a constructor, since it does not fulfill the
minimum functionality that is generally expected from a constructor, which is
the initialization of all the member variables in its class. In our case this
constructor leaves the variables x and y undefined. Therefore, a more advisable definition
would have been something similar to this:
|
|
|
which in
order to simplify and show only the point of the code I have not included in
the example.
As well as a class includes a default
constructor and a copy constructor even if they are not declared, it also
includes a default definition for the assignment operator (=) with the class itself as parameter. The
behavior which is defined by default is to copy the whole content of the data
members of the object passed as argument (the one at the right side of the
sign) to the one at the left side:
|
1 |
|
The copy assignment operator function is
the only operator member function implemented by default. Of course, you can
redefine it to any other functionality that you want, like for example, copy
only certain class members or perform additional initialization procedures.
The overload of operators does not force
its operation to bear a relation to the mathematical or usual meaning of the
operator, although it is recommended. For example, the code may not be very
intuitive if you use operator + to subtract two
classes or operator== to fill with
zeros a class, although it is perfectly possible to do so.
Although the prototype of a function operator+ can seem obvious since it takes what
is at the right side of the operator as the parameter for the operator member
function of the object at its left side, other operators may not be so obvious.
Here you have a table with a summary on how the different operator functions
have to be declared (replace @ by the operator in each case):
|
Expression |
Operator |
Member
function |
Global
function |
|
@a |
+ - * & ! ~ ++ -- |
A::operator@() |
operator@(A) |
|
a@ |
++ -- |
A::operator@(int) |
operator@(A,int) |
|
a@b |
+ - * / % ^ & | < > == != <= >= <<
>> && || , |
A::operator@ (B) |
operator@(A,B) |
|
a@b |
= += -= *= /= %= ^= &= |= <<= >>= [] |
A::operator@ (B) |
- |
|
a(b, c...) |
() |
A::operator() (B, C...) |
- |
|
a->x |
-> |
A::operator->() |
- |
Where a
is an object of class A, b is an object of class B and c is an
object of class C.
You can see in this panel that there are
two ways to overload some class operators: as a member function and as a global
function. Its use is indistinct, nevertheless I remind you that functions that
are not members of a class cannot access the private or protected members of
that class unless the global function is its friend (friendship is explained
later).
The keyword this
represents a pointer to the object whose member function is being executed. It
is a pointer to the object itself.
One of its uses can be to check if a
parameter passed to a member function is the object itself. For example,
|
1 |
// this#include <iostream>using class
int
int
|
yes, &a is b |
It is
also frequently used in operator= member functions
that return objects by reference (avoiding the use of temporary objects).
Following with the vector's examples seen before we could have written an operator= function similar to this one:
|
1 |
|
In fact this function is very similar to
the code that the compiler generates implicitly for this class if we do not include
an operator= member function
to copy objects of this class.
A class can contain static members,
either data or functions.
Static data members of a class are also
known as "class variables", because there is only one unique value
for all the objects of that same class. Their content is not different from one
object of this class to another.
For example, it may be used for a variable
within a class that can contain a counter with the number of objects of that
class that are currently allocated, as in the following example:
|
1 |
// static members in classes#include <iostream>using class
intint
|
76 |
In fact, static members have the same
properties as global variables but they enjoy class scope. For that reason, and
to avoid them to be declared several times, we can only include the prototype
(its declaration) in the class declaration but not its definition (its
initialization). In order to initialize a static data-member we must include a
formal definition outside the class, in the global scope, as in the previous
example:
|
|
int |
Because it is a unique variable value for
all the objects of the same class, it can be referred to as a member of any
object of that class or even directly by the class name (of course this is only
valid for static members):
|
1 |
|
These two calls included in the previous
example are referring to the same variable: the static variable n within class CDummy
shared by all objects of this class.
Once again, I remind you that in fact it
is a global variable. The only difference is its name and possible access
restrictions outside its class.
Just as we may include static data within
a class, we can also include static functions. They represent the same: they
are global functions that are called as if they were object members of a given
class. They can only refer to static data, in no case to non-static members of
the class, as well as they do not allow the use of the keyword this, since it makes reference to an object
pointer and these functions in fact are not members of any object but direct
members of the class.
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 affect friends.
Friends are functions or classes declared
with the friend keyword.
If we want to declare an external function
as friend of a class, thus allowing this function to have access to the private
and protected members of this class, we do it by declaring a prototype of this
external function within the class, and preceding it with the keyword friend:
|
1 |
// friend functions#include <iostream>using class
void
int
|
24 |
The duplicate
function is a friend of CRectangle. From within
that function we have been able to access the members width and height
of different objects of type CRectangle,
which are private members. Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle. It isn't! It simply has access to
its private and protected members without being a member.
The friend functions can serve, for
example, to conduct operations between two different classes. Generally, the
use of friend functions is out of an object-oriented programming methodology,
so whenever possible it is better to use members of the same class to perform
operations with them. Such as in the previous example, it would have been
shorter to integrate duplicate() within the
class CRectangle.
Just as we have the possibility to define
a friend function, we can also define a class as friend of another one,
granting that first class access to the protected and private members of the
second one.
|
1 |
// friend class#include <iostream>using classclass
class
voidint
|
16 |
In this example, we have declared CRectangle as a friend of CSquare so that CRectangle
member functions could have access to the protected and private members of CSquare, more concretely to CSquare::side, which describes the side width of
the square.
You may also see something new at the
beginning of the program: an empty declaration of class CSquare. This is necessary because within the
declaration of CRectangle we refer to
CSquare (as a parameter in convert()).
The definition of CSquare is included later,
so if we did not include a previous empty declaration for CSquare this class would not be visible from
within the definition of CRectangle.
Consider that friendships are not
corresponded if we do not explicitly specify so. In our example, CRectangle is considered as a friend class by CSquare, but CRectangle
does not consider CSquare to be a friend, so CRectangle can access the protected and private
members of CSquare but not the reverse
way. Of course, we could have declared also CSquare
as friend of CRectangle if we wanted to.
Another property of friendships is that
they are not transitive: The friend of a friend is not considered to be
a friend unless explicitly specified.
A key feature of C++ classes is
inheritance. Inheritance allows to create classes which are derived from other
classes, so that they automatically include some of its "parent's"
members, plus its own. For example, we are going to suppose that we want to
declare a series of classes that describe polygons like our CRectangle, or like CTriangle.
They have certain common properties, such as both can be described by means of
only two sides: height and base.
This could be represented in the world of
classes with a class CPolygon from which we
would derive the two other ones: CRectangle
and CTriangle.

The class CPolygon
would contain members that are common for both types of polygon. In our case: width and height.
And CRectangle and CTriangle would be its derived classes, with
specific features that are different from one type of polygon to the other.
Classes that are derived from others inherit
all the accessible members of the base class. That means that if a base class
includes a member A and we derive it to
another class with another member called B,
the derived class will contain both members A
and B.
In order to derive a class from another,
we use a colon (:) in the declaration of
the derived class using the following format:
class
derived_class_name: public base_class_name
{
/*...*/ };
Where derived_class_name
is the name of the derived class and base_class_name
is the name of the class on which it is based. The public
access specifier may be replaced by any one of the other access specifiers protected and private.
This access specifier limits the most accessible level for the members
inherited from the base class: The members with a more accessible level are
inherited with this level instead, while the members with an equal or more
restrictive access level keep their restrictive level in the derived class.
|
1 |
// derived classes#include <iostream>using class
class
class
int
|
2010 |
The objects of the classes CRectangle and CTriangle
each contain members inherited from CPolygon.
These are: width, height and set_values().
The protected
access specifier is similar to private.
Its only difference occurs in fact with inheritance. When a class inherits from
another one, the members of the derived class can access the protected members
inherited from the base class, but not its private members.
Since we wanted width and height
to be accessible from members of the derived classes CRectangle and CTriangle
and not only by members of CPolygon,
we have used protected access instead of
private.
We can summarize the different access
types according to who can access them in the following way:
|
Access |
public |
protected |
private |
|
members of the same class |
yes |
yes |
yes |
|
members of derived classes |
yes |
yes |
no |
|
not members |
yes |
no |
no |
Where "not members" represent
any access from outside the class, such as from main(),
from another class or from a function.
In our example, the members inherited by CRectangle and CTriangle
have the same access permissions as they had in their base class CPolygon:
|
1 |
|
This is because we have used the public keyword to define the inheritance
relationship on each of the derived classes:
|
|
class |
This public
keyword after the colon (:) denotes the most
accessible level the members inherited from the class that follows it (in this
case CPolygon) will have. Since public is the most accessible level, by
specifying this keyword the derived class will inherit all the members with the
same levels they had in the base class.
If we specify a more restrictive access
level like protected, all public
members of the base class are inherited as protected in the derived class.
Whereas if we specify the most restricting of all access levels: private, all the base class members are
inherited as private.
For example, if daughter was a class derived from mother that we defined as:
|
|
class |
This would set protected
as the maximum access level for the members of daughter
that it inherited from mother. That is, all
members that were public in mother
would become protected in daughter.
Of course, this would not restrict daughter
to declare its own public members. That maximum access level is only set for
the members inherited from mother.
If we do not explicitly specify any access
level for the inheritance, the compiler assumes private for classes declared
with class keyword and public
for those declared with struct.
In principle, a derived class inherits
every member of a base class except:
·
its constructor and its destructor
·
its operator=() members
·
its friends
Although the constructors and destructors
of the base class are not inherited themselves, its default constructor (i.e.,
its constructor with no parameters) and its destructor are always called when a
new object of a derived class is created or destroyed.
If the base class has no default
constructor or you want that an overloaded constructor is called when a new
derived object is created, you can specify it in each constructor definition of
the derived class:
derived_constructor_name
(parameters) : base_constructor_name (parameters) {...}
For example:
|
1 |
// constructors and derived classes#include <iostream>using class
class
class
int
|
mother: no parametersdaughter: int parameter mother: int parameterson: int parameter |
Notice
the difference between which mother's
constructor is called when a new daughter
object is created and which when it is a son
object. The difference is because the constructor declaration of daughter and son:
|
1 |
|
In C++ it
is perfectly possible that a class inherits members from more than one class.
This is done by simply separating the different base classes with commas in the
derived class declaration. For example, if we had a specific class to print on
screen (COutput) and we wanted our
classes CRectangle and CTriangle to also inherit its members in
addition to those of CPolygon we could write:
|
1 |
class class |
here is
the complete example:
|
1 |
// multiple inheritance#include <iostream>using class
class
void class
class
int
|
2010 |