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

Slides:



Advertisements
Similar presentations
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Advertisements

1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 2: Introduction to C++.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Introduction to C++ Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Introduction to C The Parts of a C++ Program –Anatomy of a simple C++ program.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Control Structures (B) Topics to cover here: Sequencing in C++ language.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ for Engineers and Scientists Second Edition
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
LESSON 2 Basic of C++.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Introduction to C++. 2 What Is a Program Made Of?
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Chapter 2 Introduction to C++. 2 Topics 2.1 Parts of a C++ Program 2.2 The cout Object 2.3 The #include Directive 2.4 Variables and Constants 2.5 Identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
TK1913 C++ Programming Basic Elements of C++.
Chapter 2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Introduction to C++ Programming
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Lecture 2 Variables, Types, Operators
Introduction to Programming - 1
Presentation transcript:

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

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

An example // This program calculates a pay check. #include using namespace std; void main( ) { int workerPayRate; float workHours, pay; workerPayRate=12; workHours=10.5; pay=workHours  workerPayRate; cout<<“A worker earns $”<<pay<<endl; }

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

Comments My: // all students grades statistics void main( ) { } Linda: // freshman grades statistics void main( ) { } David: // sophomore grades statistics void main( ) { } Kevins: // 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 area = length * width;

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

An example // This program calculates a pay check. #include using namespace std; void main( ) { int workerPayRate; float workHours, pay; workerPayRate=12; workHours=10.5; pay=workHours  workerPayRate; cout<<“A worker earns $”<<pay<<endl; }

Preprocessor Directive Part  # include

An example // This program calculates a pay check. #include using namespace std; void main( ) { int workerPayRate; float workHours, pay; workerPayRate=12; workHours=10.5; pay=workHours  workerPayRate; cout<<“A worker earns $”<<pay<<endl; }

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

An example // This program calculates a pay check. #include using namespace std; void main( ) { int workerPayRate; float workHours, pay; workerPayRate=12; workHours=10.5; pay=workHours  workerPayRate; cout<<“A worker earns $”<<pay<<endl; }

Procedure Part  General form of a procedure or a function Return _Type functioName ( parameter list) { Function_body }

An example // This program calculates a pay check. #include using namespace std; void main( ) { int workerPayRate; float workHours, pay; workerPayRate=12; workHours=10.5; pay=workHours  workerPayRate; cout<<“A worker earns $”<<pay<<endl; }

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

Arithmetic Operators  Used for performing numeric calculations  C++ has unary, binary, and ternary operators: unary (1 operand) -5 binary (2 operands) ternary (3 operands) exp1 ? exp2 : exp3

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

Binary Arithmetic Operators SYMBOLOPERATIONEXAMPLEVALUE 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  / (division) operator performs integer division if both operands are integers cout << 13 / 5; // displays 2 cout << 91 / 7; // displays 13  If either operand is floating point, the result is floating point cout << 13 / 5.0; // displays 2.6 cout << 91.0 / 7; // displays 13.0

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

An example // This program calculates a pay check. #include using namespace std; void main( ) { int workerPayRate; float workHours, pay; workerPayRate=12; workHours=10.5; pay=workHours  workerPayRate; 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  Exercise 1 He says “I’m genius!”  Exercise 2 My hw is in the directory \upload\csc125\hl

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

Programming Style // This program calculates a pay check. #include using namespace std; void main ( ){ int workerPayRate; float workHours, pay;workerPayRate=12;workHours=10.5; pay=workHours  workerPayRate; 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 area1, area2, area3, area4; area1=height  width; area2=3.14  radius  radius; area3=height  base/2; area4=(top+base)  width/2; cout<<“Areas:”<<area1<<“, ”<<area2<<“, ” <<area3<<“, ”<<area4<<endl; }

Programming Style Common elements to improve readability:  Braces { } aligned vertically  Indentation of statements within a set of braces  Blank lines between declaration and other statements  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 Do not use using namespace convention May not compile with a standard C++ compiler