Topic Pre-processor cout To output a message.

Slides:



Advertisements
Similar presentations
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Advertisements

 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Chapter 2: Introduction to C++.
Chapter 3 Getting Started with C++
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
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++
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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++
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 3: Getting Started & Input / Output (I/O)
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Programming what is C++
C++ Programming: Presentation 1
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CMPT 201.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC201: Computer Programming
Chapter 2: Introduction to C++
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Beginning C++ Programming
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 – Getting Started
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
CHAPTER 2: COMPONENTS OF PROGRAMMING LANGUAGE
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Capitolo 1 – Introduction C++ Programming
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Topic Pre-processor cout To output a message

A sample C++ Program // sample C++ program #include <iostream> comment // sample C++ program #include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } compiler directive use standard namespace beginning of function named main beginning of block for main output statement Pp55-58 string literal send 0 to operating system; exit main function end of block for main

cout Used to print out a message on the screen Not a keyword in C++ In order to use it, you have to include the file that defines cout --- “iostream.h” Use “#include <iostream>” to do it DEFINE BEFORE USE. pp55 – header file pp58, 443 - cout

The #include Directive Inserts the contents of another file into the program Do not place a semicolon at end of #include line pp55

g++ g++ is a 3-in-1 command A preprocessor will A preprocessor A compiler A linker A preprocessor will Remove #include directives Replace with copies of header files

using namespace std; A namespace is a collection of names. Every name has to be unique Uses standard namespace called std. Ends with a semicolon Follows #include directives in the code Must appear in all programs pp56

Function main int main ( ) { // function body return 0; } Starting point of the program Exactly one main function per program pp56-57

Function A collection of related statements Performs a specific operation int Specify return type of the function Returns an integer when function finishes ( ) Parameters of the function Empty ( ) means “no parameter needed” pp57

cout Displays output on computer screen Used together with the stream insertion operator << to send output to screen: cout << "Programming is fun!"; << can be used multiple times in an instruction cout << “Programming " << “is fun!"; Or: cout << “Programming "; cout << “is fun!"; pp58

Two ways to start a new line use the \n escape sequence (inside quotation marks) cout << "Programming is\nfun!"; Or use the endl manipulator (outside quotations marks) cout << "Programming is" << endl; cout << "fun!"; pp66

The endl Manipulator Do NOT put quotation marks around endl The last character in endl is a lowercase L, not the number 1. endl This is a lowercase L

They will produce Programming is fun!

Escape characters Used within double quotes Don’t get printed literally \n starts a new line \t inserts a tab \\ prints a backslash \ \” prints a double quote \’ prints a single quote pp66

What does this program output? // This program outputs a few sentences. #include <iostream> using namespace std; int main() { cout << “The works of Wolfgang\ninclude the following"; cout << “\nThe Turkish March” << endl; cout << “and Symphony No. 40 “; cout << “in G minor.” <<endl; return 0; } => This code will generate 5 lines of output

Structure of your program Include directives Using namespace Write all instructions inside main( ) function Compiler ignores all blank spaces except those inside quotation marks

Review C++ language elements How to use cout Section 2.1 pages 54-59, 66 How to use cout http://www.cppforschool.com/tutorial/structure-of-cpp-program.html