C++ Basics
C++ is a high-level, general purpose, object-oriented programming language.
C++ Basics
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.
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
C++ Basics
Source code
Object code
Compiler
TRANSLATION
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).
C++ Basics
Source code
Object code
Compiler LinkerExecutable code
TRANSLATIONOther files
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
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>
Basic C++ Rules
almost every C++ program will contain the lines ..
int main(void) { .....
return 0; }
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
Declaring Variables
Example:
double x; int X,y, z; char d;
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" _.
type
varName
Assigning values to variables
Once a variable is declared, you can assign values to it.
varName = expression
Example: double x; x = 10.0;
expressionvarName
Note that this operation looks like the math symbol for equal, but it works differently
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
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 computedcomputed value is
assigned to the variable on the left
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;
Defining Constants
To define a constant in C++ you preface the type of the variable by the keyword const.
Example:
const double pi = 3.1415;
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.
Now we can produce some more interesting programs.
A sample program
Task 1: Create a program that asks the user for the radius of a disk, then computes the area and circumference.
A sample program
Stage 0: As usual, our stage-0 program is:
#include <iostream.h>
int main() { ......
return 0; }
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 }
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 = 3.1415; // 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; }
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 = 3.1415; double r,A,C; cin >> r; A = pi * r^2; C = 2 * pi * r; cout << A; cout << C; return 0; }
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;
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.
A sample programStage 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 = 3.1415; 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; }
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;
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 = 3.1415; 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; }
Software Development
When creating a program, you usually proceed in four distinct stages: Problem Analysis Design Coding Verification and Validation
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.
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.
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.
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 ?
1.Problem
Create a program that will compute the volume of a sphere, given its radius.
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 $1201.00 income.