[C++] Basics – Improvements on C

C++ is a complex language. It is object-oriented and provides a lot of improved features on C. By learning the differences between C and C++, you will not be stuck in old C-style coding.

 

1. C++ Improvements on C Features

  • Inline functions: can be used instead of C’s #define macros that have arguments
  • const variable declarations: can be used instead of C’s #define statements that equate values to symbols
  • “new” and “delete” memory allocation operators: replace Standard C’s “malloc” and “free” functions
  • string class: replaces the character array
  • iostream class: replaces the “stdio” function library for input and output
  • try/catch/throw exception handling mechanism: replaces C’s “setjmp“/”longjmp” functions
  • namespace: creates a declarative region in which program elements can be placed

 

2. Inline Functions

Inline functions are used in C++ to reduce the overhead of a normal function call. A function is declared inline by using the specifier “inline” for C++ functions. The “inline” specifier is a suggestion to the compiler that an inline expansion can be performed. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call.
inline int add(int a, int b) {return a+b;}

int main()
{
  int m=3;
  int n=2;
  int result=0;
  result = add(m,n);
  std::cout << m << " + " << n << " = "  << result << endl; // 3+2=5

  return (EXIT_SUCCESS);
}

 

3. #include Directive

The way you are using the “#include” directive in C++ is a little bit different from that in old C. When you include a library header, you do not append “.h” anymore and the name of the header is enclosed in “< >”. The new style C++ headers are not the file names but abstract names that guarantee that the appropriate information is included by a compiler.
#include <stdio.h> // old style
#include <iostream> // new style

In general, the relationship between old C header files and new C++ header names is like this:

  • “omit the .h” and “prepend c”.

For example, “stdlib.h” becomes “cstdlib”

 

4. Using namespace

The “namespace” is a familiar concept for C# developers. In C++, it is a recent addition. The purpose of the name space is to localize the names in order to avoid name collisions.
The “namespace” keyword is used to create a block with a name. Since the namespace defines a scope, you need to use the “scope resolution operator(::)” to refer anything declared within a namespace from outside. If you need to refer members of a namespace frequently, specifying namespace for each reference becomes a tedious chore. In this case, you can use the “using” statement. The “using”  statement has two forms. When you use a “using” statement, you need to be careful for the name collisions.
using namespace ns_name; // all members are accessible
using ns_name::member; // only one specific member is made visible.
namespace Point_NS
{
  int x;
  int y;
}

using namespace std;
using namespace Point_NS;

int _tmain(int argc, _TCHAR* argv[])
{
  x = 10;
  y = 20;
  cout << x << ", " << Point_NS::y << endl; // 10, 20

  return (EXIT_SUCCESS);
}
namespace Point_NS
{
  int x;
  int y;
}

using namespace std;
using Point_NS::y;

int _tmain(int argc, _TCHAR* argv[])
{
  int x = 1; // local
  Point_NS::x = 10; // with a namespace
  y = 20; // y in "Point_NS"
  cout << x << ", " << Point_NS::x << ", " << y << endl; // 1, 10, 20

  return (EXIT_SUCCESS);
}

 

5. Console Input / Output

In C, “scanf()” and “printf()” in the “stdio.h” are used for basic console IO tasks.
In C++, “cin” and “cout” in the “iostream.h” are used. They are belong to the namespace “std“.
  • Sample code 1 : Output to the screen
printf("C: Hello World. n");
std::cout << "C++: Hello World." << endl;
  • Sample code 2 : Display diverse data to the screen
int i=3;
double d = 5.75;
char c = 'S';
char s[10] = "Hello !!!";

std::cout << "Int i = " << i << endl;
std::cout << "Double d = " << d << endl;
std::cout << "Character c = " << c << endl;
std::cout << "String s = " << s << endl;
  • Sample code 3 : Input data from the keyboard
int i;
double d;
char s[20];

std::cout << "Input Title : ";
std::cin >> s;

std::cout << "Input Number 1 (Integer) :  ";
std::cin >> i;

std::cout << "Input Number 2 (Double) :  ";
std::cin >> d;

std::cout << endl << endl;
std::cout << "Title = " << s << endl;
std::cout << "Result : " << i << " + " << d << " = " << (i+d) << endl;

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