Duke CPS 006 - Writing and Understanding C++

Unformatted text preview:

A Computer Science Tapestry2.1Writing and Understanding C++ z There are language independent skills in programming (C++, Java, …)z However, writing programs in any language requires understandingthe syntax and semantics of the programming language ¾ Syntax is similar to rules of spelling and grammar:• i before e except after c• The relationship between a command and a quote, “this is a fact,” or “this is a fact”,¾ Semantics is what a program (or English sentence) means• You ain’t nothing but a hound dog.• La chienne de ma tante est sur votre tete.z At first it seems like the syntax is hard to master, but the semantics are much harder¾ Natural languages are more forgiving than programming languages.A Computer Science Tapestry2.2Toward an Understanding of C++z Traditional first program, doesn’t convey power of computing but it illustrates basic components of a simple program#include <iostream>using namespace std;// traditional first programint main(){cout << "Hello world" << endl;return 0;}z This program must be edited/typed, compiled, linked and executed.z Other languages don’t use compile/link phase, examples?A Computer Science Tapestry2.3What’s a namespace?z In “standard” C++, objects and types are classified as to what namespace they’re in. Hierarchy is good.#include <iostream>// traditional first programint main(){std::cout << "Hello world" << std::endl;return 0;}z It’s much simpler to “use” a namespace, in small programs there won’t be any conflicts (and small is fairly big)A Computer Science Tapestry2.4Anatomy of a C++ Programz #include statements make libraries of classes and functions accessible to the program¾ Compiler needs access to interface, what the functions look like, but not to implementation• This is purpose of header files of source code¾ Linker/Loader needs access to implementations• This is purpose of Makefile/IDE libraries of object code¾ Helps programmers develop code independentlyz Comments make programs readable by humans¾ The cost of program maintenance is often far greater than the cost of program development¾ Use comments liberally, but make them meaningfulA Computer Science Tapestry2.5More C++ Anatomyz Programmer-defined functions¾ Functions are abstractions: help you to reuse ideas and code¾ The square root key on a calculator invokes a function¾ The chorus of a song is a similar abstraction¾ One word, e.g., “chorus”, takes the place of many or represents a conceptz A program is a collection of functions and classesz Programs may be implemented in more than one file, but there is only one main function¾ Execution of the program begins with main¾ The main function returns a value to the operating system or environmentA Computer Science Tapestry2.6Dennis Ritchiez Developed C and Unixz Shared 1983 Turing award and National Medal of Science in 1999“We wanted to preserve not just a good environment in which to do programming, but a system around which a fellowship could form”z Unix was¾ Free to Universities¾ Expensive originally¾ Linux precursor?A Computer Science Tapestry2.7Execution and Flow Controlz Execution of C++ programs is organized around statements¾ A statement executes, it may cause another statement to execute¾ Statements execute sequentially, or as governed by control that repeats a group of statements or selects one of several groups to execute• Control statements covered later; for now sequential flowz Syntax determines what’s in a statement, semantics determines construction of program from statementsz Output will be part of our programs¾ cout is the output stream, objects are placed on the stream¾ Objects are strings, numbers, many other typesA Computer Science Tapestry2.8Stream outputz cout is the standard output stream, use cerr for errors and other streams later. Accessible via #include<iostream>¾ Objects inserted onto stream with insertion operator <<¾ Different objects separated by insertion operator <<cout << "yadda yadda yadda" << endl;cout << " gross = " << 12*12 << endl;cout << 5 << " in. = " << 5*2.54 << " cm. " << endl;z String literals in quotes, other expressions are evaluated before being output.¾ endl is the “end of line” object (IO manipulator)¾ Can also output "\n" or "\t" or "\"" (escape sequences)A Computer Science Tapestry2.9More about streams and syntaxz C++ statements are terminated by a semi-coloncout << 3.14159*10*10 << " = area "<< " of circle with radius = "<< 10 << ", circ = " << 2*10*3.14159 << endl;z Thinking ahead:¾ Repetition of radius, problems?¾ Repetition of π, problems?¾ What’s better, several statements, or one long statement?¾ Evaluating expressions: rules of arithmetic?¾ Differences between 2*3 and 2*3.0 ?A Computer Science Tapestry2.10Functions: Abstractions, shortcutsz Can you write a program that prints “This rocks” 106times?¾ Only statements permitted are cout << and functions• If you know about loops, you have amnesia¾ Naïve solution requires one function and one million print statements, can we improve on this?z How do you attack this problem?¾ Think of a problem that’s similar, but simpler. Solve it.¾ Generalize solution to the more complex/bigger problemz How do functions help? Capture abstraction as a statement.¾ Hello(); vs cout << “Hello World” << endl;A Computer Science Tapestry2.11Toward Using Functions#include <iostream>using namespace std;int main(){cout << " |||||||||||||||| " << endl;cout << " | | " << endl;cout << " | o o | " << endl;cout << " _| |_ " << endl;cout << "|_ _|" << endl;cout << " | |______| | " << endl;cout << " | | " << endl; return 0;}z Prints head, but not as modular as program using functions¾ Harder to modify to draw differentlyA Computer Science Tapestry2.12Programmer-defined Functions#include <iostream>using namespace std;// functions appear hereint main(){Hair(); Sides();Eyes(); Ears(); Smile();Sides();return 0;}z What are advantages of this main over one in which several output statements appear in main.¾ New hair style? Stretched head?¾ Are these advantages?¾ How is width of head determined? Drawbacks? Solutions?A Computer Science Tapestry2.13Advantages of Functions#include <iostream>using namespace std;// functions appear hereint main(){Hair(); Sides();Eyes(); Ears(); Smile();Sides();return 0;}z What about eyeglasses? Mustache? Big nose? Frown?¾ Advantages in


View Full Document

Duke CPS 006 - Writing and Understanding C++

Download Writing and Understanding C++
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Writing and Understanding C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Writing and Understanding C++ 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?