A First Program.

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

Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Links and Comments.
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.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Exposure C++ Chapter IV Introduction to C++ Programs.
Hello World 2 What does all that mean?.
Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types.
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.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Programming With C.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
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.
Computer Programming I Hour 2 - Writing Your First C Program.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
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
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
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.
Lecture 4 Computer Programming // 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.
Lecture 3: Getting Started & Input / Output (I/O)
Introduction to C Topics Compilation Using the gcc Compiler
Bill Tucker Austin Community College COSC 1315
Computer Programming Your First Java Program: HelloWorld.java.
Basic concepts of C++ Presented by Prof. Satyajit De
UMBC CMSC 104 – Section 01, Fall 2016
Programming what is C++
User-Written Functions
Topic Pre-processor cout To output a message.
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
COMP 170 – Introduction to Object Oriented Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Links and Comments.
Introduction to C Language
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Beginning C++ Programming
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
void Pointers Lesson xx
Functions Inputs Output
Lesson 2: Building Blocks of Programming
Hello World 2 What does all that mean?.
Introduction to C++ Programming
Links and Comments.
Programming 1 (CS112) Dr. Mohamed Mostafa Zayed 1.
Links and Comments.
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Fundamental Programming
Links and Comments.
Introduction to Programming - 1
Introduction to C Topics Compilation Using the gcc Compiler
Lecture 3 More on Flow Control, More on Functions,
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

A First Program

"Hello, Dave"

Before diving right into the nitty-gritty of C++ language details, let's get started with a full-fledged C++ program! The idea of this program is to introduce you to the overall structure of a C++ program and give you the flavor of C++. Here, then, is an entire C++ program:

//include this file for cout #include <iostream.h> int main() { //print out the text string, "Hello, World!" cout << "Hello, World!" << endl; return 0; }

Compiling and Running Go to the PDF

Brief Explanation of "Hello, Dave"

Now that you've successfully compiled your program, let's take a look at each line of code so that you have a general understanding of how the program works. It will probably look confusing if you've never seen C++ syntax before, but that's completely natural.

Here's the program again: //include this file for cout #include <iostream.h> int main() { //print out the text string, "Hello, World!" cout << "Hello, World!" << endl; return 0; }

Let's take a look at each line of code that makes up hello.C

//include this file for cout This line is a comment line. The // indicates that everything following it should be ignored by the compiler. This allows you to add English explanations to what might otherwise be confusing code. You have the freedom to comment your code as much as you like -- some programmers write code with no comments at all; others write several lines of comments for each line of C++ code. It's all up to you. Keep in mind, though, that if anyone else will ever read your code, you'll probably want to add comments. Even if you are the only one who will ever read your code, you should add comments. It sounds implausible, but programmers often don't understand code they've written weeks ago!

#include <iostream.h> This line is read "pound include i-o-stream dot h". The effect of this line is to essentially "copy and paste" the entire file iostream.h into your own file at this line. So you can think of this syntax as replacing the line #include <iostream.h> with the contents of the file iostream.h. #include is known as a preprocessor directive, which will be covered much later. Where is the file iostream.h? This file is located somewhere in your include path. The include path indicates the directories on your computer in which to search for a file, if the file is not located in the current directory. Why do I need to include iostream.h? In this case, iostream.h is a file containing code for input/output operations. You need to include iostream.h so that the compiler knows about the word cout, which appears a couple of lines below.

int main() { Every C++ program must have what is known as a main function. When you run the program, the program will go through every line of code in the main function and execute it. If your main is empty, then your program will do nothing. There are essentially four parts to a function definition. They are the return type, the function name, the parameter list, and the function body, in that order. In this case: return type: int function name: main parameter list: () function body: { ... } For now, the important thing to remember is that the function body is the part enclosed in { ... } ("curly braces"). The { indicates the beginning of the function, and the } indicates the end of the function. The function body is the stuff in between.

//print out the text string, "Hello, World!" Another comment line. Remember, the compiler ignores anything following // (up until the end of the line), so you can say whatever you want on these lines. cout << "Hello, World!" << endl; This is the line that prints out the text string, "Hello, World!". For now, don't worry about how cout works, just know how to use it. You can print out any series of text strings by separating them with <<. So, instead of saying cout << "Hello, World!" << endl;, you could say cout << "Hello, " << "World" << "!" << endl;. The endl simply adds a carriage return (stands for "end-line"). return 0; This line is necessary because the return type of main is int (see above). We'll talk more about functions and return types later, but for now understand that because the function's return type is int, the function must return an int (integer). To return 0 (which is an integer, we simply write return 0;.