1 / 10

C++ Basics

C++ Basics. Compilation. What does compilation do? g++ hello.cpp g++ -o hello.cpp hello. Comments. Why we need comments Two styles 1. /*…………………..*/ 2. //. C++ Variable.

deidra
Télécharger la présentation

C++ Basics

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++ Basics

  2. Compilation • What does compilation do? • g++ hello.cpp • g++ -o hello.cpp hello

  3. Comments • Why we need comments • Two styles 1. /*…………………..*/ 2. //

  4. C++ Variable • Variables are the objects in C++ .where data is stored, manipulated through expressions, and printed. Variables have two defining characteriestics: a name (or identifier) and a type.

  5. C++ Variable Name Rules • Variable names may only consist of alpha-numeric characters and the '_' character. • Variables names must begin with an alphabetic character or the '_' character. • Capitalization is meaningful. Therefore, myVar and MyVar are two distinct variables. • Variables may not have the same name as a C++ keyword. some keywords: main, return, class, int, float, …

  6. Variable Types • There are an infinite number of allowable variable types, because C++ allows programmers to define their own types. However, there are only a few built-in types (i.e., types that are always available in C++ without any inclusions). The built-in types are: int    - variables of this type store positive, zero, and negative integer values. Float, double - variables of this type store values containing fractional or real values. char     - variables of this type store single characters (e.g., 'a', 'F', '$', or '!'). bool     - variables of this types store one of two values: true or false. • There is one other type, string,which we will use frequently. The string type is not built-in. You must include the string system definitions to get this type (i.e., have the code #include <string> appearing at the top of your program. The final type we will frequently use is: string  - variables of this type store zero or more characters (e.g., "a string" or "hello").

  7. Variable Declaration • Before you can store information in a variable, it needs to be decalred. When you declare a variable, you are identifying the variables two defining characteristics, i.e., its name and its type. See below for some sample variable declarations. • Example: int i;                    // declares the integer variable called "i" int myVar = 2;   // declares the variable "myVar" and stores the number 2 into it double x = 1.1;  // declares the double variable "x" and stores a 1.1 into it char c = 'Y';   // declares the variable "c" and stores a capital 'Y' into it char midInitial; // declares a character variable without giving it a value bool go = true;  // declares a boolean (i.e., true/false) variable and sets it to true string n = "Ed";// declares a string variable with the value "Ed" stored in it

  8. Assignment • we can put a value in the variable using the "=" operator. The value is assigned from the right to the left. • Example: x = 10;           // store the value 10 in x x = 10 + 4; // store the sum of 10 and 4 (that is 14) in x x = x - 1;       // this says take whatever is stored in x, subtract one from it, and store the result back in x x = 2 * y - z; // assumes you have the variables "y" and "z" already declared elsewhere in your program

  9. C++ I/O • Output Statements • To print out text or variables to the screen, we use a cout statement, for example • cout << "hello\n";    // this line prints the word hello to the screen • cout << x << endl;  // this line prints the value of the variable x to the screen • Input Statements • To enter a variable value into the program, we use a cin statement, for example cin >> x;          // this line puts the value the user types in the variable x • cin >> x >> y;  // input values in two variables x and y

  10. Expression and Operators • Syntax:  variable = expr; • expr is an expression made up of variables and operators (e.g., +, -, *, /)

More Related