Professional Documents
Culture Documents
• …
Common C++ Keywords
do inline
asm Return typedef
double int
auto short union
else long
break signed unsigned
enum mutable
case sizeof using
explicit namespace
catch static virtual
extern new
char struct void
float operator
class switch volatile
for private
const Template while
friend protected
continue this
goto public
default throw
if register
delete Try
Creating Comments
#include <iostream.h>
int main () {
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
More examples
int a;
cin >> a;
if (cin.fail())
cout << "Invalid input.";
else {
// your logic goes here
}
Namespaces
Namespaces allow to group classes, objects and functions under a
name. This way the global scope can be divided in "sub-scopes",
each one with its own name.
The syntax of namespaces is:
namespace identifier
{
entity
}
namespace first
{
int a, b;
}
first::a
first::b
Purpose of namespace
namespace second {
double x = 13.14;
double y = 12.71;
}
int main () {
using first::x;
using second::y;
cout << x << endl;
cout << y << endl;
cout << first::y << endl;
cout << second::x << endl;
return 0;
}
The keyword using is used to introduce a name from a namespace
into the current declarative region.
#include <iostream>
using namespace std;
namespace first {
int x = 50;
int y = 100;
}
namespace second {
double x = 13.14;
double y = 12.71;
}
int main () {
using namespace first;
cout << x << endl;
cout << y << endl;
cout << second::x << endl;
cout << second::y << endl;
return 0;
}