Toward an Understanding of C++ Writing and Understanding C++ ● Writing programs in any language requires understanding the ● Traditional first program, doesn’t convey power of computing syntax and semantics of the programming language as well as but it illustrates basic components of a simple program language-independent skills in programming. #include <iostream> ➤ Syntax is similar to rules of spelling and grammar: using namespace std; • i before e except after c // traditional first program • The relationship between a command and a quote, “this is a fact,” or “this is a fact”, int main() ➤ Semantics is what a program (or English sentence) means { • You ain’t nothing but a hound dog. cout << " Hello world " << endl; • La plume de ma tante est sur la porte. return 0; } ● At first it seems like the syntax is hard to master, but the ● This program must be edited/typed, compiled, linked and semantics are much harder executed. ➤ Natural languages are more forgiving than programming ● Other languages don’t use compile/link phase, examples? languages. A Computer Science Tapestry 2.1 A Computer Science Tapestry 2.2 Anatomy of a C++ Program More C++ Anatomy ● #include statements make libraries of classes and functions ● Programmer-defined functions accessible to the program ➤ Functions are abstractions that help you to reuse ideas and code ➤ Compiler needs access to interface, what the functions ➤ The square root key on a calculator invokes a function look like, but not to implementation ➤ The chorus of a song is a similar abstraction ➤ Linker/Loader needs access to implementations ➤ One word, e.g., “chorus”, takes the place of many or ➤ Helps programmers develop code independently represents a concept ● A program is a collection of functions and classes ● Comments make programs readable by humans ● Programs may be implemented in more than one file, but there is only one main function ➤ The cost of program maintenance is often far greater than ➤ Execution of the program begins with main the cost of program development ➤ The main function returns a value to the operating system ➤ Use comments liberally, but make them meaningful or environment A Computer Science Tapestry 2.3 A Computer Science Tapestry 2.4
Dennis Ritchie Execution and Flow Control ● Developed C and Unix ● Execution of C++ programs is organized around statements Shared 1983 Turing award and National Medal of ● ➤ A statement executes, it may cause another statement to Science in 1999 execute ➤ Statements execute sequentially, or as governed by control “We wanted to preserve not just a good environment in that repeats a group of statements or selects one of several which to do programming, but a system around which a groups to execute fellowship could form” • Control statements covered later; for now sequential flow ● Syntax determines what’s in a statement, semantics ● Unix was determines construction of program from statements ➤ Free to Universities ➤ Expensive originally ● Output will be part of our programs ➤ cout is the output stream, objects are placed on the stream ➤ Linux precursor? ➤ Objects are strings, numbers, many other types A Computer Science Tapestry 2.5 A Computer Science Tapestry 2.6 Stream output More about streams and syntax ● cout is the standard output stream, use cerr for errors and ● C++ statements are terminated by a semi-colon other streams later. Accessible via #include<iostream> cout << 3.14159*10*10 << " = area " << " of circle with radius = " ➤ Objects inserted onto stream with insertion operator << << 10 << ", circ = " << 2*10*3.14159 ➤ Different objects separated by insertion operator << << endl; cout << "yadda yadda yadda" << endl; cout << " gross = " << 12*12 << endl; ● Thinking ahead: cout << 5 << " in. = " ➤ Repetition of radius, problems? << 5*2.54 << " cm. " << endl; ➤ Repetition of π , problems? ● String literals in quotes, other expressions are evaluated ➤ What’s better, several statements, or one long statement? before being output. ➤ Evaluating expressions: rules of arithmetic? ➤ endl is the “end of line” object (IO manipulator) ➤ Differences between 2*3 and 2*3.0 ? ➤ Can also output " \n " or " \t " or " \ "" (escape sequences) A Computer Science Tapestry 2.7 A Computer Science Tapestry 2.8
Toward Using Functions Programmer-defined Functions #include <iostream> #include <iostream> using namespace std; using namespace std; // functions appear here int main() { int main() cout << " |||||||||||||||| " << endl; { cout << " | | " << endl; Hair(); Sides(); cout << " | o o | " << endl; Eyes(); Ears(); Smile(); cout << " _| |_ " << endl; Sides(); cout << "|_ _|" << endl; return 0; cout << " | |______| | " << endl; } cout << " | | " << endl; ● What are advantages of this main over one in which several return 0; output statements appear in main. } ➤ New hair style? Stretched head? ● Prints head, but not as modular as program using functions ➤ Are these advantages? ➤ Harder to modify to draw differently ➤ How is width of head determined? Drawbacks? Solutions? A Computer Science Tapestry 2.9 A Computer Science Tapestry 2.10 Advantages of Functions Totem Functions |||||||||||||||| |||||||||||||||| | | | | | o o | | o o | _| |_ _| |_ |_ _| |_ _| #include <iostream> int main() | |______| | | |______| | using namespace std; { | | | | // functions appear here |||||||///////// |||||||///////// Head1(); | | | | Head2(); | --- --- | | --- --- | int main() |---|o|--|o|---| |---|o|--|o|---| Head3(); { | --- --- | | --- --- | return 0; Hair(); _| |_ _| 0 |_ |_ _| |_ _| Sides(); } | +------+ | | +------+ | Eyes(); Ears(); Smile(); | | | | | | | | What changed between the two ● Sides(); | | | | return 0; runs of the program? |||||||||||||||| |||||||||||||||| | | | | } Can you write Headxx() ? ● | o o | | o o | | | | o | ➤ Is Head1 a good name? ● What about eyeglasses? Mustache? Big nose? Frown? | o | |-+ ||||||| +-| | ||||||| | |-+ +-| ➤ Does Headxx call other ➤ Advantages in extending program rather than modifying _| |_ | |______| | functions? |_ _| | | program. | |______| | ➤ Suppose we used graphics | | ➤ Multiple heads (totem poles) instead of cout << ? A Computer Science Tapestry 2.11 A Computer Science Tapestry 2.12
Parameterized Functions Functions and Parameters (continued) #include <iostream> ● A square root function that only returns square root of 2 isn’t using namespace std; very useful void WinBigMoney(string name) ➤ F = sqrt(2), so 2 is a parameter/argument to the function { cout << "Hello " << name << " you may have " ➤ Useful parameter to head-drawing functions? << " won $1,000,000" << endl; cout << name << ", please call 1-900-IMN-IDIOT" • << endl; • } int main() ➤ What about happy birthday printing argument/parameter? { Hello( " owen " ); Hello( " susan " ); Hello( " bill gates " ); ● Functions have parameters, arguments are passed to functions return 0; } Birthday( " Fred " ); // sing to fred ● Parameter list provides type and name of parameter Birthday( " Ethel " ); // sing to ethel ➤ Argument type must match parameter type ➤ Function’s prototype based on types only, not names A Computer Science Tapestry 2.13 A Computer Science Tapestry 2.14 Parameterized Functions for Songs Calling Functions: where, when, how? ● On his farm Old MacDonald had a X that that says Y ● Some functions are imported from libraries ➤ Function prototypes specified in header files, ➤ pig, oink implementations linked later ➤ cow, moo ➤ Compiler “sees” prototype before client code calls function void Verse( ); ● Some functions are in the same file in which they’re called ● Five bottles of Z on a wall, five bottles of Z ➤ Function declaration is the prototype only ➤ cola ➤ Function definition includes the implementation ➤ lemonade void Verse(string name); void Verse(string name) void Verse( ); { cout << " hi " << name << endl; ● Mama’s going to buy you a X , and if that X Y } ➤ Mocking bird, don’t sing ● Declaration or definition must appear before call ➤ Looking glass, get’s broke ➤ Ok to put declaration before, definition after call void Verse( ); ➤ Ok to put main last, all definitions first (problems?) A Computer Science Tapestry 2.15 A Computer Science Tapestry 2.16
Recommend
More recommend