chap02 - Chapter 2 Chapter The The Fundamentals of C of Key Concepts Key function main include comments definitions simple input and output integer

chap02 - Chapter 2 Chapter The The Fundamentals of C of Key...

This preview shows page 1 - 11 out of 38 pages.

Chapter 2 : Chapter 2 : The The Fundamentals Fundamentals of C++ of C++
Image of page 1
Key Concepts: Key Concepts: function main() include comments definitions simple input and output integer, floating-point, and characters types integer, floating-point, and characters literals C++ names declarations expressions usual unary conversions usual binary conversions operator precedence operator associativity iostream insertion and extraction
Image of page 2
Program Organization Program Organization Program statement Definition Declaration Action Executable unit Named set of program statements Different languages refer to executable units by different names Fortran and Basic : Subroutine Pascal : Procedure C++ : Function
Image of page 3
Program Organization Program Organization C++ program Collection of definitions, declarations and functions Collection can span multiple files physically A files containing C++ code is called a translation unit Advantages Structured into small understandable units Complexity is reduced Overall program size decreases
Image of page 4
A First Program A First Program Hello world! Hello world! // Program: Display greetings // Author(s): Ima Programmer // Date: 1/24/2001 #include <iostream> #include <string> using namespace std; int main() { cout << "Hello world!" << endl; return 0; } Preprocessor directives Insertion statement Ends executions of main() which ends program Comments Function Function named main() indicates start of program Provides simple access
Image of page 5
A Second Program A Second Program Area.cpp Area.cpp Definitions Extraction Definition with initialization Insertion #include <iostream> using namespace std; int main() { // Extract length and width cout << "Rectangle dimensions: "; float Length; float Width; cin >> Length >> Width; // Compute and insert the area float Area = Length * Width; cout << "Area = " << Area << " = Length " << Length << " * Width " << Width << endl; return 0; }
Image of page 6
Comments Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood so that they can be maintained C++ has two conventions for comments // single line comment (preferred) /* long comment */ (save for debugging) Typical uses Identify program and who wrote it Record when program was written Add descriptions of modifications A program should at least include 20% comments.
Image of page 7
Object Object Object is a representation of some information Name Values or properties Data members Ability to react to requests (messages)!! Member functions When an object receives a message, one of two actions are performed Object is directed to perform an action Object changes one of its properties In C++ we usually say that a definition of the built-in data type (integer, float, double etc.) is object object , not variable.
Image of page 8
Fundamental C++ Objects Fundamental C++ Objects built built - in object types in object types C++ has a large number of fundamental or built-in object types The fundamental object types fall into one of three categories Integer objects Floating-point objects Character objects Z P 3.14 1 5 1.28345
Image of page 9
Integer Object Types Integer Object Types The basic integer object type is int int An int int
Image of page 10
Image of page 11

You've reached the end of your free preview.

Want to read all 38 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes