17
Infsy 307 C++ Powerpoint 1 The Basics

Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

Embed Size (px)

Citation preview

Page 1: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

Infsy 307C++

Powerpoint 1

The Basics

Page 2: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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

Page 3: 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– “\n” or endl to indicate a new line or endl– Quoted strings

Page 4: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

Simple Input/Output in C++

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;

Output stream: the output stream generated by the program

Page 5: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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;

Page 6: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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;

Page 7: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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

Page 8: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

Variables

Operators

Declarations and Assignments

Section 2

Page 9: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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

Page 10: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

C++

RESERVED (KEY) WORDS e.g. class, main, return– Special Class of identifier– May not be used as variable names

CASE SENSITIVE CONSTANTS

– Value may not changeconst int increase = .076; <identifier = value>

const int six_months = .50;

Page 11: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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

Page 12: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

VARIABLE DECLARATIONS– Describes kind of data to be used– Defines storage requirements

int whole number 2 bytesfloat decimal 4 bytesdouble decimal 8 byteslong int whole number 4 byteslong double decimal 10 byteschar single symbol/single quotes 1 bytestring one or more symbols/double quotesbool true/false 2 bytes

Page 13: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

Arithmetic Operators

( ) parentheses * / % multiply/divide/mod + and - plus/minus

(See page 863 for full list)

PRECEDENCE OF OPERATORS

Page 14: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

Variable Declarations

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

Page 15: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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

Page 16: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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

Page 17: Infsy 307 C++ Powerpoint 1 The Basics. A stream is a flow of characters either “in” or “out” of your program!

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