
























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The basics of C++ programming, including program construction, output using cout, string constants, directives, comments, defining an integer variable, integer constant, and C++ data types. It provides code examples and explanations for each topic.
Typology: Slides
1 / 64
This page cannot be seen from the preview
Don't miss anything!

























































//first.cpp #include
Explanation: ◼ This program consists of a single function called main().
◼ Every function must use this pair of braces around the function body. ◼ A function body can consist of many statements but this function has only three statements (line number 6, 7 and 8) ◼ You can put several statements on one line and one statement in over two or more lines. cout <<"Welcome to this course\n" ;
1 cout <<"Welcome Students\n" ; return 0 ;
◼ We don’t recommend these syntax. it’s nonstandard and hard to read. but it does compile correctly. ◼ #include is a preprocessor directive , which must be written on one line. ◼ #include
to this
course\n" ; cout <<"Welcome " "to this " "course\n" ;
◼ There are two statements this program cout <<"Welcome to this course\n"; return 0; ◼ The first statement tells the computer to display the quoted phrase. ◼ A semicolon ; signals the end of the statement. If you leave out the semicolon, the compiler will often signal an error. ◼ The return 0; tells main() to return the value 0 to whoever called it, in this case the operating system or compiler. ◼ you can not give main() the return type of void , this is an error in new compilers.
◼ (If you know C, you’ll recognize << as the left-shift bit-wise operator and wonder how it can also be used to direct output. ◼ In C++, operators can be overloaded. That is, they can perform different activities, depending on the context.
◼ The phrase in quotation marks, "Welcome to this course\n", is an example of a string constant. ◼ A constant , unlike a variable, cannot be given a new value as the program runs. Its value is set when the program is written, and it retains this value throughout the program’s existence. ◼ The '\n' character at the end of the string constant is an example of an escape sequence. ◼ The '\n' causes the next text output to be displayed on a new line. ◼ Line no. 2 and 3 in our program are called directives. The first is a preprocessor directive ,and the second is a using directive.
◼ The preprocessor directive #include tells the compiler to insert another file into your source file. In effect, the #include directive is replaced by the contents of the file indicated. ◼ Using an #include directive to insert another file into your source file is similar to pasting a block of text into a document with your word processor. ◼ #include is only one of many preprocessor directives, all of which can be identified by the initial # sign. ◼ The type file usually included by #include is called a header file.
◼ In our program the preprocessor directive #include tells the compiler to add the source file IOSTREAM to the FIRST.CPP source file before compiling. ◼ IOSTREAM is an example of a header file (sometimes called an include file). It’s concerned with basic input/output operations, and contains declarations that are needed by the cout identifier and the << operator. ◼ Without these declarations, the compiler won’t recognize cout and will think << is being used incorrectly. ◼ The newer Standard C++ header files don’t have a file extension, but some older header files have the extension .H.
◼ Comments help the person writing a program, and anyone else who must read the source file, understand what’s going on. ◼ The compiler ignores comments, so they do not add to the file size or execution time of the executable program. ◼ Comments start with a double slash symbol ( // ) and terminate at the end of the line //first.cpp #include
/ this is an old-style comment / / this is a potentially very long multiline comment / int main(/ a comment within parentheses/) { /* this is comment into the function body / } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16*
◼ Integer variables exist in several sizes, but the most commonly used is type int. ◼ The amount of memory occupied by the integer types is system dependent. ◼ On a 32 - bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of memory. This allows an int to hold numbers in the range from – 2,147,483,648 to 2,147,483,. ◼ The type int occupies 4 bytes on current Windows computers, it occupied only 2 bytes in MS-DOS and earlier versions of Windows.
◼ Figure: A variable of type int in memory