Chapter 2 Introduction to C Department of Computer




























- Slides: 28

Chapter 2 Introduction to C++ Department of Computer Science Missouri State University

Outline o The structure of C++ programs o Cout Objects o The #include directive o Variables and constants o Data Types o Operations o Comments o Programming Style

An example // This program calculates a pay check. #include<iostream> using namespace std; void main( ) { int worker. Pay. Rate; float work. Hours, pay; worker. Pay. Rate=12; work. Hours=10. 5; pay=work. Hours worker. Pay. Rate; cout<<“A worker earns $”<<pay<<endl; }

Comments o Used to document parts of the program o Intended for persons reading the source code of the program: n n n Indicate the purpose of the program Describe the use of variables Explain complex sections of code o Are ignored by the compiler

Comments My: David: // all students grades statistics // sophomore grades statistics void main( ) { } } Linda: Kevins: // freshman grades statistics // junior grades statistics void main( ) { } }

C++ Style Comments Begin with // through to the end of line: int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // calculate rectangle area = length * width;

C-Style Comments o Begin with /*, end with */ o Can span multiple lines: /* this is a multi-line C-style comment */ o Can be used like C++ style comments: int area; /* calculated area */

An example // This program calculates a pay check. #include<iostream> using namespace std; void main( ) { int worker. Pay. Rate; float work. Hours, pay; worker. Pay. Rate=12; work. Hours=10. 5; pay=work. Hours worker. Pay. Rate; cout<<“A worker earns $”<<pay<<endl; }

Preprocessor Directive Part o # include <a. File. Name>

An example // This program calculates a pay check. #include<iostream> using namespace std; void main( ) { int worker. Pay. Rate; float work. Hours, pay; worker. Pay. Rate=12; work. Hours=10. 5; pay=work. Hours worker. Pay. Rate; cout<<“A worker earns $”<<pay<<endl; }

Namespaces Missouri State Campus C++ Glass Cheek ostrea m Temple cou t … …

An example // This program calculates a pay check. #include<iostream> using namespace std; void main( ) { int worker. Pay. Rate; float work. Hours, pay; worker. Pay. Rate=12; work. Hours=10. 5; pay=work. Hours worker. Pay. Rate; cout<<“A worker earns $”<<pay<<endl; }

Procedure Part o General form of a procedure or a function Return _Type functio. Name ( parameter list) { Function_body }

An example // This program calculates a pay check. #include<iostream> using namespace std; void main( ) { int worker. Pay. Rate; float work. Hours, pay; worker. Pay. Rate=12; work. Hours=10. 5; pay=work. Hours worker. Pay. Rate; cout<<“A worker earns $”<<pay<<endl; }

Operations : Assignment float current. Temperature; double pi; int Price; current. Temperature = pi = Price = Output cout<<“Current temperature is “ <<current. Temperature<<endl; cout<<“pi=”<<pi<<endl;

Arithmetic Operators o Used for performing numeric calculations o C++ has unary, binary, and ternary operators: n unary (1 operand) n binary (2 operands) n ternary (3 operands) exp 1 ? exp 2 : exp 3 -5 13 - 7

Arithmetic Operators o Ternary operator: conditional operator X<0 ? Y=10 : z=20;

Binary Arithmetic Operators SYMBOL OPERATION EXAMPLE VALUE OF ans + addition ans = 7 + 3; 10 - subtraction ans = 7 - 3; 4 * multiplication ans = 7 * 3; 21 / division ans = 7 / 3; 2 % modulus ans = 7 % 3; 1

/ Operator o / (division) operator performs integer division if both operands are integers cout << 13 / 5; cout << 91 / 7; // displays 2 // displays 13 o If either operand is floating point, the result is floating point cout << 13 / 5. 0; cout << 91. 0 / 7; // displays 2. 6 // displays 13. 0

% Operator o % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 o % requires integers for both operands cout << 13 % 5. 0; // error

An example // This program calculates a pay check. #include<iostream> using namespace std; void main( ) { int worker. Pay. Rate; float work. Hours, pay; worker. Pay. Rate=12; work. Hours=10. 5; pay=work. Hours worker. Pay. Rate; cout<<“A worker earns $”<<pay<<endl; }

Cout Object Exercise: 1) cout<<“You got 98 points. Cheers!”; 2) cout<<“You got ”; cout<<“ 98”; cout<<“ points. ”; cout<<“Cheers!”; 3) cout<<“You got ”<<“ 98” <<“ ponts. ”<<“Cheers!”; 4) cout<<“You got ”<<98<<“ points. Cheers!”; 5) cout<<“You got ”<<98<<“ points. ”<<endl; cout<<“Cheers!”;

Common escape sequences o Exercise 1 He says “I’m genius!” o Exercise 2 My hw is in the directory uploadcsc 125hl

Programming Style o The visual organization of the source code o Includes the use of spaces, tabs, and blank lines o Does not affect the syntax of the program o Affects the readability of the source code

Programming Style // This program calculates a pay check. #include<iostream> using namespace std; void main ( ){ int worker. Pay. Rate; float work. Hours, pay; worker. Pay. Rate=12; work. Hours=10. 5; pay=work. Hours worker. Pay. Rate; cout<< “A worker earns $”<<pay<<endl; }

Programming Style void main( ) { float height=4, width=3. 5, radius=12. 1, base=9, top=5. 2; float area 1, area 2, area 3, area 4; area 1=height width; area 2=3. 14 radius; area 3=height base/2; area 4=(top+base) width/2; cout<<“Areas: ”<<area 1<<“, ”<<area 2<<“, ” <<area 3<<“, ”<<area 4<<endl; }

Programming Style Common elements to improve readability: o Braces { } aligned vertically o Indentation of statements within a set of braces o Blank lines between declaration and other statements o Long statements wrapped over multiple lines with aligned operators

Standard and Prestandard C++ Older-style C++ programs: • • • Use. h at end of header files: #include <iostream. h> Do not use using namespace convention May not compile with a standard C++ compiler