1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.

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.
Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
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.
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.
Chapter 2: Introduction to C++.
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.
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++
Hello World 2 What does all that mean?.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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++
Basic Program Construction
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
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 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 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 1: Introduction to Computers and Programming.
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
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.
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.
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
What Actions Do We Have Part 1
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.
CS-103 COMPUTER PROGRAMMING
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?.
2.1 Parts of a C++ Program.
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
Arithmetic Operations
Capitolo 1 – Introduction C++ Programming
Introduction to Programming - 1
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program

2 8/30/06CS150 Introduction to Computer Science 1 Problem  Programs are written to solve problems  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

3 8/30/06CS150 Introduction to Computer Science 1 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!

4

5 8/30/06CS150 Introduction to Computer Science 1 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

6 8/30/06CS150 Introduction to Computer Science 1 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 ;

7 8/30/06CS150 Introduction to Computer Science 1 Syntax  Rules that must be followed when constructing a program  Controls the use of key words, programmer- defined identifiers, operators, and punctuation

8 8/30/06CS150 Introduction to Computer Science 1 Variables  Names storage location in the computers memory  Holds data  The data can change

9 8/30/06CS150 Introduction to Computer Science 1 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

10 8/30/06CS150 Introduction to Computer Science 1 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  The editor in Visual Studio will colour code your comments. They will be green

11 8/30/06CS150 Introduction to Computer Science 1 Comments  There are two ways to write comments o // I am a comment  Anything after // till 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

12 8/30/06CS150 Introduction to Computer Science 1 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

13 8/30/06CS150 Introduction to Computer Science 1 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

14 8/30/06CS150 Introduction to Computer Science 1 Namespace std  using namespace std;  Indicates that we will be using objects ( cout & cin ) that are named in a region called std  The statement ends in a semicolon  The statement appears in all our programs

15 8/30/06CS150 Introduction to Computer Science 1 main Function int main() { // program statements return 0; }  Every program must have a main function  It is 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

16 8/30/06CS150 Introduction to Computer Science 1 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;

17 8/30/06CS150 Introduction to Computer Science 1 Program Statements  Declaration statements string name;  Executable statements cout << “Type your name, then press enter” << endl; cin >> name; cout << “Hello “ << name << “!” << endl;

18 8/30/06CS150 Introduction to Computer Science 1 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; }

19 8/30/06CS150 Introduction to Computer Science 1 Summary  Today we o Wrote our first C++ program o Introduced the basic components of a C++ program  To see the program in action you should test it in Visual Studio 2005 o New Project | General | Empty Project o Add Item | C++ Source File  We covered p from your textbook