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
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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
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.
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.
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?.
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++
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
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
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.
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:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
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.
Bill Tucker Austin Community College COSC 1315
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.
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.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Life is Full of Alternatives
Arithmetic Operations
What Actions Do We Have Part 1
Capitolo 1 – Introduction C++ Programming
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 Today  In today’s lecture we will o Write our first C++ program o Analyze the different components of C++ programs

3 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 Your summer surveying job requires you to study some maps that give the distance in kilometers and some that use miles. You and your co-workers prefer to deal in metric measurements. Write a program that performs the necessary conversion

4 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja 4 // Date: 08/23/ // Purpose: This program displays a welcome message to 6 // the user 7 //*********************************************************** 8 #include 9 #include “stdafx.h” using namespace std; int main() 14 { 15 string name; cout << “Type your name, then press enter” << endl; cin >> name; cout << “Hello “ << name << “!” << endl; return 0; 24 } Program Output Type your name, then press enter Chadd Hello Chadd!

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

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; Create some memory labeled ‘name’ to hold a string 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.NET  We covered p from your textbook