[C++] Basics

What do people say about C++? “Powerful” and “Fast” BUT “Difficult”, “Dangerous”, “Confusing”, “Cryptic” …

Yes, C++ is more confusing than C# or Java. C# and Java knew the problems of C++ when they were designed.

If you start a new project, C# or Java can be a better choice. But there are a lot of C++ code out there and it is not practical to migrate them to other languages.

As a developer, there are many chances for you to encounter old legacy C++ code. Let’s find out why C++ is scary to programmers.  

 

1. Main()

Th starting point of the program is the “main()” function.

int main() {...}
int main(int argc, char *argv[]) {...}

 

2 . Data Types

Data types in C++ are quite complex and there are a lot of combinations.

(1) The basic types are:

  • char: 1 byte
  • wchar_t: 2 bytes (Unicode character)
  • short int:  2 bytes
  • int: 4 bytes (signed)
  • long int: 4 bytes
  • long: 4 bytes
  • long long: 8 bytes
  • float: 4 bytes
  • double: 8 bytes
  • bool: 1 byte
  • void: N/A

(2) Data type modifiers are:

  • for integer based types : signed, unsigned, short, long
  • for character types : signed, unsigned
  • for double : long

As you can see the number of possible combinations is quite a lot. Actually, the size of  a data type (in bytes) is different on different machines and OSs. The sizes mentioned above are targeted on Windows systems.

You might be surprised that the “long” is 4 bytes, not 8 bytes. The “long” type is an alias of the “long int”. MS Visual C++ provides some other integer types when you’re confused with the sizes.

  • __int8: 1 byte
  • __int16: 2 bytes
  • __int32: 4 bytes
  • __int64: 8 bytes

The “sizeof()” operator show the size of a type.

using namespace std;
void get_size()
{
  cout << "char: " << sizeof(char) << " bytes" << endl; // 1
  cout << "wchar_t: " << sizeof(wchar_t) << " bytes" << endl; // 2
  cout << "short: " << sizeof(short) << " bytes" << endl; // 2
  cout << "int: " << sizeof(int) << " bytes" << endl; // 4
  cout << "long: " << sizeof(long) << " bytes" << endl; // 4
  cout << "long long: " << sizeof(long long) << " bytes" << endl; // 8
  cout << "float: " << sizeof(float) << " bytes" << endl; // 4
  cout << "double: " << sizeof(double) << " bytes" << endl; // 8
  cout << "long double: " << sizeof(long double) << " bytes" << endl; // 8
  cout << "bool: " << sizeof(bool) << " bytes" << endl; // 1

  cout << "__int8: " << sizeof(__int8) << " bytes" << endl; // 1
  cout << "__int16: " << sizeof(__int16) << " bytes" << endl; // 2
  cout << "__int32: " << sizeof(__int32) << " bytes" << endl; // 4
  cout << "__int64: " << sizeof(__int64) << " bytes" << endl; // 8
}

 

3. Output to Console

As a programmer, it is mandatory to create a “Hello World” application whenever you learn a new language.

Rather than using “printf()”, C++ provides improved stream objects.

#include <iostream>

using namespace std;
int main()
{
  std::cout << "Hello, Worldn";
  cout << "Hello, World" << endl;
}

You can specify the namespace or use the “using” statement.

 

4. Scope

  • Global Scope: When a variable is declared outside of any functions, it is called an external variable and it has a global scope
  • Local Scope: Variables defined in a block are in the scope of that block

When you use a local variable whose name is the same as that of a global variable, there should be a construct to distinguish which one you are using. By default, local variable is referred. Global scope resolution operator “:: is used to refer to the global variable rather than the local variable.

int amount = 100; // global
void test_scope()
{
  cout << "Amount (Global) = " << amount << endl; // 100
  int amount = 50; // local
  cout << "Amount (Local) = " << amount << endl; // 50
  cout << "Amount (Global) = " << ::amount << endl; // 100
}

 

5. C++ Storage Classes

Variables can be declared with storage class specifiers that tell the compiler how variables are to be treated.

  • auto: Each invocation of the statement block in which the variable is defined gets a new copy with its own memory space and with initialization each time. Local variables implicitly are declared auto.
  • static: The variable retains its value between executions of the statement block.
  • extern: Declares external variables that are not defined locally, but the local code needs to reference. An application can have several extern declarations but only one definition (a declaration without the extern specifier). The declaration must appear outside any functions.
  • register: Works like an “auto” specifier. Suggest that the variable reside in a hardware register, but it can be ignored by the compiler. You cannot take the address of register variables.
int Add()
{
  static int amount = 100;    // static, initialized only once
  amount += 50;
  return amount;
}
int Add1()
{
  int amount = 100;    // auto, initialized every time
  amount += 50;
  return amount;
}
int main()
{
  for (int i=0; i<3; i++)
    cout << "Amount (Static) = " << Add() << endl; // output : 150, 200, 250

  for (int i=0; i<3; i++)
    cout << "Amount (Auto) = " << Add1() << endl; // output : 150, 150, 150

  return 0;
}

 

6. Examples – External variables

// extern1.h file
#ifndef _EXTERN1_H
#define _EXTERN1_H
    void    setTotal(int total);
#endif /* _EXTERN1_H */
// extern1.cpp file
int total;  // global external variable declaration
void setTotal(int total)
{
  ::total = total*2;
}
#include <iostream>
#include "extern1.h"

using namespace std;

int main()
{
  extern int total;   // external variable, defined elsewhere

  setTotal(100);
  cout << "Total = " << total << endl; // 100*2 = 200

  return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s