Variables and Data Types. Variable: Portion of memory for storing a determined value. Could be...

15
C++ TUTORIAL PART 3 SUMMARY Variables and Data Types

Transcript of Variables and Data Types. Variable: Portion of memory for storing a determined value. Could be...

Page 1: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

C++ TUTORIAL PART 3 SUMMARY

Variables and Data Types

Page 2: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Values and variables

Variable: Portion of memory for storing a determined value.

Could be numerical, could be character or sequence of characters called strings

Variable examples: 20, “California”, ‘a’ etc…

Page 3: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Identifiers

Valid Identifier: Used to identify a particular variable in the memory. Like a reference.

Sequence of one or more letters, digits or one underscore character.

Cannot start with number Best practice is to start with a letter. Examples: myValue, _num, num2 etc…

Page 4: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Identifier restrictions with reserved keywords

Cannot use words like: asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while 

Or, cannot use and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq 

Page 5: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Important note:

C++ is case sensative

Name identifier like abc is not same as Abc or ABC. They are identified completely differently

Page 6: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Fundamental Data types

Computer needs to know what kind of data we are storing in case we will have to process those data later on for other purposes.

Fundamental data types: char: Character or small integer, size=1 byte short int(short): short integer, size: 2 bytes int: Integer, size: 4bytes long int(long): long integer 4bytes bool: Boolean value(true or false, 1 or 0), size: 1byte float: decimal number, size: 4 bytes double: double precision floating point number, size: 8 bytes long double: size: 8bytes wchat_t: Wide character, size: 2 or 4 bytes

Page 7: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Variable declaration

int a; identifier is a, data type is integer float b; identifier is b, data type is b; double num; identifier is num, data

type is double int a, b, c; 3 integer variables named a,

b, c Same as int a; int b; int c;

Page 8: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Signed vs unsigned int

Signed: can have both positive and negative numbers

Unsigned: Only positive numbers Example: signed int a; unsigned int b; Default is signed in c++ so if nothing is

given it takes it as signed. Example int a; is signed integer a.

Page 9: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Example program

#include<iostream>using namespace std;int main(){

int a, b;int sum;a=23;b=56;sum=a+b;cout << sum;return 0;

}

Page 10: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Initialization of variables

Variable needs to be declare first to be initialized.

int a; is declaring variable, saying that make a new

variable named a which is an integer a = 23;

Initializing variable a and giving value 23 to a; In c++ both can happen in same line

Example int a = 23; First initialized int a( = 23;) Then initialized int (a = 23;)

Page 11: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Initialization

Can be initialized in two ways: int a = 23; Or int a(23);

Page 12: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Previous program can now be:

#include<iostream>using namespace std;int main(){

int a = 23;int b(56);int sum;sum=a+b;cout << sum;return 0;

}

Page 13: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Strings

Need to add #include<cstring> to start the program String: sequence of characters Declaring string same as other variables except

changing data type to string Example: string myname;

Value of string data type always needs to be inside “ ”. Example myname = “Dean”;

While declaring characters they should be given inside single quotes: Example: char myChar=‘a’; Character takes only one character while string is

collection of character in one variable identifier

Page 14: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

String example program

#include<iostream>#include<cstring>using namespace std;

int main(){

string MyName = “My name is Dean”;cout << MyName << endl;string AnotherString = “Jones”;string AddedString = MyName+AnotherString;cout << AddedString << endl;

}

Important!!!

String Concatination(Adding two strings)

Page 15: Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.

Please rate, comment and Subscribe

Next Video: Constants Then: Operators Following with: Basic input and output to

end the basics!!!