{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C++ - CS106B Autumn 2010 Handout 02 Getting Started January...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106B Handout 02 Autumn 2010 January 4 th , 2010 Getting Started Handout written by Julie Zelenski, Mehran Sahami, and Robert Plummer. After today’s lecture, you should run home and read all of Chapters 1 and 2 on your own. We won't teach the basic syntax and constructs. We’ll just highlight some of the common programming idioms and C++ idiosyncrasies. This handout contains a smattering of facts about the C++ language, mentions some of the basics, and points out a few things to avoid. Most programs have the same component sections —A comment explaining what the program does This is a good programming practice that makes your code more accessible to the reader (who might be you!) —Library inclusions These #include statements let the compiler know what libraries you want to use functions from. Many standard libraries come with all C++ compilers. You will also use libraries you write yourself, or in this course, libraries that we provide such as genlib or simpio . —Constant definitions By using the #define mechanism to define constants, you make your program more readable and avoid "magic numbers". You also make it easy to change the value of the constant. —Function prototypes One common style is to write the code for functions after the main program. This means that functions are often called before the code appears. Function prototypes let the compiler compile the calls correctly and make sure they are consistent with the function code. —Main program The main program is actually a function called main . A prototype for this function is not required. Execution of your program begins with the first line of the main function. The function main takes no arguments and returns an int (as shown below); typically we return 0 to indicate the program exited cleanly. —Function definitions All programs worth writing involve breaking the code into functions, so that no part of the program manages too large a detail. Good decomposition leads to code that is clear, logical, and easy to understand. The first assignment, in fact, is an exercise in decomposition, and the A program will make it a point to write functions that can be called from two or more different places.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Variables must be declared before they’re used Variables are local to the function in which they are declared. We will never use global variable, and you won’t either. In C++, local variables can be declared anywhere with a block of statements. The scope of a local variable extends to the end of the enclosing block.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern