IntroC++Notes

advertisement
Intro. to C\C++ Programming
Programming - The process




Understand the problem and requirements
Design or select an algorithm to solve it
Express (code) the algorithm in a programming language (e.g., C)
Test and debug the program until it works and meets the requirements
Hats I (and the TA) will wear this semester
You
Me
Programming - The mechanics
1
Learning the setup of a program
 the setup is the same for many of your programs
 the setup must be in the SAME order as shown as below
Setup Example
Place name here
Place what the program is going to be done
Place all includes here
Place any (if any) global variables here
Place functions other than main here
Create main here
**Where most of your code goes!!**
Includes
Functions
Main
Description of Setup
Some commands and features require other “packages or
libraries”
Further breakdown of code (we cover more later)
where most of your code will be placed. Starts from top to
bottom.
2
Types of files in a program
(or sections of a program- it depends on your style):
1. .c\.cpp file :
The .c\.cpp, or source file, contains your functions (programming
procedures). This is the file you will primarily be writing in, because
functions are the bulk of C\C++ coding.
2. .h file:
The .h, short for header, always accompanies a .cpp file.
It contains:
a. declarations of functions created in the .cpp file
b. variables used throughout the program
c. special self-created classes, which are explained later in the year
d. list of reserved command words that you use
e. a “library” of functions, variables, and other functions that you can use
in your .c\.cpp file
3
Data types
Match the variables with their correct datatype. Take a WILD (not) guess.
Variable name and value
??? myGPA = 3.79;
??? myName = “Lupoli”;
??? myIQ = 152;
??? myMiddleInitial = ‘V’
Datatype
float
int (integer)
char (character)
String
Type
int
Examples/Definitions
int yards = 202;
a WHOLE number, ranging from -2147483647 to 2147483647
unsigned int
unsigned int feet = 6;
a WHOLE NON-NEGATIVE number ranging from 0 to 2147483647
short
short coordinate = -12;
a WHOLE number, ranging from -32767 to 32767
unsigned short unsigned short attitude = 32,000;
a WHOLE NON-NEGATIVE number ranging from 0 to 32767
long
long debit = 10000000000;
a WHOLE number, ranging from -2^63 to 2^63-1
unsigned long unsigned long population = 5000000000;
a WHOLE NON-NEGATIVE number ranging from 0 to 2^63-1
float
float GPA = 3.99;
a real number in a floating point representation
double
double mole = 1.2336483;
a double-precision real number in a floating point representation
long double
long double weight = 7.7493343658792749;
a extended-precision real number in a floating point representation
char
char choice = 'x';
ONE character, or a small INTEGER in the range from -128 to 127
boolean
bool found = true;
a Boolean value can either AND ONLY be true(1) or false(0)
string
string name = “Lupoli”;
One or more characters put together. Requires <string>.
 each variable of NO MATTER what type
o holds ONE value of that type
o variable name should MATCH what it is being used for
 cannot have a number as a first letter in it’s name
4
Input and Output
I/O stream
ke
y
b
oa
rd

stream

Computer
cin >>
scanf
cout <<
printf
Printing a Line of Text
C++
// Mr. Lupoli
// 1st program
#include <iostream>
using namespace std;
int main()
{
cout << “Welcome to C!” << endl;
cout << “Welcome”;
cout << “to Mr. Lupoli’s Class”;
return 0;
}
1. Draw a sizeable rectangle on your paper. If the rectangle was a monitor, what
would like after the code above completed?
2. Use the code above to display YOUR name on line ONE, and your town and state
on line TWO
Difference/Substitutions
1.) #include <stdio.h>
2.) printf(
3.) \n”
#include <iostream>
cout <<
”<< endl;
5
Using printf/scanf and cout/cin
 to display to the screen
