Chapter 2 Part 1
Chapter 2 Part 1
C++ Basics
Part 1
C++ Basics
2
Names: Identifiers
The name of a variable (or anything else in C++ that
needs naming) is called an identifier.
An identifier must be spelled starting with a letter or an
underscore symbol (_) followed by any number of
letters, digits, or underscore symbols.
Valid Examples x x_1 _abc A2b
hisIsAVeryLongIdentifier
Invalid Examples 12 3X %change myFirst.c data-1
There are identifiers that are reserved to the use of the
C++ language, called keywords, or reserved words.
Identifier Spelling Rule:
Identifiers must start with a letter or underscore and
have only letters, digits or underscores after the first.
3
Data Types
Name Description Size* Range*
short signed: -32768 to 32767
Short Integer. 2bytes
int(short) unsigned: 0 to 65535
signed: -2147483648 to 2147483647
int Integer. 4bytes
unsigned: 0 to 4294967295
1 Next 2 Next 3
Integer Data Type
// initialization of variables
Ex1 #include <iostream>
int a,b; using namespace std;
Ex2
unsigned short int NumberOfSisters; int main ()
{
signed int MyAccountBalance; int a=5; // initial value = 5
Ex3 int b{2}; // initial value = 2
int a=10; int result; // initial value undetermined
int b{3};
a = a + 3;
int c(5);
result = a - b;
Ex4 cout << result;
short year;
short int year; return 0;
}
6
Real Data Type
Three types
float (4 bytes) // initialization of variables
double (8 bytes) #include <iostream>;
long double (according to compiler)
int main ()
Ex1
{
float a=1.5; double x;
double c= -0.0000013; float y =0.01;
x = y*y ;
cout << “y^2 = ” << x << endl;
return 0;
}
y^2 = .0001
Boolean Data Type
// initialization of variables
Takes two values #include <iostream>;
True 1 int main ()
False 0 {
bool a,b; // boolean variables
Ex1 int x=6, y=7, z=10;
a = x < y;
bool a=0, b=1; b = (z+x) < y ;
cout << “x<y = ” << a << endl;
cout << “(z+x) < y = ” << b<< endl;
return 0;
}
x<y= 1
(z+x) < y = 0
Strings Data Type
Variables that can store
non-numerical values
longer than one single character
Ex1
String St1 =“ab5% P&”;
string mystring = "This is a string";
string mystring ("This is a string");
#include <iostream>
using namespace std;
int main( ) {
String Name = “Maha ”;
cout << “” << “ Welcome” << Name ;
return 0; }
Welcome Maha
What is the output of the Program?
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
Constants
Use #define preprocessor directive.
Format // defined constants: calculate circumference
1. #define identifier value #include <iostream>;
Ex 1
#define PI 3.14159
#define PI 3.14159
#define NEWLINE ' \n '
#define NEWLINE '\n'
2. Use const prefix int main ()
Ex2 {
const int pathwidth = 100; double r=5.0; // radius
const char tabulator = '\t'; double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0;
}
31.14159
Data Casting: Implicit & Explicit Casts
Local variables
is limited to the block enclosed in braces ({}) where they are declared.