Creating your first C++ program

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
CSE202: Lecture 1The Ohio State University1 Introduction to C++
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.
Introduction to C Creating your first C program. Writing C Programs  The programmer uses a text editor to create or modify files containing C code. 
 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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 2: Your First Program.
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.
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.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Hello World 2 What does all that mean?.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
Programming With C.
Chapter 02 (Part III) Introduction to C++ Programming.
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.
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.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
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.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
Introduction to C CMSC 104, Section 4 Richard Chang 1.
Introduction to C 2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park 1.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
CMSC 1041 Introduction to C Creating your first C program.
Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3: Getting Started & Input / Output (I/O)
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
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.
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.
Completing the Problem-Solving Process
Introduction to C Language
CS-103 COMPUTER PROGRAMMING
Chapter 2 part #1 C++ Program Structure
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.
Computer Engineering 1nd Semester
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 part #3 C++ Input / Output
Hello World 2 What does all that mean?.
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Chapter 2 part #3 C++ Input / Output
Introduction to Programming - 1
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
SPL – PS1 Introduction to C++.
Presentation transcript:

Creating your first C++ program Introduction to C++ Creating your first C++ program

Writing C++ Programs The programmer uses a text editor to create or modify files containing C code. C code is a specific instance of source code. A file containing source code is called a source file. After a source file has been created, the programmer must invoke the C compiler.

Using Borland complier LINUX Using Borland complier

The Result : a.out If there are no errors in g++ .cpp, this command produces an executable file, one that can be run or executed. Both the cpp and the gcc compilers name the executable file a.out To execute the program, type a.out at the Unix prompt. Although we call this “compiling a program”, what actually happens is more complicated.

3 Stages of Compilation Preprocessor - modifies the source code Handles preprocessor directives Strips comments and “white space” from the code

3 Stages of Compilation Compiler - translates the modified source code into object code Parser - checks for errors Code Generator - makes the object code Optimizer - may change the code to be more efficient

3 Stages of Compilation Linker - combines the object code of our program with other object code to produce the executable file. The other object code can come from: The Run-Time Library - a collection of object code with an index so that the linker can find the appropriate code. other object files other libraries

Compilation Diagram < Compiler Preprocessor Parser Linker Editor Source File myprog.cpp Compiler Preprocessor Parser Code Generator Optimizer Object File myprog.obj Other Obj’s Run-time library Other libraries Linker Executable file a.out

editor Mypgm. cpp Pre Processor Header Library Complier Temp file Object file Linker Object Library .EXE file

An algorithm for writing code Write the algorithm/do flow chart Write the code using emacs (pico, vi) While not yet working… Try to compile the code While there are still syntax errors Fix errors end while still syntax errors Run the program Fix logical errors end while not yet working

Beginning the program Draw your “Black Box” Write your algorithm identify your inputs and outputs Write your algorithm do it in PENCIL! Look at it! Play computer - will this work? Think about it! Now, head to the computer...

Incremental Approach to Writing Code Move your entire algorithm (highest level) into the text editor. Select the first segment of your algorithm. Write your code and test it for this small piece. For instance: For your project Don’t write the whole program at once. Just write enough that you display the prompt to the user on the screen. Get that part working first. Next write the part that gets the value from the user, and then just print it out.

Incremental Approach to Writing Code (continued) Get that working. Next, pick the next piece of code. Write it. Make program modifications: perhaps additional instructions to the user a displayed program description for the user add more comments. Continue piece by piece until your whole top level is working. Repeat this process for each function.

A Simple C++ Program //* Charlene Davis Sept. 02. 2008 Hello.cpp //* This program is used as a template for future assignments //* Input: Hello World //* Output: Hello World #include <iostream> #include <string> using namespace std; int main() { string name,anychar; cout << "Please enter your name: "; cin >>name; cout << "Hello World, this is "<< name<< endl; cin >>anychar; return 0; }

Anatomy of a C++ Program program header comment preprocessor directives main ( ) { statement(s) }

The Program Header Comment All comments must begin with the characters /* and end with the characters */ The program header comment always comes first! The program header comment should include the filename, author, date written and a description of the program

Preprocessor Directive Lines that begin with a # are called preprocessor directives The #include <stdio> directive causes the preprocessor to include a copy of the standard input/output header file stdio at this point in the code. Us e this with C This header file was included because it contains information about the printf ( ) function that’s used in this program.

main ( ) Every program has a function called main, where execution begins The parenthesis following main indicate to the compiler that it is a function.

Left Brace A left curly brace – { -- begins the body of every function. A corresponding right curly brace must end the function. The style is to place these braces on separate lines in column 1.

Right Brace This right curly brace -- } --matches the left curly brace above. It ends the function main ( ).

Good Programming Practices C++ programming standards are available on the Web -- see course homepage You will be expected to conform to these standards for all programming projects in this These standards include: Naming conventions Use of white space Use of Braces Comments

Examples of Comment Styles /* a comment */ /*** another comment ***/ /*****/ /*A comment can be written in this * fashion to set it off from the * surrounding code. */

More Comments /******************************************* * If you wish, you can put comments * * in a box. This is typically used for * * program header comments and for * * function header comments * *******************************************/

Use of White Space (con’t) All executable statements are indented one tab stop. How deep should my tabs be ? Typically 3 or 4 spaces. 2 is not enough for good readability, more than four causes indentation to be too deep.