c++ Session 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

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,

Predefined Identifiers C++


Cin, endl, INT_MIN, iomanip, main, npos, std, cout, include,
INT_MAX, iostream, MAX_RAND, NULL, string,
Variable declaration
• This declares three variables (a, b and c), all of type int, and has
exactly the same meaning that is those in a and b below.
• 1.
This is the same for all the other data types
• i. int a;
1. string str1;
• ii. int b; 2. string str2;
• iii. int c; 3. string str3;
• Equivalent to
• 2. is the same as
string str1, str2, str3;
• i. int a, b, c;
Initialization of variables (Case
SENSITIVE)
• When a variable is declared it could be initialize there and then or
initialized in the later. Initialization is assigning values to the declared
data type. There are three types of initialization
• 1) a is NOT EQUAL to A 2
• Int a = 5; Int a(5);
• Int a; Int a (5);
Int a{5}; // Please note that this
• a = 5;
does not work for string data type
Are all equal to int a = 5;
• A variable must be declared before it can be used
int main()
{
a = 5;
cout<<“a is ”<<a; // it will not run because a is not declared or known
}
Variables are:
1. Case sensitive
2. Alphanumeric
3. Begin with letter or _
Operators (Unary, binary)
• C++ supports the usual binary operators (+, -, *, /)
• Binary because there are two operands
• Float a= 2.0;
• Float b = 5.0;
• Float c = 6.0;
• Float arg = b*b – 4.0*a*c;
• Question what is the value of arg. BODMAS rules
• Use parenthesis when in doubt.
Incremental and decrement operators
Operator Meaning Comment
A++ A=A+1 Postfix
++A A=A+1 Prefix
A- - A=A-1 Postfix
--A A=A-1 Prefix
Code Analysis
// initialization of variables // initialization of variables

#include <iostream> #include <iostream>


using namespace std;
using namespace std;
int main ()
int main () {
{ int a = 3;
int a = 3; cout<<"++a is "<<++a<<endl;// 4 prefix
cout<<"a++ is "<<a++;//3 postfix
return 0;
return 0; }
}
Code Analysis
// initialization of variables // initialization of variables

#include <iostream> #include <iostream>


using namespace std; using namespace std;

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 main ()


{ {
const int a = 3, c = 2; const int a = 3, c = 2;
cout<< ++a<< " "<<++c; const double d;//initialization
//will not run why? d = a + c;
//has changed cout<<d;
return 0; return 0;
• Boolean expressions evaluate to true or false
Boolean Expression • True can be interpreted as 1 and false as 0 e.g
#include <iostream>
using namespace std;

int main ()
{
int a = 3, b = 4, c = 2, d= 4;

cout<< "a == c "<< (a == c)<<endl<<endl;// o Why


cout<< "b == d "<< (b == d);//1 why

}
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

You might also like