You are on page 1of 12

Intro to C++

• C++ was devised by Bjarne Stroustrup in 1983.

• An extension of C by adding some enhancements to C language.

• C++ is object oriented while C is function or procedure oriented .

• C++ breaks down the problem into objects, C breaks down it to


functions.

• C++ puts major focus on Data, C puts on functions.

• …
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

• A line comment begins with two slashes (//) and


continues to the end of the line on which it is placed

• A block comment begins with a single slash and an


asterisk (/*) and ends with an asterisk and a slash
(*/); it might be contained on a single line or
continued across many lines
C++ Binary Arithmetic Operators

#include<iostream.h> //for standard IO services


void main(){
cout<<“Hello World!”; // display string
cout<<20+30; // display addition of two int
int a = 20+30;
cout<<a;

}
The switch Statement
Different outcomes depending on specific value of
a variable.
#include<iostream.h>
void main(){

switch(choice){
case 1:
cout<<“Bubble sort”;
break;
case 2:
cout<<“Quick sort”;
break;
default:
cout<<“No more Work”;
}

}
More examples

#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
}

where identifier is any valid identifier and entity is the set of


classes, objects and functions that are included within the
namespace.
Example

namespace first
{
int a, b;
}

In order to access these variables from outside the


first namespace we have to use the scope operator ::

first::a
first::b
Purpose of namespace

The functionality of #include <iostream>


namespaces is useful in the using namespace std;
case that there is a
possibility that a global namespace first
object or function uses the {
same name as another one, int age = 5;
causing redefinition errors. }

In the example, two global namespace second {


variables have the same int age = 10;
name age. One is defined }
within the namespace first
and the other one in second. int main () {
cout << first::age << endl;
cout << second::age << endl;
return 0;
}
The keyword using
#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 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;
}

The keyword using can also be used as a directive to introduce


an entire namespace.

You might also like