150 likes | 261 Views
C++ Tutorial Part 3 Summary. Variables and Data Types. 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…. Identifiers.
E N D
C++ Tutorial Part 3 Summary Variables and Data Types
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…
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…
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
Important note: • C++ is case sensative • Name identifier like abc is not same as Abc or ABC. They are identified completely differently
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
Variable declaration • int a; identifier is a, data type is integer • float b; identifier is b, data type is b; • doublenum; identifier is num, data type is double • int a, b, c; 3 integer variables named a, b, c • Same as int a; • int b; • intc;
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.
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; }
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;)
Initialization • Can be initialized in two ways: • int a = 23; • Or • int a(23);
Previous program can now be: #include<iostream> using namespace std; int main() { int a = 23; intb(56); int sum; sum=a+b; cout << sum; return 0; }
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
String example program Important!!! #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; } String Concatination (Adding two strings)
Please rate, comment and Subscribe • Next Video: Constants • Then: Operators • Following with: Basic input and output to end the basics!!!