10
C++ Basics

C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

Embed Size (px)

Citation preview

Page 1: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

C++ Basics

Page 2: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

CompilationWhat does compilation do?g++ hello.cppg++ -o hello.cpp hello

Page 3: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

CommentsWhy we need commentsTwo styles

1. /*…………………..*/2. //

Page 4: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

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.

Page 5: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

C++ Variable Name RulesVariable 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,

Page 6: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

Variable TypesThere 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").

Page 7: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

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

Page 8: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

Assignmentwe 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

Page 9: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

C++ I/OOutput 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

Page 10: C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello

Expression and OperatorsSyntax:  variable = expr; expr is an expression made up of variables and

operators (e.g., +, -, *, /)