1 / 15

C++ Tutorial Part 3 Summary

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.

doane
Download Presentation

C++ Tutorial Part 3 Summary

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Tutorial Part 3 Summary Variables and Data Types

  2. 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…

  3. 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…

  4. 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

  5. Important note: • C++ is case sensative • Name identifier like abc is not same as Abc or ABC. They are identified completely differently

  6. 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

  7. 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;

  8. 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.

  9. 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; }

  10. 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;)

  11. Initialization • Can be initialized in two ways: • int a = 23; • Or • int a(23);

  12. 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; }

  13. 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

  14. 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)

  15. Please rate, comment and Subscribe • Next Video: Constants • Then: Operators • Following with: Basic input and output to end the basics!!!

More Related