1 / 100

C++ Programming Basics

C++ Programming Basics. Running a C++ Program. C++ source code is written with a text editor The compiler on your system converts source code to object code. The linker combines all the object code into an executable program. Edit. The first phase consist of editing a file.

Télécharger la présentation

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

  2. Running a C++Program C++ source code is written with a text editor The compiler on your system converts source code to object code. The linker combines all the object code into an executable program.

  3. Edit • The first phase consist of editing a file. • The programmer types a C++ program with editor. • The program source file is then stored on secondary storage device such as disk. • C++ program file names often end with .cpp

  4. Compile • Next, the programmer gives the command to compile the program. • The compiler translates the C++ program into machine language code. • In C++ system • A preprocessor program executes automatically before the compiler’s translation phase begins. • Preprocessor obeys commands called preprocessor directives. • Indicates that certain manipulations are to be performed on the program before compilation.

  5. Loader • The next phase is called loading. • Before a program can be executed, the program must first be placed in memory. This is done by the loader, which takes the executable image from disk and transfers it to memory.

  6. Execute • Finally, the computer executes the program one instruction at a time.

  7. Basic Program Construction • Compilers take source code and transform it into executable files, which your computer can run as it does other programs. • Source files are text files (extension .CPP) • Executable files have the .EXE extension

  8. A Sample C++Program * A simple C++ program begins this way #include <iostream> using namespace std; int main() { Statements; return 0; }

  9. #include <iostream> using namespace std; int main() { cout << “Hello World\n”; return 0; }

  10. Preprocessor Directives • The first line that begins the program is preprocessordirective. e.g., #include <iostream.h> • It isn’t a program statement • It isn’t a part of a function body • It does not end with a semicolon

  11. Program statement vs preprocessor directive • Program statements are instructions to the computer to do something, such as adding two numbers or printing a sentence. • A preprocessor directive, on the other hand, is an instruction to the compiler.

  12. Program statement vs preprocessor directive • The preprocessor directive #include tells the compiler to insert another file into your source file. • The type file usually included by #include is called a header file.

  13. Header Files • The preprocessor directive #include tells the compiler to add the file iostream to the source file before compiling. Why do this?

  14. Header Files • iostream is an example of a header 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

  15. Always Start with main() • When you run a C++ program, the first statement executed will be at the beginning of a function called main(). • The program may consist of many functions, but on startup, control always goes to main(). • If there is no function called main() in your program, an error will be reported when you run the program.

  16. Preprocessor Directives • Commands supplied to the preprocessor • Runs before the compiler • Modifies the text of the source code before the compiler starts • Syntax • start with #symbol • #include <headerFileName> • Example#include <iostream>

  17. Namespace • The #include <iostream>command is wherecinand coutare declared • They are declared within a namespace called std • When we specify using namespace std; • Then we need not preface the cin andcout commands with std::cinand std::cout

  18. Functions • The parentheses following the word main are the distinguishing feature of a function. • Without parentheses compiler would think that main refers to some other program element. • The word intpreceding the function name indicates that this particular function has a return value of type int.

  19. Braces and the Function Body • The body of a function is surrounded by braces (sometimes called curly brackets). • Every function must use this pair of braces around the function body. • In this example there are only two statements in the function body: • the line starting with cout and the line starting with return. • However, a function body can consist of many statements.

  20. Program Statements • There are two statements in the FIRST program: the line • cout << “Hello World: My first C++ program”; • and the return statement return 0;

  21. Output Using cout • The identifier cout (pronounced “C out”) is actually an object. It is predefined in C++ to correspond to the standard output stream. • The operator << is called the insertionor put tooperator. • It directs the contents on its right to the object on its left. • In our example it directs the string constant “Hello World: My first C++ program” to cout, which sends it to the display.

  22. String Constants • The phrase in quotation marks, “Hello World: My first C++ program”, is an example of a string constant. • Its value is set when the program is written, and it retains this value throughout the program’s existence.

  23. Program Statements • 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 signal an error.

  24. Program Statements • The last statement in the function body is return 0; • This tells main() to return the value 0 to whoever called it, in this case the operating system or compiler. The value 0 indicates that the program had terminated successfully.

  25. Whitespace • Compiler ignores whitespace almost completely. • Whitespaceis defined as spaces, tabs and newlines. • These characters are invisible to the compiler. • You can put several statements on one line, separated by any number of spaces or tabs, or you can run a statement over two or more lines.

  26. Two new lines

  27. Exceptions to the rule • The first line of the program, starting with #include, is a preprocessor directive, which must be written on one line. • Also, string constants, such as “Every age has a language of its own”, cannot be broken into separate lines. • If you need a long string constant, you can insert a backslash(\) at the line break or divide the string into two separate strings, each surrounded by quotes.

  28. Backslash

  29. Comments • They help the person writing a program, and anyone else who must read the source file, understand what’s going on. • The compiler ignores comments

  30. Comment Syntax • Comments start with a double slash symbol (//) and terminate at the end of the line.

  31. Alternative Comment Syntax

  32. AFirstProgram-Greeting.cpp //Program: Display greetings Preprocessordirectives Comments #include<iostream>usingnamespacestd; Providessimpleaccess Functionnamedmain()indicatesstartofprogram intmain(){ cout<<"!!!HelloWorld!!!"<<endl;return0; } Insertionoperator Endsexecutions ofmain()whichendsprogram Insertion statement Function

  33. Bits&Bytes • Abitisasinglenumericvalueeither‘1’or‘0. • Abyteis asequenceofbits. • 8 bits=1byte.

  34. Variables Astoragebox,itstypedefinesthekind ofstuffyoustoreinit E.g.shoeboxusedforshoes Jewelrybox usedforjewelry CDkit usedforCDs

  35. Variable Variableisabox Incomputer,astorageboxisamemorylocationon RAM

  36. MemoryinComputer Firstyouaskcomputertogiveyouaboxandyoualsohavetospecifywhattypeofboxyouwant Eachlittleboxisamemorylocation OccupiedMemorySpaces FreeMemorySpaces

  37. Variables • Variables are the most fundamental part of any language. • A variable has a symbolic name and can be given a variety of values. • Variables are located in particular places in the computer’s memory. • When a variable is given a value, that value is actually placed in the memory space assigned to the variable.

  38. Variable types • Most popular languages use the same general variable types, such as integers, floating-point numbers, and characters

  39. Integer variables • Integer variables represent integer numbers like 1, 30,000, and –27. • Such numbers are used for counting discrete numbers of objects, like 11 pencils or 99 bottles of beer. • Unlike floating point numbers, integers have no fractional part; you can express the idea of fourusing integers, but not four and one-half.

  40. Contdd . . . . • 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.

  41. Defining Integer Variables

  42. Here’s a program that defines and uses several variables of type int:

  43. Explanation • The statements int var1; int var2; define two integer variables, var1 and var2 • The keyword int signals the type of variable. • These statements, which are called declarations, must terminate with a semicolon, like other program statements.

  44. Contdd . . . . • You must declare a variable before using it. • However, you can place variable declarations anywhere in a program.

  45. Declarations and Definitions • A declaration introduces a variable’s name (such as var1) into a program and specifies its type (such as int). • However, if a declaration also sets aside memory for the variable, it is also called a definition.

  46. Variable Names • The names given to variables (and other program features) are called identifiers. What are the rules for writing identifiers? • You can use upper- and lowercase letters, and the digits from 1 to 9. • You can also use the underscore ( _ ). • The first character must be a letter or underscore.

  47. Contdd . . . . • Identifiers can be as long as you like, but most compilers will only recognize the first few hundred characters. • The compiler distinguishes between upper- and lowercase letters, so Var is not the same as var or VAR. • You can’t use a C++ keyword as a variable name.

  48. Keyword • A keywordis a predefined word with a special meaning. • int, class, if, and while are examples of keywords.

More Related