1 C++ Introduction
1 C++ Introduction
in C++
Software Development Cycle
Compile
Library routines
Edit Link
Other object files
Think Load
Execute
What are the Key Ingredients of a Program?
• Common elements in programming languages:
– Keywords (aka reserved words)
• Defined in C++. Have a special meaning
– Programmer-defined entities
• Names made up by programmer
• Used to represent variables, functions, etc.
– Operators
• Defined in C++
• For performing arithmetic, comparison, etc. operations
– Constructs
Defined in C++
Sequence, selection, repetition, functions, classes, etc.
#include <iostream>
using namespace std;
int main()
{
declaration(s)
statement(s)
return 0;
}
The statements specify the algorithm for the solution to your problem.
Example – Algorithm for Converting Miles to Kilometers
• Problem Input
miles distance in miles
• Problem Output
kms distance in kilometers
• Algorithm will use Conversion Formula
1 mile = 1.609 kilometers
• Formulate the algorithm that solves the problem.
• Algorithm
1. Get the distance in miles.
2. Convert the distance to kilometers by multiplying by 1.609
3. Display the distance in kilometers.
Statements may be
- Simple – typically one line of program code
#include <iostream>
using namespace std;
int main()
{
declaration(s)
statement(s)
return 0;
}
#include <iostream>
using namespace std;
The #include Directive
int main()
• Preprocessor directive {
declaration(s)
statement(s)
• Inserts the contents of another file into the program return 0;
}
iostream is C++ library of input/output functions
Variables, as their name implies, are data whose values can change
during the course of execution of your program.
For both constants and variables, the name, the data type, and value
must be specified.
Syntax
const type name = expression;
The statement must include the reserved word const, which designates
the declaration as a constant declaration.
Examples
const float TAXRATE = 0.0675;
const int NUMSTATES = 50;
Ends execution
of main() which ends the
program
The basic data types supported by C++ are shown in the table
below
The three integer types allow for whole numbers of three different
sizes.
The two real types permit numbers with decimal parts with two
different amounts of precision.
Finally, there is one type for character data that consists of a single
character, and one type for logical values that are either true or false.
The cout Output Statement
#include <iostream>
using namespace std;
int main()
{
cout << " * " << endl;
cout << " *** " << endl;
cout << " ***** " << endl;
cout << " * " << endl;
cout << " * " << endl;
cout << " * " << endl;
}
#include <iostream> Example
using namespace std;
int main() {
double radius;
double area;
// Step 1: Set value for radius
radius = 20;
// Step 2: Compute area
area = radius * radius * 3.14159;
// Step 3: Display the area
cout << "The area is ";
cout << area << endl;
}
The cin Statement
• Syntax
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "What is your name? ";
cin >> name;
• Identifiers should be
– Short enough to be reasonable to type (single word is the norm)
• Standard abbreviations are fine
– Long enough to be understandable
• When using multiple word identifiers capitalize the first letter
of each word – but this is just a convention
• Examples
– Min
– Temperature
– CameraAngle
– CurrentNbrPoints
Example - Valid and Invalid Identifiers
totalSales Yes
total_Sales Yes
- On separate lines:
int length;
int width;
unsigned int area;
int x; // Declare x to be an
// integer variable;
char a; // Declare a to be a
// character variable;
Variable Assignments and Initialization
Assignment
variable = expression ;
Initialization
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
/ Operator
area = 2 * PI * radius;
Example
#include <iostream>
using namespace std;
int main()
{
cout << (1 + 2 + 3) / 3 << endl; // not the same as 1+2+3/3
return 0;
}
Expressions
y 2 isywritten
1 as m = (y2-y1) /(x2-x1);
m
x 2 x1
Note the use of brackets to group variables into expressions and
inform the compiler the order of evaluation.
Example
m = (y2-y1) /(x2-x1);
Compound Assignment
• Examples
int i = 3;
i += 4; // i is now 7 Equivalent to i = i + 4
float a = 3.2;
a *= 2.0; // a is now 6.4 Equivalent to a = a * 2.0
Shorthand Assignment Operators
• Consider
float y = 2.7;
int i = 15;
int j = 10;
i = y; // i is now 2
y = j; // y is now 10.0
Operators and Precedence
• Example
20 - 4 / 5 * 2 + 3 * 5 % 4
(4 / 5)
((4 / 5) * 2)
((4 / 5) * 2) (3 * 5)
((4 / 5) * 2) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) + ((3 * 5) % 4)
Increment and Decrement Operators
int k = 4;
++k; // k is 5
k++; // k is 6 Nothing strange so far!
BUT
fstream
- File stream processing
assert
- Assertion processing
iomanip
- Formatted input/output (I/O) requests
ctype
- Character manipulations
cmath
Trigonometric and logarithmic functions
Note
C++ has many other libraries
C++ cmath Library
Function call
y = sqrt(x);
Function Function
name argument
Some Mathematical Library Functions
Example
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Enter Quadratic coefficients: ";
double a, b, c;
cin >> a >> b >> c;
if ( (a != 0) && (b*b - 4*a*c > 0) ) {
double radical = sqrt(b*b - 4*a*c);
double root1 = (-b + radical) / (2*a);
double root2 = (-b - radical) / (2*a);
cout << "Roots: " << root1 << " " << root2;
}
else {
cout << "Does not have two real roots";
}
return 0;
}