Introduction to C and C++ Programming Monday, March 6, 1995 11 AM -- 5 PM Eastern Time Dr. H. E. Dunsmore Department of Computer Science Purdue University West Lafayette, Indiana Copyright 1995, H.E. Dunsmore, Purdue University CLASS SCHEDULE 11:00-12:00 (1) Introduction to C and C++ 12:00-12:30 BREAK 12:30- 1:25 (2) Data Types and Program Structure 1:25- 1:30 BREAK 1:30- 2:30 (3) Standard Operations and Functions 2:30- 3:00 BREAK 3:00- 3:55 (4) If (Decision) Statements 3:55- 4:00 BREAK 4:00- 5:00 (5) Loops INTRODUCTION TO C AND C++ The C and C++ Languages Comments iostream.h Output and Input THE C AND C++ LANGUAGES Machine language String of zeroes and ones Understandable and executable by computer No translation necessary 001010110010 Extremely difficult for human programmer to make sense of such statements Assembly language ADD X,Y Programming languages for (counter = 0; counter <= NUM; ++counter) Programming language evolution Motivation to produce languages that lead to greater productivity and reduction in cost of software development C and C++ becoming languages of choice for many software developers Ease of use and powerful capabilities C++ is ``better'' than C Name C++ was derived from language name C and ++ (increment) operator indicating that C++ is ``one language better than C'' C++ adds extensions that improve language's syntax Easier to use Support data abstraction and object-oriented programming Object-oriented approach helps software developers conceptualize and build complex software systems while managing software complexity C++ is approximate superset of C Most of constructs of C are included in C++ C++ was developed by Bjarne Stroustrup of AT&T C++ has been available outside AT&T since approximately 1985 Several C++ features have been adopted as part of ANSI standard C language Function prototypes, ``void'' type, and const type specifier COMMENTS Standard C comment format: /* This is a typical C comment */ C++ introduced new format: // This is a C++ style comment On any line, after // is encountered, everything to end of line is comment // This program uses function minute_convert to // calculate the minutes in a certain number of // days and hours #include int minute_convert (int, float); void main () { // Output the number of minutes in // 3 days and 6.5 hours cout << minute_convert (3, 6.5) << endl; } // Function to convert days and hours to minutes int minute_convert (int days, float hours) { int days_minutes = days * 24 * 60; int part_day = int (hours * 60); // truncates here return (days_minutes + part_day); } Entire lines and right sides of lines (following //) serve as comments C style comment is still useful for commenting large blocks of text: /* This is a multi-line comment that could go on and on forever */ iostream.h iostream.h is standard C++ #include file for input/output Most C++ programs begin with the statement #include Directs C++ compiler to include code from file iostream.h while compiling program OUTPUT AND INPUT Standard output in C++ is accomplished via cout << stuff-to-output; cout is object that directs output to standard output device (usually screen) << is output operator cout << "Please enter your age"; To signify end-of-line: cout << "Please enter your age" << " n"; cout << "Please enter your age" << endl; cout is able to determine variable's type (such as integer or float) and outputs it appropriately cout << "Gino is now age " << how_old << endl; If how_old is integer variable with value 30... Gino is now age 30 Printing multiple items on one line: cout << "There are "; cout << number; cout << " people with an average weight of "; cout << weight; cout << endl; There are 30 people with an average weight of 129.5 cout << "There are " << number << " people with an average weight of "; cout << weight << endl; Standard input in C++ is accomplished via cin >> variable; cin is object that maps to standard input device (usually keyboard) >> is input operator C++ compiler determines variable's type and inputs and stores value appropriately cin >> number; Consecutive cin statements: cin >> number; cin >> weight; Can also be accomplished via multiple occurrences of input operator: cin >> number >> weight; char grade; cin >> grade; Gets next non-whitespace character char name[30]; cin >> name; Input character string from standard input device and store it in variable name String is terminated by end-of-line or any whitespace cerr object can be used to write to standard error device and is used much like cout : cerr << error-stuff-to-output << endl; Allows error (or informational) messages to be sent to console even if standard output device ( cout ) has been re-directed elsewhere