Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Sample Program #include using namespace std; int main(void) { cout << “Hello, world!”; return 0; }

Similar presentations


Presentation on theme: "A Sample Program #include using namespace std; int main(void) { cout << “Hello, world!”; return 0; }"— Presentation transcript:

1 A Sample Program #include using namespace std; int main(void) { cout << “Hello, world!”; return 0; }

2 Program Outline #include using namespace std; int main(void) { }

3 Another Simple Program # include using namespace std; int main(void) { int a, b; cout “; cin >> a >> b; cout << “Total= “ << a+b << endl; return 0; }

4 Elements of the Program #include This allows the program to use I/O functions.

5 Elements of the Program using namespace std; Identifiers can be in separate namespaces to avoid overlap. This allows you to avoid typing “std::” before many identifiers.

6 Elements of the Program int main(void) This is the standard function heading for the main function. Every program needs a main function. This function takes no arguments ( void ) and returns an int.

7 Elements of the Program { } The brackets surround the actual code of the main function.

8 Elements of the Program cout << “Hello, world!” << endl; This prints out the text Hello, world! in a console window and then starts a new line. c out is the standard output, in this case, the console window on the display.

9 Elements of the Program return 0; This returns the value 0 to whomever called the program.

10 Data Objects Data objects are boxes in memory that hold values. Each box can hold exactly one value. Values can be integers, floating point numbers (similar to real numbers), characters, strings, and so on. Each data object has a specific type of object it can hold. So, a data object for an integer is different than a data object for a floating point number.

11 Identifiers and Variables To use a data object, we give it a name. Many things in a C++ program have names – data objects, functions (e.g., “main”), namespaces (e.g., “std”). The name of a data object is called a variable.

12 Rules for Identifiers There are rules for valid identifiers. In C++, an identifier must start with either a letter (a-z, A-Z) or the underscore character (_) (usually reserved for system stuff). The rest of the character can be letters, digits, or underscore. case-sensitive. FOOBEAR is different from foobear and different from FooBear Identifiers are case-sensitive. FOOBEAR is different from foobear and different from FooBear

13 Examples of Identifiers Valid Identifiers: x x1 x13bc George abc_def myStudent Invalid Identifiers 12 hello.cpp data-1 change/3

14 Identifier Conventions In addition to the rules, there are also conventions about identifiers that are commonly used: Variables start with lower-case letters Classes start with upper-case letters Literals are all upper-case If a variable is more than one word, the first word starts with a lower-case letter, other words begin with upper-case letters, e.g., myFirstVariable Underscores are not often used in C++, but are in C

15 Variables Variables are identifiers which are names for data objects. They are used for storing values in data objects and for accessing those values. Think of the data object as a box and the variable as the label on the box. (A box can have more than one label on it – we will get to that later.)

16 Definitions and Declarations Before a data object object can be used it must be created and given a type (what kind of thing can be stored in it). This is called a definition. Before a variable can be used it must be given a type as well. This is called a declaration. In C++, we usually combine a declaration with a definition. (This is different from Java.)

17 Declarations and Definitions - Exs. int a; This creates a new data object that can hold an integer, and assigns the name a to that data object. a is an integer variable. char foobear; This creates a data object that can hold a character and assignes the name foobear to it. f oobear is a character variable

18 Types The are several common types of data objects: integers ( int ), floating point – similar to reals ( float, double ), characters, ( char ), Boolean – true/false ( bool ). There are also other types, long and short.

19 Assignment Statements An assignment statement is a way to change the value stored in a data object. The left-hand side of an assignment statement is the name of the data object (a variable), and the right-hand side is the value that will replace the contents of the data object. The right-hand side may be an expression whose value is put into the data object.

20 Assignment Statement - Example The syntax for an assignment statement is Variable = Expression; For example, x = x + y; is an an assignment statement. The variable on the left-hand side indicates where to put the value. Any variables on the right-hand side are used to retrieve values stored in their data objects.

21 Assignment Statement - Example x = x + 1; means to take the old value of the data object (box) labeled x, add 1 to that value, and store that value back in the data object labeled x.

22 Variable Initialization Be sure to put a value into a data object before trying to use that value, i.e., all variables should be initialized before being used. Initialization can be done at the same time as declaration/definition. For example, int a = 5; creates a new int data object, labels it with the variable a, and sets its initial value to 5.

23 Expressions Simple expressions combine operators, variables, and literals in a meaningful way. C++ uses common symbols for arithmetic operators, + (plus), - (minus), * (times), / (divides), and also % for remainder. The order of evaluation is the one used in mathematics: *, /, % done first, and then +, -. Parentheses may be used to change the order.

24 Expressions - Examples a + b * c / 2 – 5 (a + b) * c / (2 – 5) frodo + samwise + gandalf + strider 7.2 / 5.0 – 6

25 Exercise Write a short program that calculate and prints out the area of a circle of radius 7. Create a double, called PI, to store the value of PI. Create a int, r, for the radius 7. Have an output statement to calculate and print out the area.


Download ppt "A Sample Program #include using namespace std; int main(void) { cout << “Hello, world!”; return 0; }"

Similar presentations


Ads by Google