1 / 17

Infsy 307 C++ Powerpoint 1

Infsy 307 C++ Powerpoint 1. The Basics. A stream is a flow of characters either “in” or “out” of your program!. cout &lt;&lt;identifier1&lt;&lt;“ “&lt;&lt;…………..; &lt;&lt; is the insertion operator cout &lt;&lt;num1&lt;&lt;“ number 1”; Can include: arithmetic expressions “<br>” or endl to indicate a new line or endl

Télécharger la présentation

Infsy 307 C++ Powerpoint 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Infsy 307C++Powerpoint 1 The Basics

  2. A stream is a flow of characters either “in” or “out” of your program!

  3. 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

  4. 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;

  5. 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;

  6. 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;

  7. // 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_

  8. Section 2 Variables Operators Declarations and Assignments

  9. 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

  10. 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;

  11. 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; }

  12. 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

  13. Arithmetic Operators • ( ) parentheses • * / % multiply/divide/mod • + and - plus/minus • (See page 863 for full list) PRECEDENCE OF OPERATORS

  14. Variable Declarations Format: DataType identifier1, identifier2,……; const DataType identifier1,………..; Examples: int num1,num2; char achar; const int inch_per_ft = 12;

  15. 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’;

  16. // 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_

  17. // 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 ; }

More Related