1 c++ programming basics chapter 1 lecture csis 10a
TRANSCRIPT
![Page 1: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/1.jpg)
1
C++ Programming BasicsChapter 1 Lecture
CSIS 10A
![Page 2: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/2.jpg)
2
Agenda
Hardware Basics The IDE
My First Program
Its all G(r)eek to me
Variables and Declarations
Input and Output
![Page 3: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/3.jpg)
3
Anatomy of a Computer
Memory Output
ALU Control Input
MouseKeyboardScanner
Hard DiskFloppy Disk
MonitorPrinterSpeakers
![Page 4: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/4.jpg)
4
The CPU uses machine languageAssembly instructions to calculate the radius of a
circle:
LOAD radiusLOAD piMULTIPLYLOAD twoMULTIPLYSTORE circle
![Page 5: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/5.jpg)
5
Compilers to the Rescue !
High level languages like C++ allows writing code that is easier to understand and universally works on any CPU.
circle=2.0*radius*pi;
The compiler is what translates instructions from C++ into Machine language.
![Page 6: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/6.jpg)
6
Agenda
Hardware Basics
The IDE My First Program
Its all G(r)eek to me
Variables and Declarations
Input and Output
![Page 7: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/7.jpg)
7
THE MECHANICS OF WRITING A PROGRAM
1. Editing -- Writing a program 2. Compiling -- Translating from C++ into machine language 3. Linking -- Combining your program with other libraries4. Running – Letting the computer execute a program5. Debugging – Running step by step through a program
searching for mistakes
The Integrated Development Environment (IDE) combines 1-5.
![Page 8: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/8.jpg)
8
Agenda
Hardware Basics
The IDE
My First Program
Its all G(r)eek to me
Variables and Declarations
Input and Output
![Page 9: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/9.jpg)
9
Hello World !
Lets start off the traditional way
Program that prints out “Hello World” on your output console (your screen)
Lets start off on our journey…..
![Page 10: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/10.jpg)
10
Steps
1. Create new source file
2. Write the code
3. Create a workspace (only in MSVC++)
4. Compile
5. Link
6. Execute
![Page 11: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/11.jpg)
11
Agenda
Hardware Basics
The IDE
My First Program
Its all G(r)eek to me
Variables and Declarations
Input and Output
![Page 12: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/12.jpg)
12
It’s all G(r)eek to me
… actually, its C++
Lets dissect and analyze a simple program
![Page 13: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/13.jpg)
13
20,000 ft. above sea level
#include <iostream>using namespace std;
int main(){// This is my first program cout << "Hello World" << endl; system("pause"); return 0;}
![Page 14: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/14.jpg)
14
20,000 leagues under the sea
#include <iostream>
Actually includes some information into your code
Contains some definitions that are needed for your code
More of this later on in the course
![Page 15: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/15.jpg)
15
22,000 leagues under the sea
using namespace std;
This line refers to a set of standard object name definitions
For now, this is “boilerplate”—stick it in cause it makes everything work!!
![Page 16: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/16.jpg)
16
… deeper….
int main()
• This is the piece of code (function) that is operated on first when a program is executed
• What’s a function ???
• …. all that’s coming soon …
![Page 17: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/17.jpg)
17
… even deeper….
//This is my first program
• This is a comment you write to yourself
• Useful when writing large programs
• Starts with a //
![Page 18: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/18.jpg)
18
… and deeper…..
Output operator
cout << “Hi”<<endl;
cout is the console monitor (your display)
<< is the output operator. Use to chain together your output message.
“Hi”is a string literal
endl means end-line (like enter key)
![Page 19: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/19.jpg)
19
… getting sleepy?…..
For the Bloodshed environment…Hold the display open so you can read it (pressing a key will continue) system("pause");
Finish up this program, return a 0 to operating system (everything ended OK)
return 0;
![Page 20: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/20.jpg)
20
… and even deeper ….
{} delineates the code block
Each line ends with a ;
![Page 21: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/21.jpg)
21
Other types of cout statement
cout<< “ my age is” <<endl << 39<<endl;
cout<< “ my age is 39”;
cout<< “ what’s your’s”;
Escape with \
Output a “ cout<<“ \” ” <<endl;
Output a endl cout<<“\n”;
![Page 22: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/22.jpg)
22
Tom’s All Purpose Program Shell#include <iostream>using namespace std;
int main(){// Your code here ...
system("pause"); return 0;}
Future slides may neglect some of the above components for clarity
![Page 23: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/23.jpg)
23
You Do ItModify your hello world program to print your name, address and phone number (COULD BE FAKE!) on three lines:
Tom Rebold1600 Pennsylvania Ave123-456-7890
Experiment with line breaks
![Page 24: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/24.jpg)
24
Lets get moving…..
We’ll learn as we proceed
You’ll be saying “Aha!” or “Oho!” when you hear these terms again
Let’s play around a bit more
![Page 25: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/25.jpg)
25
What more ?
Variables and Declarations
Getting User Input
![Page 26: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/26.jpg)
26
Variables and Declarations
Variables represent storage locations in the computer’s memory
variable = expression
Assignment is from right to leftn = 5;
Would give n the value 5
![Page 27: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/27.jpg)
27
Using int Variables
int main()
{
int m,n;
m = 44;
cout << “m = “ << m;
n = m + 33;
cout << “ and n = “ << n << endl;
}
![Page 28: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/28.jpg)
28
Clearing the haze
int m;
m
int
m = 44.0; 44
![Page 29: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/29.jpg)
29
Clearing the haze
int m = 44, n;
44 + 33 = 77
intn
m
int
44
77
n = m + 33;
![Page 30: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/30.jpg)
30
Want more ?
Variables and Declarations
Getting User Input
![Page 31: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/31.jpg)
31
User Input with cin
How shall I feed in data ?
Remember cout ?
Meet cin
cin >> m;
Will put the value entered through the console (keyboard) into m
![Page 32: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/32.jpg)
32
User Input with cin
int main(){cout << "Enter the value of m:";cin >> m;cout << "m = " << m << endl;
}
![Page 33: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/33.jpg)
33
User Input with cin
int main(){cout << “Enter the value of m:”;cin >> m;cout << “m = “ << m << endl;
}
HEY THERE’S SOMETHING WRONG
HERE !!!
![Page 34: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/34.jpg)
34
User Input with cinint main(){int m;cout << "Enter the value of m:";cin >> m;cout << "m = " << m << endl;
}
![Page 35: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/35.jpg)
35
Your turn ()
1. from hello.cpp File>SaveAs age.cpp
2. Delete all the cout statements. Add lines to:
a) Declare a variable called age
b) Display a message asking for data (such as “tell me your age”)c) read the data into age d) display the variable age with a descriptive message. (refer to slide 34)
![Page 36: 1 C++ Programming Basics Chapter 1 Lecture CSIS 10A](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f1b5503460f94c30d1d/html5/thumbnails/36.jpg)
36
That’s a wrap !
What we learned so far:How to write a basic C++ program
The structure of a program
Displaying to the console (cout)
Variables
Getting user input via the console (cin)