Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Expressions and Declarations in C++ Programming

Part of the C++ for Dummies Cheat Sheet

To perform a calculation in the C=++ program you need and 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

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

Declarations

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
float
[long] double
bool

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

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 define and instantiate their own enumerated types:

enum [class] Name = {label1[=value], label2[=value]...};
Name variable = Name::label1;  // used in an assignment

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

<struct | class> ClassName [ : [virtual] [public] BaseClass]
{
     <public|protected>:
       // constructor
       ClassName([arg list]) <[: member(val),...] {...} |;>
       ClassName() [= <delete|default>;]
       // destructor
       [virtual] ~ClassName() <{...} | [=<delete|default>;>
       // public data members
       type dataMemberName;
       // 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;
};

In addition, a constructor with a single argument may be flagged as explicit.

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 { {...} };
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!