Your First C++ Program Aug 27, 2008. 2 8/27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
COSC 120 Computer Programming
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Three types of computer languages
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Hello World 2 What does all that mean?.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
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.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
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++
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
CS CS CS IA: Procedural Programming CS IB: Object-Oriented Programming.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
Basic Program Construction
Control Structures (B) Topics to cover here: Sequencing in C++ language.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 05, 2005 Lecture Number: 4.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CS Computer Science I. BCPL was developed in 1967 as a language for writing operating systems and software compilers In 1970, the creators of the.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Foundations of Computer Science C & C++ programming lecture 2
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Computer Terms Review from what language did C++ originate?
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
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.
Chapter 2 part #1 C++ Program Structure
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Hello World 2 What does all that mean?.
CS150 Introduction to Computer Science 1
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Lab 1 Introduction to C++.
CS IA: Procedural Programming CS IB: Object-Oriented Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Computer Terms Review from what language did C++ originate?
Introduction to Programming - 1
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Your First C++ Program Aug 27, 2008

2 8/27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most popular programming languages o used extensively in industry  Language + Libraries o Libraries: bits of programs you can use

3 8/27/08 CS 150 Introduction to Computer Science I Visual Studio  Microsoft Tools o Integrated Development Environment o all the tools you need to write a program!  Source code editor o highlights source code as you type  Debugger  Extra Libraries

4 8/27/08 CS 150 Introduction to Computer Science I Problem  Programs are written to solve problems o source code o process data  Imagine that you have been asked to solve the following problem o Write a program that asks the user to enter their name and display a personalized welcome message

5 8/27/08 CS 150 Introduction to Computer Science I Your First C++ Program //*********************************************************** // File name: hello.cpp // Author: Bob Smith // Date: 08/30/2006 // Purpose: This program displays a welcome message to // the user //*********************************************************** #include using namespace std; int main() { string name; cout << " Type your name, then press enter " << endl; cin >> name; cout << " Hello " << name << " ! " << endl; return 0; } Program Output: Type your name, then press enter Shereen Hello Shereen!

6 8/27/08 CS 150 Introduction to Computer Science I

7 8/27/08 CS 150 Introduction to Computer Science I Building an Application Source Code #include int main() {... } Preprocessor Modified Source Code #hdr int foo() { … } int main(){ … Compiler foo: main:10111 Linker Object Code Executable Code link, ld cl, gcc, cc, g++ app.exe YOU ARE HERE!

8 8/27/08 CS 150 Introduction to Computer Science I Language Elements  Key Words o Have special meaning in C++ o using namespace int  Programmer-Defined Identifiers o Names made up by the programmer o name  Operators o Perform operations o * =  Punctuation o Used to mark the beginning and end of the program o ;

9 8/27/08 CS 150 Introduction to Computer Science I Syntax (Grammar)  Rules that must be followed when constructing a program  Controls the use of key words, programmer- defined identifiers, operators, and punctuation

10 8/27/08 CS 150 Introduction to Computer Science I Variables  Names storage location in the computers memory  Holds data  The data can change Address Contents Shereen string name;

11 8/27/08 CS 150 Introduction to Computer Science I Program Components  The C++ program on the previous slide consists of the following elements: o Comments o Preprocessor directives o Standard namespace o main function o Declaration statements o Executable statements

12 8/27/08 CS 150 Introduction to Computer Science I Comments  Comments are o How you explain in English what the different parts of your program do o Ignored by the compiler o Very important for you and me  The editor in Visual Studio will colour code your comments. They will be green

13 8/27/08 CS 150 Introduction to Computer Science I Comments  There are two ways to write comments o // I am a comment  Anything after // to the end of the line will be a comment o /* I am another comment */  You must start the comment with /* and end it with */ in this style of comment

14 8/27/08 CS 150 Introduction to Computer Science I Preprocessor directives  #include  # signifies preprocessor directive  Processed before program translation  #include tells the preprocessor to look for libraries  <> signifies part of standard C++ libraries  We’ll see other examples of preprocessor directives later

15 8/27/08 CS 150 Introduction to Computer Science I Preprocessor directives  iostream is the input/output stream library  It is needed to output data to the screen and read in data from the keyboard  #include takes the contents of the library file and places them in the current program

16 8/27/08 CS 150 Introduction to Computer Science I Namespace std  using namespace std;  Indicates that we will be using objects ( cout & cin ) that are named in a region called std o predefined objects C++ provides  The statement ends in a semicolon  The statement appears in all our programs

17 8/27/08 CS 150 Introduction to Computer Science I main Function int main() { // program statements return 0; }  Where the start of your program execution begins  return 0; ends the main function and indicates that the program terminated successfully  Everything within the double braces {} should be indented o Style!

18 8/27/08 CS 150 Introduction to Computer Science I Program Statements  There are two types of statements that you can write inside the main (or any other) function o Declaration statements  Specify the data that is needed by the program o Executable statements  Perform operations  All statements must end with a semicolon;

19 8/27/08 CS 150 Introduction to Computer Science I Program Statements  Declaration statements string name;  Executable statements cout << “Type your name, then press enter” << endl; cin >> name; cout << “Hello “ << name << “!” << endl;

20 8/27/08 CS 150 Introduction to Computer Science I Program Skeleton  All programs in C++ should have the following skeleton //*********************************************************** // File name: filename.cpp // Author: Your Name // Date: 09/01/2004 // Purpose: Description about what the program does //*********************************************************** #include using namespace std; int main() { // declaration statements // executable statements return 0; }

21 8/27/08 CS 150 Introduction to Computer Science I Do try this at home (or in the lab) !  Add New Project o Visual C++ | General | Empty Project  Add New Item o C++ Source File

22 8/27/08 CS 150 Introduction to Computer Science I Summary  Today we o Wrote our first C++ program o Introduced the basic components of a C++ program  We covered p from your textbook