computer science 1620 c++ - basics. #include using namespace std; int main() { return 0; } a very...
Post on 22-Dec-2015
221 views
TRANSCRIPT
![Page 1: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/1.jpg)
Computer Science 1620
C++ - Basics
![Page 2: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/2.jpg)
#include <iostream> using namespace std;
int main() {
return 0;}
• A very basic C++ Program.
• When writing your first programs, always start with this template.
• The code that you should write should go here.
• The details of this template (what each component means) will become clear as the semester progresses.
![Page 3: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/3.jpg)
coutconsole output
sends data to the current output stream for us, this will typically mean the screen this can be re-routed to other output devices if
necessarywhen you wish to display something to the
screen, use the following syntax:
cout << a valid C++ expression
![Page 4: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/4.jpg)
C++ Expressionan expression in C++ is an entity that
represents a value this value can take on many typeswe will look at one type of expressions first
string literal
![Page 5: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/5.jpg)
String Literala string literal is a sequence of charactersenclosed in quotation marksExamples:
"Hello""Computer Science 1620" ”Dolly Parton"
![Page 6: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/6.jpg)
Back to cout to display a string literal to the screen,
include it on the right side of the << operator
Example:write a program that displays the text "Hello
world!" on the computer screen.
![Page 7: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/7.jpg)
#include <iostream> using namespace std;
int main() {
cout << "Hello World!"; cout << endl;
return 0;}
String literal
![Page 8: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/8.jpg)
![Page 9: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/9.jpg)
Notice the semicolon at the end of each lineall of your C++ constructs will end in a
semicolon (unless otherwise instructed) indicates the end of an instruction
![Page 10: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/10.jpg)
What about the second cout command?cout << endl;
endl causes the data to be flushed from the buffer (explained in class), and moves the input to the next line
your output should always include a call to endl (at some point)
![Page 11: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/11.jpg)
cout concatenation instead of having two separate cout
statements, you can include them on the same line
![Page 12: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/12.jpg)
#include <iostream> using namespace std;
int main() { cout << "Hello World!" << endl;
return 0;}
![Page 13: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/13.jpg)
#include <iostream> using namespace std;
int main() { cout << "Hello " << "World!" << endl;
return 0;}
![Page 14: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/14.jpg)
Example: Write a program that writes your name and address to the screen:
#include <iostream> using namespace std;
int main() { cout << "Robert Benkoczi" << endl; cout << "123 Sunset Blvd" << endl; cout << "Hollywood, CA" << endl; cout << "90210" << endl;
return 0;}
![Page 15: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/15.jpg)
applepie $ g++ -o print-adr print-adr.cc applepie $ ./print-adr Robert Benkoczi123 Sunset BlvdHollywood, CA90210applepie $
![Page 16: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/16.jpg)
Recap: structure of a C++ program
the first 2 lines are almost always included in a C++ program. every program begins with “main ( )”. main begins with { and ends with } the brackets ( ) after main are required. program starts and ends in “main”. return 0 is put right before the closing } statements always end with a semicolon. Anything preceded with // is a comment and the compiler ignores it.
#include <iostream>#include <iostream>using namespace std;using namespace std;int main( )int main( ){ { statement 1;statement 1;
statement 2;statement 2;return 0;return 0;
} // end of program} // end of program
![Page 17: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/17.jpg)
Other data types
C++ Data Typessimplestructured (struct, class)pointers function
we will deal with these later
![Page 18: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/18.jpg)
Other data types
C++ Data Types four categories of simple data
integer whole numbers, no fractional part
floating point real numbers
enumerationboolean
![Page 19: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/19.jpg)
Integer types a number without a decimal part
integers: 10, -24, 5800, -9600, etc…
non-integers 14.5, -24.6, 5800.0, -8 x 106
How are integers written in C++? in the simplest case, exactly as you would expect no spaces or punctuation (commas, dollar signs, etc)
![Page 20: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/20.jpg)
Note that a number in C++ is an expressionhence, the number 34 is an expression
typically called an integer literal the value of this expression is 34
because it is an expression, it can be used wherever an expression is permitted
eg. as data to cout
![Page 21: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/21.jpg)
Write a program that displays your name on the first line, and age on the second line. Display your name as a string, and your age as an integer.
#include <iostream> using namespace std;
int main() {
return 0;}
![Page 22: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/22.jpg)
Integer types there are actually several flavours of "integer" in C++
by default, all integer literals are ints we will defer discussion about the others until we take
variables
* compiler dependent
Type Range Size
char -128 .. 127* 1 byte
short -32768 .. 32767* 2 bytes
int -2147483648 … 2147483647* 4 bytes
long -2147483648 … 2147483647* 4 bytes
![Page 23: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/23.jpg)
Internal vs source code representations
Type:used to specify the internal representation
of a value (or expression). EX: how is that value stored in the computer memory.
Source code representation:how do we write a literal in the source code
![Page 24: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/24.jpg)
Representation of integers
Representing numbers: roman: I, II, III, IV, V, VI, VII, VIIII, IX, Xarabic: coins with face value 1, 10, 100, 1000 etc
Base 2 numbering (face value 1, 2, 4, 8...)13 = 10 + 313 = 8 + 4 + 1 = 1101 (meaning 1 x 8, 1 x 4, 0 x 2,
1 x 1)
![Page 25: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/25.jpg)
Representation of integers
Representing numbers: roman: I, II, III, IV, V, VI, VII, VIIII, IX, Xarabic: coins with face value 1, 10, 100, 1000 etc
Base 2 numbering (face value 1, 2, 4, 8...)13 = 10 + 313 = 8 + 4 + 1 = 1101 (meaning 1 x 8, 1 x 4, 0 x 2,
1 x 1)
Addition is easy with arabic numerals!bits
![Page 26: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/26.jpg)
Representation of integers
char (8 bits or 1 byte)0 1 1 1 1 1 1 1 = +127
int, long - same idea, more bits.
represents the sign (+/-)
![Page 27: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/27.jpg)
Representation of integers
negative integers1 0 0 0 0 0 0 0 = -0 (?)
This one’s complement representation. Not nice to have negative 0!
We would like to add + and - integers using the same procedure as for adding + integers.
represents the sign (+/-)
![Page 28: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/28.jpg)
Representation of integers
two’s complement representation1 ? ? ? ? ? ? ? = -x
Goal: x + (-x) = x - x = 0 = 0 0 0 0 0 0 0 0
represents the sign (+/-)
![Page 29: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/29.jpg)
Unsigned integers
Use when there is no need to represent negative integers. We can represent more positive integers.
Type Range Size
unsigned char
0 .. 255* 1 byte
unsigned short
0 .. 65536* 2 bytes
unsigned int
0 ... 4 294 967 295* 4 bytes
unsigned long
0 ... 4 294 967 295* 4 bytes
![Page 30: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/30.jpg)
Floating Point Numbers a number with a decimal part
floating point numbers: 10.8, -24.8372, 5800.0, etc…
non-fps 14, 28, 96, -49
How are fp numbers written in C++? (source code representation) in the simplest case, exactly as you would expect no spaces or punctuation (commas, dollar signs, etc) scientific notation:
<mantissa> E <exponent> = <mantissa> 10<exponent>
![Page 31: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/31.jpg)
Write a program that displays your name on the first line, age on the second line, and bank account balance on the 3rd.
#include <iostream> using namespace std;
int main() {
return 0;}
![Page 32: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/32.jpg)
Floating Point types there are actually several flavours of fps in C++
by default, all floating point literals are doubles we will defer discussion about the others until we take
variables
* compiler dependent
Type Range Size
float 1.17549e-38 ... 3.40282e+38 4 bytes
double 2.22507e-308 ... 1.79769e+308* 8 bytes
long double 3.3621e-4932 ... 1.18973e+4932* 10 bytes
![Page 33: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/33.jpg)
Integers vs. Floating Pointwhy would you use a floating point instead
of an integer?when you are working with real numbers
why would you use an integer instead of a floating-point number?
1) Integers use less memory than doubles2) Integers are more precise than floats
more on this when we take variables
![Page 34: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/34.jpg)
Why would you use either number type instead of a string? after all, could we not just say:
cout << "Car Price:" << endl;
cout << "32999" << endl;
two reasons 1) Cannot do arithmetic on a string 2) Strings typically require more space than the number.
![Page 35: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/35.jpg)
1) Cannot do arithmetic on a string next class, we will perform arithmetic on numbers
cout << (32999 – 5000) << endl;
you cannot perform the same operation on the string representation
cout << ("32999" – "5000") << endl;
![Page 36: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/36.jpg)
2) Strings typically require more space than a numberas mentioned, an int requires 4 bytes of
memorya string requires x+1 bytes, where x is the
number of characters in the string32999 requires 4 bytes to store"32999" requires 6 bytes to store
![Page 37: Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,](https://reader030.vdocuments.us/reader030/viewer/2022032523/56649d795503460f94a5d28c/html5/thumbnails/37.jpg)
Exercise: Determine an appropriate data type (int, fp, string) for each of the following?1) The average height of the class2) The number of desks in the class3) The name of the class4) The distance from my office to the
classroom