Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Basics C++ is a high-level, general purpose, object-oriented programming language.

Similar presentations


Presentation on theme: "C++ Basics C++ is a high-level, general purpose, object-oriented programming language."— Presentation transcript:

1 C++ Basics C++ is a high-level, general purpose, object-oriented programming language.

2 C++ Basics no less, but also no more. but yourself.
When  executing a program the computer only does what you have programmed , no less, but also no more. If you forget anything, or the output does not look pretty, or the results are incorrect, there's nobody to blame but yourself.

3 C++ Basics The computer understands (long) sequences of 0's and 1's, humans only understand a natural language such as English. Therefore, there is a translation mechanism involved when you try to tell the computer what you want it to do

4 C++ Basics TRANSLATION Compiler Object code Source code

5 C++ Basics Source code: The source code is a text document that contains statements written according to the rules Object code: A sequence of 0's and 1's that the computer can, in principle, understand (machine code). A program called the compiler is used to translate the source code into object code. Executable code: Sequence of 0's and 1's, consisting of your object code plus any other modules (machine code).

6 C++ Basics TRANSLATION Other files Compiler Linker Object code
Source code Executable code

7 C++ Basics When you write C++ programs,
There are several things to keep in mind: Your program does exactly and only what you define, no more and no less C++ programs execute, from top to bottom C++ programs can use cin to get input from a user, and cout to display output C++ programs usually do some computations

8 Basic C++ Rules every C++ program uses certain keywords that have special meaning (e.g. int, main, void, return) every C++ program uses certain operators that perform specific actions (e.g. +, *, cin, cout) every C++ program is case-sensitive, (e.g. int is different from Int or INT) every C++ program uses curly brackets to group statements together {} every C++ program has a semicolon at the end of every instruction almost every C++ program that handles user input and output will contain the line #include <iostream.h>

9 Basic C++ Rules almost every C++ program will contain the lines ..    int main(void)    {      ..... return 0;    }

10 Declaring Variables C++ provides several different basic data types. The most important ones are: double: a decimal number int: an integer number char: a single letter or special symbol, anything that is on your keyboard

11 Declaring Variables Example: double x; int X,y, z; char d; varName
C++ is case-sensitive Variable names can not contain spaces. They must start with letters, and can contain only letters, numbers, and certain special symbols such as an "underscore" _. varName type

12 Assigning values to variables
Once a variable is declared, you can assign values to it. varName = expression Example: double x; x = 10.0; expression varName Note that this operation looks like the math symbol for equal, but it works differently

13 Assigning values to variables
When you assign an expression to a variable, the following happens: first, the value of the right side is computed second, that computed value is assigned to the variable on the left

14 Assigning values to variables
Example: double x, y; int i, k; x = 2.8; y = -1.4*x; i = 9; k = (i % 2) * (7 + 5*i); the value of the right side is computed computed value is assigned to the variable on the left

15 Assigning values to variables
Combined Declaration and Assignment: In C++, you can declare a new variable, and at the same time assign a value to it (or initialize the variable) Example; double x = 1.0; int i = 10, j = 20; int k = i + j;

16 Defining Constants To define a constant in C++ you preface the type of the variable by the keyword const. Example: const double pi = ;

17 Defining Constants A constant can not change inside your program.
usually declared at the beginning of your program, must be assigned a value at the time you declare them.

18 Now we can produce some more interesting programs.

19 A sample program Task 1: Create a program that asks the user for the radius of a disk, then computes the area and circumference.

20 A sample program Stage 0: As usual, our stage-0 program is:
#include <iostream.h> int main() {      return 0; }

21 A sample program Stage 1: We use comments to break up our task into smaller subtasks : #include <iostream.h> void main() {    // get the radius from the user    // compute the area    // compute the circumference    // display the answers }

22 A sample program Stage 2: Now we get into the details of which variables and formulas to use: #include <iostream.h> int main() { const double pi = ;    // need a variable r for the radius double r;    // getting the input from the user    cin >> r;     // computing the area A = pi * r^2    double A = pi * r^2;    // computing the circumference    double C = 2 * pi * r;     // displaying both answers    cout << A;    cout << C; return 0; }

23 A sample program #include <iostream.h> int main() {
Stage 2: Now we get into the details of which variables and formulas to use: #include <iostream.h> int main() {    const double pi = ; double r,A,C;    cin >> r;    A = pi * r^2;    C = 2 * pi * r;    cout << A;    cout << C; return 0; }

24 A sample program At this point, we let the compiler tell us if the C++ grammar is correct or not. The compiler will tell us the r^2 is "unknown", so we change that line to: A = pi * r*r;

25 A sample program Then we compile it again. It will now compile
So we can link it to produce the executable file. Finally, we execute the program to test it, and we find that everything works, but it does not look good. So, we'll modify the program input&output one more time.

26 A sample program Stage 3: We add some more input/output statements to make our program more "appealing" to the user: #include <iostream.h> int main() {    const double pi = ; double r,A,C; cout << "Please enter the radius: ";    cin >> r;    A = pi * r*r;    C = 2 * pi * r; cout << "The area is: ";    cout << A; cout << "The circumference is: ";      cout << C; return 0; }

27 A sample program Acutally, the last four statements can be linked together. Instead of saying:    cout << "The area is: ";    cout << A;    cout << "The circumference is: ";     cout << C; we can also say: cout << "The area is: " << A << " and the circumference is " << C;

28 A sample program After changing that, our program will work correctly, and produce reasonably nice looking results on the screen. #include <iostream.h> int main() {    const double pi = ; double r,A,C; cout << "Please enter the radius: ";    cin >> r;    A = pi * r*r;    C = 2 * pi * r; cout << "The area is: " << A << "The circumference is: " << C; return 0; }

29 Software Development When creating a program, you usually proceed in four distinct stages: Problem Analysis Design Coding Verification and Validation

30 Stage 1: Problem Analysis
In this stage you analyze what exactly it is that your program needs In particular, you describe: all input values, i.e. values that must be supplied from outside the program all constant values, i.e. values that are given with the problem all output values, i.e. values that must be produced as part of the solution to the problem You should also think about the types of all these values.

31 Stage 2: Design In this stage,
break up the problem into subtasks in the order write the pseudocode, using comments to describe the subtasks instead of actually coding it. All necessary formulas are part of this stage.

32 Stage 3: Coding In this stage
enter the code for your program, following the rules that C++ requires. leave the comments from stage 2, and put your code after the respective comments. The end product of this stage should be a program that compiles and links without errors.

33 Stage 4: Verification and Validation
Check if your program works correctly, Is the sequence of events correct ? Does your program print out enough information to guide the user as to what to do and what the output means ?

34 1.Problem Create a program that will compute the volume of a sphere, given its radius.

35 2.Problem : You are working as a consultant for a cable company. For each installation that is performed by the company, there's a $25.00 service charge and an additional $2.00 charge per meter of cable used. They need a program to compute the total income per month. In other words, if they use 263 meter of cable at 27 different locations, they make $ income.


Download ppt "C++ Basics C++ is a high-level, general purpose, object-oriented programming language."

Similar presentations


Ads by Google