0% found this document useful (0 votes)
26 views15 pages

Chapter 2 Part 1

The document discusses different C++ data types including integer, character, floating point, Boolean, and string. It also covers variable naming rules, constants, implicit and explicit type casting, and variable scope.

Uploaded by

marivull2811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

Chapter 2 Part 1

The document discusses different C++ data types including integer, character, floating point, Boolean, and string. It also covers variable naming rules, constants, implicit and explicit type casting, and variable scope.

Uploaded by

marivull2811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 2

C++ Basics

Part 1
C++ Basics

 Data Types and Expressions


 Variables and Assignments
 Input and Output

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

long int (long) Long integer. 8bytes


Boolean value; takes one of
bool 1byte 1 or 0
two values: true/false.
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
Double precision floating
double 8bytes +/- 1.7e +/- 308 (~15 digits)
point number.
Long double precision According to
long double
floating point number. compiler
signed: -128 to 127
char Character or small integer. 1byte
unsigned: 0 to 255
String A number of characters (n) N bytes
Void No storage
Character Data Type
 Contains one character only
 Defined within a single quote
 Ex1
 Char one =‘1’;
 Ex2 #include <iostream>;
int main( ) {
 Char two;
char one =‘1’;
 Two =‘2’; char two, three;
two = ‘2’;
three = ‘3’;
cout << one << “ Next ” << two << “ Next ” <<
three;
return 0; }

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

 Declare variable in one type and use it in another


 Ex:
Implicit
1. int x=1; cast
Try this yourself
3. float y = x; int x=1;
Error float y = float(x);
1. x = float(x);
cout <<y;
 Ex:
1. float y = 2.5; Try it yourself
2. int x = 1;
3. x = x + y; Explicit
4. x = x+ int(y) cast
Data Casting with static_cast Operator
 Implicit type coercion: when value of one type is
automatically changed to another type
 Cast operator: provides explicit type conversion
static_cast<dataTypeName>(expression)
 Resolve at compile time

 Does not affect any const or volatile modifiers

 Eg. float to int, char to int

 If static keyword is used, variable/functions cannot


be modified again
Casting with static_cast Operator
Scope of variables
 Global variables
 referred from anywhere in the code, even inside functions, whenever it is after its
declaration.

 Local variables
 is limited to the block enclosed in braces ({}) where they are declared.

You might also like