170 likes | 285 Views
Infsy 307 C++ Powerpoint 1. The Basics. A stream is a flow of characters either “in” or “out” of your program!. cout <<identifier1<<“ “<<…………..; << is the insertion operator cout <<num1<<“ number 1”; Can include: arithmetic expressions “<br>” or endl to indicate a new line or endl
E N D
Infsy 307C++Powerpoint 1 The Basics
A stream is a flow of characters either “in” or “out” of your program!
cout <<identifier1<<“ “<<…………..; • << is the insertion operator • cout <<num1<<“ number 1”; • Can include: • arithmetic expressions • “\n” or endl to indicate a new line or endl • Quoted strings
Simple Input/Output in C++ Output stream: the output stream generated by the program cout<<"Press return after entering a number.\n"; cout<<"Enter the number of pods:\n"; cin>>number_of_pods; cout<<"Enter the number of peas in a pod:\n"; cin>>peas_per_pod;
C++ • Input Stream • Uses cin • cin>>identifier1>>identifier2; • Program waits for input and a return (after all input) to designate that the input is complete • Spaces/returns on input will separate the data • cin >>num1>>num2;
Input stream: the input stream generated by the program cout<<"Press return after entering a number.\n"; cout<<"Enter the number of pods:\n"; cin>>number_of_pods; cout<<"Enter the number of peas in a pod:\n"; cin>>peas_per_pod;
// FILE: paymain.cpp //Student Name: //Assignment: Project #3, Page 101 #include <iostream.h> #include "payprogram.h" int main () { payprogram pay; pay.get_pay (); pay.calculate_increase (); pay.calculate_retro(); pay.calculate_annual (); pay.calculate_monthly (); pay.output_var (); cout<<"The End"<<endl; return 0; } //FILE:payprogram.h //Student Name: //Assignment: Project #3, Page // 101 #ifndef PAY_H_ #define PAY_H #include <iostream.h> class payprogram { public: void get_pay ( ); void calculate_increase (); void calculate_retro (); void calculate_annual (); void calculate_monthly (); void output_var (); private: double salary, retro, annual; double monthly; }; // Class definition end. #endif //PAY_H_
Section 2 Variables Operators Declarations and Assignments
C++ • VARIABLES (may change value during program execution) double salary, retro, annual,monthly; • Designates a memory location • Identifies location by a name (identifier) • Names must be meaningful • Declare prior to use - in the header file or top of class function • IDENTIFIER (broader term) • Begins with a letter or underscore • Includes letters,numbers, underscores • Maximum length 127 characters
C++ • RESERVED (KEY) WORDSe.g. class, main, return • Special Class of identifier • May not be used as variable names • CASE SENSITIVE • CONSTANTS • Value may not change const int increase = .076; <identifier = value> const int six_months = .50;
C++ • Class Variables double salary, retro, annual,monthly; • Class Function Variables void payprogram::calculate_retro () { double amt_of_increase; const int increase = .076; const int six_months = .50; double amt_of_increase; amt_of_increase = salary * increase; retro = amt_of_increase * six_months; }
VARIABLE DECLARATIONS • Describes kind of data to be used • Defines storage requirements int whole number 2 bytes float decimal 4 bytes double decimal 8 bytes long int whole number 4 bytes long double decimal 10 bytes char single symbol/single quotes 1 byte string one or more symbols/double quotes bool true/false 2 bytes
Arithmetic Operators • ( ) parentheses • * / % multiply/divide/mod • + and - plus/minus • (See page 863 for full list) PRECEDENCE OF OPERATORS
Variable Declarations Format: DataType identifier1, identifier2,……; const DataType identifier1,………..; Examples: int num1,num2; char achar; const int inch_per_ft = 12;
C++ • TYPE MISMATCH • Common Error • Placing value of one type into location designated as another type • ASSIGNMENTS • Sets value to some expression • num1=25; • num1=num2*25; • achar=‘a’;
// FILE: paymain.cpp //Student Name: //Assignment:Project #3,Pg 101 #include <iostream.h> #include "payprogram.h" int main () { payprogram pay; pay.get_pay (); pay.calculate_increase (); pay.calculate_retro(); pay.calculate_annual (); pay.calculate_monthly (); pay.output_var (); cout<<"The End"<<endl; return 0; } //FILE:payprogram.h //Student Name: //Assignment:Proj. #3,Page // 101 #ifndef PAY_H_ #define PAY_H #include <iostream.h> class payprogram { public: void get_pay ( ); void calculate_increase (); void calculate_retro (); void calculate_annual (); void calculate_monthly (); void output_var (); private: double salary, retro, annual; double monthly; }; // Class definition end. #endif //PAY_H_
// FILE: payprogram.cpp //Student Name:GJY //Assignment:Project#3,Pg 101 #include <iostream.h> #include "payprogram.h" void get_pay () { void payprogram::get_pay (); { cout<<“Please Enter Your Annual Pay"<<endl; cin>>salary; return ; }