welcome to cs 106l! - stanford university · welcome to cs 106l! ali malik [email protected]....
TRANSCRIPT
![Page 2: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/2.jpg)
Welcome
Why CS106L?
Logistics
History and Philosophy of C++
C++ Basics
Game Plan
![Page 3: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/3.jpg)
Welcome!
![Page 5: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/5.jpg)
Email me whenever you have questions or even if you just want to talk. I’d love to hear from all of you!
Ali Malik [email protected]
Instructor
Tell me about the things you are working on, or anything you find interesting.
![Page 6: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/6.jpg)
You
![Page 7: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/7.jpg)
You
Symbolic Systems
Biology
Economics
Electrical Eng.
Mathematics
Material Science
Music
Education
Mechanical Eng.
Undeclared
Aero/Astro
StatisticsMS&E
Energy Resource Eng.
Physics
MCSCivil and
Environmental Eng.Petroleum Eng.
MBA
Computer Science
![Page 8: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/8.jpg)
Why CS106L?
![Page 9: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/9.jpg)
CS106B/X:
● Focus is on teaching concepts like abstractions, recursion, pointers etc.
● Only teaches you enough C++ to practice these concepts.
CS106L:
● Learn how to write powerful and elegant code.● Write actual C++ - no Stanford libraries!● Understand the design decisions that lead to “good” code
What is CS106L?
![Page 10: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/10.jpg)
Good C++ Code
#include <iostream>
int main() { std::cout << "Hello, world!" << std::endl; return 0;}
![Page 11: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/11.jpg)
“Good” C++ Code
#include "stdio.h"#include "stdlib.h"
int main(int argc, char *argv) { printf("%s", "Hello, world!\n"); return EXIT_SUCCESS;}
![Page 12: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/12.jpg)
“Good” C++ Code#include "stdio.h"#include "stdlib.h"
int main(int argc, char *argv) { asm( "sub $0x20,%rsp\n\t" "movabs $0x77202c6f6c6c6548,%rax\n\t" "mov %rax,(%rsp)\n\t" "movl $0x646c726f, 0x8(%rsp)\n\t" "movw $0x21, 0xc(%rsp)\n\t" "movb $0x0,0xd(%rsp)\n\t" "leaq (%rsp),%rax\n\t" "mov %rax,%rdi\n\t" "call __Z6myputsPc\n\t" "add $0x20, %rsp\n\t" ); return EXIT_SUCCESS;}
![Page 13: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/13.jpg)
“Good” Terrible C++ Code#include "stdio.h"#include "stdlib.h"
int main(int argc, char *argv) { asm( "sub $0x20,%rsp\n\t" "movabs $0x77202c6f6c6c6548,%rax\n\t" "mov %rax,(%rsp)\n\t" "movl $0x646c726f, 0x8(%rsp)\n\t" "movw $0x21, 0xc(%rsp)\n\t" "movb $0x0,0xd(%rsp)\n\t" "leaq (%rsp),%rax\n\t" "mov %rax,%rdi\n\t" "call __Z6myputsPc\n\t" "add $0x20, %rsp\n\t" ); return EXIT_SUCCESS;}
![Page 14: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/14.jpg)
Why Learn C++?
![Page 15: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/15.jpg)
Why C++: Popularity
TIOBE Index, March 2017
![Page 16: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/16.jpg)
Why C++: Popularity
Most loved languages - StackOverflow 2015
![Page 17: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/17.jpg)
Why C++: Users (companies)
![Page 18: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/18.jpg)
Why C++: Users (browsers)
![Page 19: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/19.jpg)
Why C++: Users (software)
![Page 20: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/20.jpg)
Why C++: Users (games)
![Page 21: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/21.jpg)
Why C++: Users (cool things)
The Spirit rover was operational for over 6 years when the mission was only planned to run for around 3 months
The F-35 Lightning II (Joint Strike Fighter) relies extensively on C++
![Page 22: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/22.jpg)
Logistics
![Page 23: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/23.jpg)
Lecture T/Th 1:30 - 2:20pm in STLC-111
Website cs106l.stanford.edu
Office Hours TBA. Email if any help needed till then
Assignments 3 in total - you need to do at least 2 to pass
Late Days Three 24-hr late days (max two per assignment)
Development We will be using QT Creator. Get this set up quickly!
Honor Code Don’t cheat, repercussions are severe
Logistics
![Page 24: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/24.jpg)
Lecture T/Th 1:30 - 2:20pm in STLC-111
Website cs106l.stanford.edu
Office Hours TBA. Email if any help needed till then
Assignments 3 in total - you need to do at least 2 to pass
Late Days Three 24-hr late days (max two per assignment)
Development We will be using QT Creator. Get this set up quickly!
Honor Code Don’t cheat, repercussions are severe
LogisticsQT Help hours this Wed 7-9pm
![Page 25: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/25.jpg)
What is C++?
![Page 26: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/26.jpg)
C++ History
“If you wish to make an apple pie from scratch, you must first invent the universe”
- Carl Sagan
![Page 27: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/27.jpg)
C++ History
“If you wish to understand C++ from scratch, you must first invent the universe”
- (not) Carl Sagan
![Page 28: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/28.jpg)
C++ History: Assembly
section .textglobal _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel
section .datamsg db 'Hello, world!',0xa ;our dear stringlen equ $ - msg ;length of our dear string
![Page 29: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/29.jpg)
Unbelievably simple instructions (move bits around, add, subtract).
Well written assembly is extremely fast.
Gives you complete control over your program.
C++ History: Assembly
Why don’t we always use assembly?
![Page 30: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/30.jpg)
C++ History: Assembly
section .textglobal _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel
section .datamsg db 'Hello, world!',0xa ;our dear stringlen equ $ - msg ;length of our dear string
![Page 31: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/31.jpg)
C++ History: Assembly
Requires lots of code to do simple tasks.
Hard to understand other people’s code
Extremely unportable
![Page 32: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/32.jpg)
Writing assembly was too difficult but computers only understood assembly.
C++ History: Moving Forward
Idea:
● Source code can be written in a more intuitive language ● An additional program can convert it into assembly
This is called a compiler!
![Page 33: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/33.jpg)
K&R created C in 1972, to much praise.
C made it easy to write code that was
● Fast● Simple● Cross-platform
Learn to love it in CS107!
C++ History: Invention of C
Ken Thompson and Dennis Ritchie, creators of the C language.
![Page 34: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/34.jpg)
C was popular since it was simple.
This was also its weakness:
● No objects or classes (think cmap/cvec)● Difficult to write code that worked generically● Tedious when writing large programs
C++ History: Invention of C
![Page 35: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/35.jpg)
In 1983, the first vestiges of C++ were created by Bjarne Stroustrup.
C++ History: Bjarne Stroustrup
He wanted a language that was:
● Fast● Simple to Use● Cross-platform● Had high level features
![Page 36: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/36.jpg)
In 1983, the first vestiges of C++ were created by Bjarne Stroustrup.
C++ History: Bjarne Stroustrup
He wanted a language that was:
● Fast● Simple to Use● Cross-platform● Had high level features
![Page 37: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/37.jpg)
In 1983, the first vestiges of C++ were created by Bjarne Stroustrup.
C++ History: Bjarne Stroustrup
He wanted a language that was:
● Fast● Simple to Use● Cross-platform● Had high level features
![Page 38: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/38.jpg)
C++ History: Evolution of C++
C with Classes
C++
C++98
C++03
C++11
C++14
C++17
1979 1983 1998 2003 2011 2014 2017
![Page 39: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/39.jpg)
C++ History: Evolution of C++
C with Classes
C++
C++98
C++03
C++11
C++14
C++17
1979 1983 1998 2003 2011 2014 2017
You are here
![Page 40: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/40.jpg)
Only add features if they solve an actual problem
Programmers should be free to choose their own style
Compartmentalization is key
Allow the programmer full control if they want it
Don’t sacrifice performance except as a last resort
Enforce safety at compile time whenever possible
The C++ Philosophy
![Page 41: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/41.jpg)
Only add features if they solve an actual problem
Programmers should be free to choose their own style
Compartmentalization is key
Allow the programmer full control if they want it
Don’t sacrifice performance except as a last resort
Enforce safety at compile time whenever possible
The C++ Philosophy
![Page 42: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/42.jpg)
Only add features if they solve an actual problem
Programmers should be free to choose their own style
Compartmentalization is key
Allow the programmer full control if they want it
Don’t sacrifice performance except as a last resort
Enforce safety at compile time whenever possible
The C++ Philosophy
![Page 43: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/43.jpg)
Only add features if they solve an actual problem
Programmers should be free to choose their own style
Compartmentalization is key
Allow the programmer full control if they want it
Don’t sacrifice performance except as a last resort
Enforce safety at compile time whenever possible
The C++ Philosophy
![Page 44: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/44.jpg)
Our first C++ Program
![Page 45: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/45.jpg)
hello.cpp
#include <iostream>
int main() { std::cout << "Hello, world!" << std::endl; return 0;}
![Page 46: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/46.jpg)
Next TimeStreams
![Page 47: Welcome to CS 106L! - Stanford University · Welcome to CS 106L! Ali Malik malikali@stanford.edu. Welcome Why CS106L? Logistics ... Focus is on teaching concepts like abstractions,](https://reader033.vdocuments.net/reader033/viewer/2022050210/5f5d3852281bcc5c2b2d8788/html5/thumbnails/47.jpg)