Fundamentals of Programming –
C++
Lecture 1: Introduction to Programming
Expected Background
■ “A one-semester college course in
programming.”
■ I assume you can write a program in some
language, understand variables, control structures, functions/subroutines.
■ Has background on C or C++. ■ If in doubt, let’s talk.
Course Outline
■ Week 1: Background, basics of C++, first
program, programming environments
■ Week 2: Raw materials: types, variables,
operators, program control
■ Week 3: Classes: declarations, constructors,
cleanup & garbage collection
■ Week 4: Packages, access specifiers, finals,
Course Outline (cont.)
■ Week 5: Polymorphism, abstract classes,
design patterns
■ Week 6: Interfaces & extends, inner
classes, callbacks via inner classes
■ Week 7: Arrays, container classes, iterators ■ Week 8: More on collections, collections of
Course Outline (cont.)
■ Week 9: Exception handling ■ Week 10: I/O
■ Week 11: Applications, ■ Week 12: Database
Administrative Details
■ Professor: Christine W. Pitos ■ Office: IT Office
■ “Business hours”: 7:30-11:30 & 1:00-5:00 ■ Email: [email protected]
■ Course Web sites
Administrative Details (cont.)
■ 12 weeks
■ Two consecutive 80-minute lectures per
week, a break in between the lectures.
■ Course notes in PowerPoint or HTML,
attached to the syllabus.
■ Course notes available (usually) Sunday
before class.
■ Send course questions to Blackboard,
Administrative Details (cont.)
■ Everything is attached to the syllabus.
Don’t look for assignments, etc. Look at the syllabus!
■ Homework usually weekly.
■ Submission instructions with each
assignment, usually printed listings and a softcopy.
■ Printing slides? Three to a page, at least.
Administrative Details (cont.)
■ Attendance is not required, but…
■ …you are responsible for everything said in
class.
■ I encourage you to ask questions in class. ■ This is Second Level Prograamming…you
are supposed to ask questions. Don’t guess, ask a question!
My Policy on Cheating
■ Cheating means “submitting, without proper
attribution, any computer code that is directly traceable to the computer code written by another person.”
■ I give students a failing course grade for
any cheating. Expulsion is also possible.
My Policy on Cheating
■ You may discuss homework problems with
classmates (although it is not to your advantage to do so).
■ You can use ideas from the literature (with
proper citation).
■ You can use anything from the
textbook/notes.
■ The code you submit must be written
Even More On Cheating
■ “In addition to any penalties imposed by the
instructor, including failing the course, all cheating and plagiarism infractions will be
reported in writing to the Associate Dean for the program, the Associate Dean of Faculty, the Dean of Student Affairs, and the Dean. They will
review and determine if expulsion should be
recommended. The report will become part of the student’s permanent record.”
Course Etiquette
■ Etiquette is “conduct in polite society” ■ No cell phones
■ No random comings and goings ■ If you are sleepy, go home
■ If you want to read email or surf the Web,
Activity
■ Group your self by row
■ Discuss in the group your plans in studying
computer programming by answering the ff. in 5 minutes:
– What is your team name?
– What programs would you like build/develop? – Why would you develop the said program?
■ Select a representative who will present
Programming Language Evolution
■ Machine language
■ Assembler
■ “3rd generation” (COBOL, FORTRAN, C)
■ "object-oriented" languages: C++
■ "scripting", Web, component-based, ...:
■ Java, Perl, Python, Visual Basic, Javascript, ■ Specialized (Lisp, Prolog, APL)
■ “4th generation” (SQL, GAMS, spreadsheets,
Mathematica)
Why So Many Languages?
■ Bring the language “closer” to the problem. ■ But 4GLs are typically focused on
specialized domains (e.g., relational databases).
■ We want a language that is general purpose,
22
Computer Languages
■ High-level languages
» Similar to everyday English, use common mathematical notations
» Single statements accomplish substantial tasks
■ Assembly language requires many instructions to accomplish simple tasks
» Translator programs (compilers)
■ Convert to machine language
» Interpreter programs
■ Directly execute high-level language programs
» Example:
23
History of C and C++
■ History of C
– Evolved from two other programming languages
» BCPL and B
■ “Typeless” languages
– Dennis Ritchie (Bell Laboratories)
» Added data typing, other features
– Development language of UNIX – Hardware independent
» Portable programs
– 1989: ANSI standard
– 1990: ANSI and ISO standard published
24
History of C and C++
■ History of C++
– Extension of C
– Early 1980s: Bjarne Stroustrup (Bell Laboratories)
– Provides capabilities for object-oriented programming
» Objects: reusable software components
■ Model items in real world
» Object-oriented programs
■ Easy to understand, correct and modify
– Hybrid language
» C-like style
» Object-oriented style » Both
25
C++ Standard Library
■ C++ programs
– Built from pieces called classes and functions
■ C++ standard library
– Rich collections of existing classes and functions
■ “Building block approach” to creating
programs
26
Java
■ Java – 1991: Sun Microsystems » Green project – 1995: Sun Microsystems» Formally announced Java at trade show
– Web pages with dynamic and interactive content – Develop large-scale enterprise applications
– Enhance functionality of web servers
– Provide applications for consumer devices
27
Structured Programming
■ Structured programming (1960s)
– Disciplined approach to writing programs
– Clear, easy to test and debug, and easy to modify
■ Pascal
– 1971: Niklaus Wirth
■ Ada
– 1970s - early 1980s: US Department of Defense (DoD) – Multitasking
28
The Key Software Trend: Object
Technology
■ Objects
– Reusable software components that model real world items – Meaningful software units
» Date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects, etc.
» Any noun can be represented as an object
– More understandable, better organized and easier to maintain than procedural programming
– Favor modularity
» Software reuse
■ Libraries
– MFC (Microsoft Foundation Classes) – Rogue Wave
29
Basics of a Typical C++
Environment
■ C++ systems – Program-development environment – Language – C++ Standard Library■ C++ program names extensions
– .cpp – .cxx – .cc – .C
30
Basics of a Typical C++ Environment
Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute Loader Primary Memory Program is created in the editor and stored on disk.
Preprocessor program processes the code.
Loader puts program in memory.
CPU takes each instruction and executes it, possibly storing new data values as the program executes.
Compiler
Compiler creates object code and stores it on disk.
Linker links the object code with the libraries, creates an executable file and stores it on disk
Editor Preprocessor Linker CPU Primary Memory . . . . . . . . . . . . Disk Disk Disk Disk Disk
31
Basics of a Typical C++
Environment
■ Common Input/output functions – cin
» Standard input stream » Normally keyboard
– cout
» Standard output stream » Normally computer screen
– cerr
» Standard error stream » Display error messages
32
A Simple Program: Printing a
Line of Text
■ Before writing the programs
– Comments
» Document programs
» Improve program readability » Ignored by compiler
» Single-line comment
■ Use C’s comment /* .. */ OR Begin with // or
– Preprocessor directives
» Processed by preprocessor before compiling » Begin with #
© 2003 Prentice Hall, Inc. All rights reserved.
Outline 33 fig01_02.cpp (1 of 1) fig01_02.cpp output (1 of 1) 1 // Fig. 1.2: fig01_02.cpp 2 // A first program in C++. 3 #include <iostream.h> 4
5 // function main begins program execution
6 int main()
7 {
8 cout << "Welcome to C++!\n";
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main
Welcome to C++!
Single-line comments. Preprocessor directive to
include input/output stream header file <iostream>. Function main appears
exactly once in every C++ program..
Function main returns an integer value.Left brace { begins function
body.
Corresponding right brace } ends function body.
Statements end with a semicolon ;.
Name cout belongs to n iostream.
Stream insertion operator. Keyword return is one of several means to exit function; value 0 indicates program terminated successfully.
34
A Simple Program: Printing a
Line of Text
■ Standard output stream object – cout
– “Connected” to screen
– <<
» Stream insertion operator
» Value to right (right operand) inserted into output stream
■ Escape characters – \
35
A Simple Program: Printing a
Line of Text
Assignment
Write a C++ program that displays the following shape: