Expressions and Declarations in C++ Programming - dummies

Expressions and Declarations in C++ Programming

By Stephen R. Davis

Part of C++ for Dummies Cheat Sheet

To perform a calculation in the C++ program you need an expression. An expression is a statement that has both a value and a type. In the C++ program, a declaration is statement that defines a variable or it’s a “holding tank” for some sort of value like a number or character.


Expressions take one of the following forms:

objName                         // for a simple object
operator expression             // for unary operators
expr1 operator expr2            // for binary operators
expr1 ? expr2 : expr3           // for the ternary operator
funcName([argument list]);      // for function calls

Literal expressions

A literal is a form of constant expression. The various types of literals are defined in the following table.

Example Type
1 int
1L long int
1LL long long int
1.0 double
1.0F float
‘1’ char
“a string” char* (automatically terminated with a null character)
L”a string” wchar_t*
u8″this is a UTF-8 string with a UTF-8 character: u2018″ char8_t*
u”this is a UTF-16 string with a UTF-16 character: u2018″ char16_t*
U”this is a UTF-32 string with a UTF-32 character:
true, false bool
0b101 binary (C++ 2014 standard)


Declarations use both intrinsic and user-defined types. The intrinsic types are

[<signed | unsigned >]char
[<signed | unsigned >]wchar_t
[<signed | unsigned>] [<short | long | long long>] int
[long] double

Declarations have one of the following forms:

[<extern|static>][const] type var[=expression]; // variable
[<extern|static>][const] type array[size][={list}]; // array
[const] type object[(argument list)];           // object
[const] type object [= {argument list}];        // alternative
[const] type * [const] ptr[=pointer expression];// pointer
type& refName = object;                         // reference
type fnName([argument list]);                   // function

The keyword auto can be used if C++ can determine the type of variable itself:

auto var = 1L;               // the type of var is long int

The keyword decltype extracts the type of an expression. This type can then be used wherever a type name is used. For example, the following example uses decltype to declare a second variable with same type as an existing variable:

decltype(var1) var2;         // the type of var2 is the same as var1

A function definition has the following format:

// simple function
[<inline|constexpr>] type fnName(argument list) {...}
// member function defined outside of class
[inline] type Class::func(argument list) [const] {...}
// constructor/destructors may also be defined outside of class
Class::Class([argument list]) {...}
Class::~Class() {...}
// constructors/destructor may be deleted or defaulted
// in lieu of definition
Class::Class([argument list]) = <delete|default>;
Class::~Class() = <delete|default>;

An overloaded operator looks like a function definition. Most overloaded operators may be written either as member or simple functions. When written as a member function, *this is the assumed first argument to the operator:

MyClass& operator+(const MyClass& m1, const MyClass& m2);// simple
MyClass& MyClass::operator+(const MyClass& m2); // member;

Users may also define their own types using the class or struct keywords:

<struct | class> ClassName [ : [virtual] [public] BaseClass]
       // constructor
       ClassName([arg list]) <[: member(val),...] {...} |;>
       ClassName() [= <delete|default>;]
       // destructor
       [virtual] ~ClassName() <{...} | [=<delete|default>;>
       // public data members
       type dataMemberName [= initialValue];
       // public member functions
       type memberFunctionName([arg list]) [{...}]
       // const member function
       type memberFunctionName([arg list]) const [{...}]
       // virtual member functions
       virtual type memberFunctionName([arg list]) [{...}];
       // pure virtual member functions
       virtual type memberFunctionName([arg list]) = 0;
       // function that must override a base class function
       type memberFunctionName([arg list]) override;
       // a function that cannot be overriden in a subclass
       type memberFunctionName([arg list]) final;

In addition, a constructor with a single argument may be flagged as explicit meaning that it will not be used in an implicit conversion from one type to another. Flagging a constructor as default means “use the default C++ constructor definition”. Flagging a constructor as delete removes the default C++ constructor definition.

C++ supports two types of enumerated types. The following old enumeration type does not create a new type:

enum STATE {DC,                 // gets 0
            ALABAMA,            // gets 1
            ALASKA,             // gets 2
            ARKANSAS,           // gets 3
            // ...and so on
int n = ALASKA;                 // ALASKA is of type int

By default an individual entry is of type int but this can be changed in the C++ 2011 standard:

enum ALPHABET:char {A = 'a',    // gets 'a'
                    B,          // gets 'b'
                    C,          // gets 'c'
                                // ...and so on
char c = A;                     // A is of type char

C++ 2011 allows a second format that does create a new type:

// the following enumeration defines a new type STATE
enum class STATE { DC,          // gets 0
                   ALABAMA,     // gets 1
                   ALASKA,      // gets 2
                   ARKANSAS,    // gets 3
                   // ...and so on
STATE s = STATE::ALASKA;  // now STATE is a new type
// the following uses a different underlying type
enum class ALPHABET:char {A = 'a',// gets 'a'
                          B,      // gets 'b'
                          C,      // gets 'c'
                                  // ...and so on
ALPHABET c = ALPHABET::A;         // A is of type ALPHABET

Template declarations have a slightly different format:

// type T is provided by the programmer at use
template <class T, {...}> type FunctionName([arg list])
template <class T, {...}> class ClassName { {...} };