Professional Documents
Culture Documents
Data Types
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Goals
• A variable
– is used to store information: the contents of the variable:
• can contain one piece
of information at a time.
– has an identifier: the name of the variable
–
Primitive types
integral floating
unsigned
Basic Data Types
bool values
true false
int ageOfDog;
•char middleInitial;
•float taxRate;
Variable Declaration
and
each space can contain only one car
and
only cars, not buses or trucks
int cans_per_pack = 6;
int
indicates that the variable cans_per_pack
will be used to hold integers.
= 6
indicates that the variable cans_per_pack
will initially contain the value 6.
• An assignment statement
cans_per_pack = 8;
counter
counter= =11; // //
11; setset
counter to 11
counter to 11
counter
counter= =counter
counter+ 2;
+ //
2; increment
// increment
Any questions?
That 2
is called a “magic number”
(so is that one)
because it would require magic to know what 2 means.
It is not good programming practice to use magic numbers.
Use constants.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Constants
...
• Comments
are explanations for human readers of your code
(other programmers).
• The compiler ignores comments completely.
Comment
/*
This program computes the volume (in liters) of a six-pack of soda
cans and the total volume of a six-pack and a two-liter bottle.
*/
int main()
{
int cans_per_pack = 6;
const double CAN_VOLUME = 0.355; // Liters in a 12-ounce can
double total_volume = cans_per_pack * CAN_VOLUME;
You must define a variable before you use it for the first time.
For example, the following sequence of statements would not
be legal:
? ?
Statements are compiled in top to bottom order.
When the compiler reaches the first statement, it does not know that
liter_per_ounce will be defined in the next line, and it reports an
error.
(not a . b or ab as in math)
In a + b / 2,
the b / 2 happens first.
pow(base, exponent)
b * pow(1 + r / 100, n)
cout << "Please enter your last three test scores: ";
int s1;
int s2;
r or
int s3; Er
cin >> s1 >> s2 >> s3;
double average = (s1 + s2 + s3) / 3;
cout << "Your average score is " << average << endl;
So 4.0 is stored.
or
(-(b * b - 4 * a * c) / (2 * a)
?
What is wrong with it?
-(b * b - (4 * a * c))) / 2 * a)
A reference site on
the Internet such as: http://www.cplusplus.com
is a great help.
It is easier to read
than
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
Itreallyiseasiertoreadwithspaces!
Write sqrt(x)
not sqrt (x)
You can read more than one value in a single input statement:
You can read more than one value in a single input statement:
Alternatively, the user can press the Enter key after each input:
Which do you think the user prefers to see on her gas bill?
price_per_liter = 1.21997;
cout << fixed << setprecision(2)
<< "Price per liter: $"
<< setw(8)
This code:
double price_per_ounce_1 = 10.2372;
double price_per_ounce_2 = 117.2;
double price_per_ounce_3 = 6.9923435;
cout <<fixed << setprecision(2);
cout << setw(8) << price_per_ounce_1 <<endl;
cout << setw(8) << price_per_ounce_2 <<endl;
cout << setw(8) << price_per_ounce_3<<endl;
cout << "--------" << endl;
produces this output:
10.24
117.20
6.99
--------
Once you set the precision, that width is used for all
floating-point numbers until the next time you set the
precision.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Read price per pack
cout << "Please enter the volume for each can (in ounces): ";
double can_volume;
cin >> can_volume;
return 0;
}
"Hello world"
#include <string>
using namespace std;
...
string name = "Harry";
// literal string "Harry" stored
string response;
// literal string "" stored
0?
0?
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
String Functions
H e l l o , W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
H e l l o , W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
H e l l o , W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
H e l l o , W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
cout << "Enter your first name: ";
string first;
cin >> first;
cout << "Enter your significant other's first name: ";
string second;
cin >> second;
string initials = first.substr(0, 1)
+ "&" + second.substr(0, 1);
cout << initials << endl;
return 0;
}