Professional Documents
Culture Documents
c++ Session 2
c++ Session 2
c++ Session 2
Data Type
Data Types
• Variables are used to store various information
• Variables are nothing but reserved memory locations to
store values.
• This means that when you create a variable you reserve
some space in memory.
Primitive Built in data type
• These data types are called primitive because they have been
provided by the c++ programming language as a basic building block.
• There several of them but, we will be dealing with the following
Type Keyword Size
Boolean bool 1 (byte) (though it’s true or
false
Character char 1 (byte)
String string 4 byte
Integer int 4 byte
Floating Point float 4 (byte)
Double Floating Point double 8 (byte)
Valueless void 1 (byte)
Wide Character Wchar_t 2 (byte)
ASCII values of commonly used characters
Character Value Comment
0 through 9 48 - 57 0 is 48, 9 is 57
A through Z 65 - 90 A is 65, Z is 90
a through z 97 - 122 a is 97, z is 122
Primitive Data Type Cont.
• Please note that:
• The sizes of variables might be different from those shown in the above
table, depending on the compiler and the computer you are using.
We can use sizeof(type) to find the size displayed on our computer. E.g.
cout<<“The size of float is ”<<sizeof(float);
• Several of the basic types can be modified using one or more of the modifier
types.
• signed
• unsigned
• short
• long
The string data type
• String class
• Strings are objects that represent sequences of characters. The
standard C++ library provides a string class type that supports all the
operations mentioned above, and much more functionality. We will
study this class in C++ Standard Library later
C++ key words (Reserved)
you are not to memorize them
Group 1 from C to C++
Auto, const, double, float, int, short, struct, unsigned, break,
continue, else, for, long, signed, switch, void, case, default,
enum, goto, register, sizeof, typedef, volatile, char, do, extern,
if, return, static, union, while.
Group 2 C++
Asm, dynamic_cast, namespace, reinterpret_cast, try, bool,
explicit, new, static_cast, typeid, catch, false, operator,
template, typename, class, friend, private, this, using,
const_cast, inline, public, throw, virtual, delete, mutable,
protected, true, wchar_t.
C++ key words (Reserved)
• Reserved for ASCII C++
And, bitand, compl, not_eq, or_eq, xor_eq, and_eq, bitor, not,
or, xor,
int main ()
int main () {
{ int a = 3;
int a = 3; cout<<“--a is “<< --a<<endl;// 2 prefix
cout<<"a-- is "<<a--;//3 postfix
return 0; return 0;
}
}
// initialization of variables
#include <iostream>
using namespace std;
int main ()
{
int a = 3;
cout<<"a++ is "<<a++<<endl;// will display 3 postfix
cout<<"++a is "<<++a<<endl;// will display 5 prefix
cout<<"a-- is "<<a--<<endl; // will display 5 postfix
cout<<"--a is "<<--a<<endl; // will display 3prefix
//Why?
return 0;
}
// initialization of variables Casting converting one type to another
#include <iostream>
// initialization of variables
using namespace std; #include <iostream>
using namespace std;
int main ()
{ int main ()
int a = 3, b = 7, c; {
double d; int a = 3, b = 7, c;
c = b/a; //2 why? double d;
d = b/a; //2 why? c = b/a; //2 why?
d =(double) b/a;
cout<<"c is "<<c<<endl
cout<<"c is "<<c<<endl
<<"d is "<<d;
<<"d is "<<d;
return 0; return 0;
} }
• In c++ const is used to create run time constant.
Constant const • A constant cannot be altered declared so initialization
must take place on definition
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main ()
{
int a = 3, b = 4, c = 2, d= 4;
}
Assignment 2
• A,b,c are variables of type int while d, e f and g are of type float.
• If a= 4, b = 6 and c = -3, Find
• d=3a + -ac
• f = d/a
• e = f+2c
• g = f/a
Submission date is:
midnight 12/02/2018
Good Luck
End