o C  printf(
o C++  cout <<
 to read from the keyboard
o C  scanf(
o C++  cin >>
C and C++ Versions using I/O
// declare your variables first!!!
int x = 10;
double y = 23.3;
char z = ‘A’;
short int a = 12;
long int b = 12857612554;
long double c = 2834892734892.23;
printf
printf(“x is = %d \n”, x); // int
printf(“y is = %f \n”, y); // double
printf(“z is = %c \n”, z); // char
printf(“a is = %hd \n”, a); // short int
printf(“b is = %Ld \n”, b); // long int
printf(“c is = % Lf\n”, c); // long double
printf(“X is: %d and Y is: %d\n”, x, y); \\ 2 displ
cout <<
cout << “x is = “ << x << endl; // int
cout << “y is = “ << y << endl; // double
cout << “z is = “ << z << endl; // char
cout << “a is = “ << a << endl; // short int
cout << “b is = “ << b << endl; // long int
cout << “c is = “ << c << endl; // long double
cout << “x is =“ << x << “ and y =“ << y << endl;
What is the difference between the 2 x’s??
scanf
scanf(“%d”, &x); // int
scanf(“%f”, &y); // double
scanf(“%c”, &z); // char
scanf(“%hd”, &a); // short int
scanf(“%Ld”, &b); // long int
scanf(“%Lf”, &c); // long double
scanf(“%d %f”, &x, &y); // 2 done
cin >>
cin >> x; // int
cin >> y; // double
cin >> z; // char
cin >> a; // short int
cin >> b; // long int
cin >> c; // long double
cin >> x >> y; // 2 done
6
 Things to notice
o both are vary similar
o on a scanf/cin, DO NOT USE \n or endl!!!
o in a scanf, make sure you have &variable
o BE CAREFUL WITH QUOTES!!!
o can use ‘\n’ in C++ version instead of << endl;
Using cout << with variables
int x = 0;
int y = 8;
// MUST DECLARE ALL VARIABLE BEFORE USING
Match the output
(A) cout << "X is: " << x << " and Y is: " << y << endl;
(B) cout << "X is: << x <<
and Y is: << y" << endl;
??
??
Options:
(1) X is: << x << and Y is: << y
(2) X is: 0 and Y is: 8
7
literal escape constants/command constants
 only work INSIDE of " "
Escape Sequences
Escape
Sequence
\t
\a
\r
\\
\”
\’
\n
\b
\f
Description
tab
Audible alert (beep)
carriage return, go to beg. of next line
backslash
double quote
single quote
new line
back space
form feed
What will these display??
cout << “Hi Class! \a” << endl;
cout << “Good Luck!!! \n”;
cout << “\t \t You’ll need it!!!” << endl;
8
Using cin >>
 very tricky
 must match the DATA TYPE
cin >>
cin >> x; // int
cin >> y; // double
cin >> z; // char
cin >> a; // short int
cin >> b; // long int
cin >> c; // long double
First cin >> Example
#include <iostream>
using namespace std;
int main()
{
cout << "Hello C++ world" << endl;
int age = -1;
int dogYears = -1;
cout << "Please enter a value: " << endl;
cin >> age;
dogYears = age * 7;
cout << "You are " << dogYears << " in Dog Years!!" << endl;
return 0;
}
1.
2.
3.
Identify where the include statement is located
Identify where the cin command is located
Identify type of data (float, String, int) is being read in
Identify where the output is taking place
4.
Inputting a Number
Inputting a Decimal
Inputting a String
Hello C++ world
Please enter a value:
5
You are 35 in Dog Years!!
Hello C++ world
Please enter a value:
23.3
You are 161 in Dog Years!!
Hello C++ world
Please enter a value:
Lupoli
You are 0 in Dog Years!!
Why did entering a decimal (float) or String NOT break the program??
9
Use of Comments
//
//
//
//
//
/* … code … */
Mr. Lupoli
Project 1
6/22/03
Period 1
/*
Mr. Lupoli
Project 1
6/23/03
Period 1
//  “//”reserves REST of line for */
a // comment
/*
“ /* ” reserves whole block until
you end it with a “ */ “
// used for ONE line comments
void main()
{
int counterValue;// sentinel value
used for MULTIPLE lined comments
*/
Why use comments?
 For notes
o to yourself
o to me!!
 For commenting out unfinished lines of code
o skipping unfinished functions
 To understand what the code is doing!!
 watch where you put them!!
Reserved Words
 case sensitive
 can not be used as variable or function names
 ex
cin >>
cout <<
auto
break
case
char
const
continue
double
float
for
if
int
long
default
register
else
return
short
signed
sizeof
static
do
struct
enum
switch
typedef
union
unsigned
void
volatile
extern
while
10
Input/Output Exercise
// Your name PHYSICALLY here
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "", address = "";
// declare ALL variables before you use them
// 1.) Create the CODE to LITERALLY display YOUR name, and address (No variables yet.)
// start below
2.) Create the CODE to ask for user’s first name and address, USE THE VARIABLES
// DECLARED FOR YOU ALREADY!! (hint: use cin>>)
// start below
// 3.) Create the code to display their name and address that THEY typed in. NOT yours!!
// start below
return 0;
}
11
Code Writing (like Hand Writing)
 nested blocks
o used for
 compound statements
 iteration (repeating loops of code)
 conditional (if (x < 10)…)
// ******************************
// Functions
// ******************************
void main( )
{
// variables
char user;
cout << “Will I do my work in Mr. Lupoli’s Class??”<<endl;
// have user press “Y” or “N”
cin >> user; // reads what user typed
if(user == ‘Y’) // will pass
{ cout << “Then I will pass C++, and take C++ AB next year!” << endl; }
// ONE LINE
if(user == ‘N’) // will fail
{
cout << “Then I will not pass Mr. Lupoli’s class.” << endl;
cout << “And my parents will be upset!” << endl;
// TWO LINES OR MORE
}
}
12
Version A
Version B
int main()
{
cout << "Hello C++ world" << endl;
int main()
{
cout << "Hello C++ world" << endl;
int age = -1;
int dogYears = -1;
cout << "Please enter a value: " << endl;
cin >> age;
dogYears = age * 7;
cout << "You are " << dogYears << " in Dog Years!!" <<
endl;
return 0;
}
int age = -1;
int dogYears = -1;
cout << "Please enter a value: " << endl;
cin >> age;
dogYears = age * 7;
cout << "You are " << dogYears << " in Dog Years!!" << endl;
return 0;
}
Version C
Version D
int main()
{
cout << "Hello C++ world" << endl;
int main()
{
cout << "Hello C++ world" << endl;
{
int age = -1;
int dogYears = -1;
int age = -1;
int dogYears = -1;
cout << "Please enter a value: " << endl;
cin >> age;
cout << "Please enter a value: " << endl;
cin >> age;
}
dogYears = age * 7;
dogYears = age * 7;
cout << "You are " << dogYears << " in Dog Years!!" << endl;
cout << "You are " << dogYears << " in Dog Years!!" << endl;
return 0;
}
return 0;
}
Versiob E
int main() { cout << "Hello C++ world" << endl;
int age = -1; int dogYears = -1; cout << "Please enter a value: " << endl;
cin >> age; ....
Remember, no one gets it right the 1st time
Some kinds of bugs...
 Syntax errors
 Simple bugs
 Logic errors
 Solving the wrong problem
13
FYI Section
Include Statements
 become a big problem for professors when they started using namespace
 namespace
o package of library files that Visual Studio uses
o are different than normal library files used in Borland or even VS 6.0
Differences in using Namespace
using namespace
#include <iostream> // for cin/cout
#include <string> // for strings variables
not using namespace (6.0 only??)
#include <iostream.h> // for cin/cout
#include <string.h> // for strings variables
using namespace std; // MUST HAVE
// rest of code below
// rest of code below
 Use chart below (also on web as Namespace Chart)
Common Library Files Used
using namespace
#include <assert.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
not using namespace
#include <assert.h>
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>
#include <istream.h>
#include <ostream.h>
#include <stdio.h>
#include <stdlib.h>
ONLY USE NAMESPACE!!!
#include <time.h>
#include <vector.h>
Non-Commonly Used Library Files
using namespace
#include <algorithm>
#include <deque>
#include <exception>
#include <limits>
#include <list>
#include <math.h>
#include <numeric>
not using namespace
#include <algorithm.h>
#include <deque.h>
#include <exception.h>
#include <limits.h>
#include <list.h>
#include <math.h>
#include <numeric.h>
14
#include <queue>
#include <sstream>
#include <stack>
#include <typeinfo>
#include <queue.h>
#include <sstream.h>
#include <stack.h>
#include <typeinfo.h>
15
Flowcharting
Symbol
Name
Flow line
Terminal
Input/Output
Processing
Decision
Connector
Predefined
Process/Function
Note/Annotation
Meaning
used to connect symbols
and indicate the flow of
logic
used to represent the
beginning (START) or end
(END) of a task
used for input and output
operations, such as reading
in from the keyboard (user
input) or outputting to the
screen
used for arithmetic, code,
and data manipulations.
Multiple lines can be
placed in one of these.
used for ANY logical
comparison like if/else, <,
>, &&, ||, !, etc…
if you have multiple pages,
place a number inside this
symbol so it can match
another with the same
number
used to represent a function
call, or variables are sent to
another function to return
data or an answer
used to add personal notes
to your project
 ALL IN MICROSOFT WORD!!!
o Drawing  Autoshapes  Flowchart
16
Example:
Create a flowchart that will ask for a person’s age. Then have the program double it,
and display their new age.
Age Program
int age;
int new age
// declare all variables
1. Ask for
user’s age
2. Get users age,
put into “age”
new age = 2 * age;
display “new age”
End of program
 Exercise
o create a flowchart to determine age in Dog years, when given user input
17
Example If-decisions flowchart setups
grade >
59
NO
if grade >
59
YES
Pass!!!
NO
Fail!!!
YES
NO
grade >
69
F
YES
grade >
79
YES
grade >
89
YES
grade >
99
D
NO
C
NO
B
NO
A
18
Download