Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++

Slides:



Advertisements
Similar presentations
Programming for Beginners
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
C Programming for engineers Teaching assistant: Ben Sandbank Home page:
Introducing Programming a general discussion. What is a Program? Sets of instructions that get the computer to do something Programs may be a few lines.
COSC 120 Computer Programming
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.
Computers: Tools for an Information Age
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
Guide To UNIX Using Linux Third Edition
Assembler Compiler Interpreter ASSEMBLER To convert the assembly language into machine code. Translate mnemonic operation codes to their machine language.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CS102 Introduction to Computer Programming
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 9 Interactive Multimedia Authoring with Flash - Introduction to Programming “Computers and Creativity” Richard D. Webster, COSC 109 Instructor.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
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)
Introduction COMP104: Fundamentals and Methodology.
Object-Oriented Programming in C++
High-Level Programming Languages: C++
UNIVERSITI TENAGA NASIONAL “Generates Professionals” CHAPTER 4 : Part 2 INTRODUCTION TO SOFTWARE DEVELOPMENT: PROGRAMMING & LANGUAGES.
COMPUTER PROGRAMMING Source: Computing Concepts (the I-series) by Haag, Cummings, and Rhea, McGraw-Hill/Irwin, 2002.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
CS2311 Computer Programming
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Creating your first C++ program
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Fundamental Programming: Fundamental Programming Introduction to C++
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Module 4 Part 2 Introduction To Software Development : Programming & Languages Introduction To Software Development : Programming & Languages.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Invent Your Own Computer Games with Python
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Fundamental Programming Fundamental Programming Introduction to Functions.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
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.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Software Engineering Algorithms, Compilers, & Lifecycle.
PROGRAMMING (1) LECTURE # 1 Programming and Languages: Telling the Computer What to Do.
Bill Tucker Austin Community College COSC 1315
Programming what is C++
Programming Introduction to C++.
Computer science By/ Midhat Mohiey. Introduction to Programming using C ++ 2.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++

Session 13 – aims and objectives So far we’ve only talked about Java, but there are many other languages available... Today, we will take a quick look at one alternative: C++. We will see that C++ has many similarities to Java. There are also many key differences.

Languages Available So far, we’ve only talked about Java, but there are many other languages available... Language choice depends on the task in hand! Procedural Fortran COBOL (old versions) Pascal C Perl Object-oriented  COBOL (latest version)  C++  C#  Java  Delphi  Visual Basic  Perl

Languages Available Fortran (acronym for formula translator) is one of the oldest high-level languages. It is still commonly used for mathematical/scientific computation. COBOL (Common business-oriented language) is another old high-level language. Some argue that it is now very out-dated. Was used for business applications running on large computers. Required very wordy coding! Pascal – often used for teaching, but not much else. Very inflexible. Perl (Practical Extraction & Report Language) is a programming language especially designed for processing text. Good for quick program development, task automation and some web applications.

C Procedural language Originally designed as a systems programming language – e.g. Unix. Can be used for a variety of applications, from business programs to engineering. Requires less memory than other languages. Lower-level, a lot closer to assembly language than other high-level languages this can make it unsuitable for some applications – especially those requiring rapid development.

C++ C, but incremented once! Includes object-orientation, but can still use procedural syntax too. Popular for writing programs for graphical applications, such as Windows or Mac applications Good general language to learn, especially if you want a job as a programmer!

C# Hybrid of C and C++, written by Microsoft to compete with Java. Object-oriented programming language used with XML- based web services on the.NET platform. We’ll talk about XML next time... The.NET framework allows interaction between codes written in different languages. Designed for improving productivity in the development of web applications. Many similarities to Java!

Differences between C and C++ C++ is object-oriented; C is not C++ standardizes how objects are created and managed; C is a lot more variable in terms of style and design. C++ applications are slower at runtime and slower to compile than C programs. C++ programs tend to be larger than C. C interacts with memory much more than C++ (pointers).

Compiling C++ Code on Granby Various C++ compilers are available. We will use g++ C++ files are usually given the extension.cpp To compile the file test.cpp: g++ test.cpp By default, the compiler will create an executable called a.out. To run it, type./a.out Or, we can give the output a specific name (test): g++ -o test test.cpp./test

Compiling C++ Code on Granby Various C++ compilers are available. We will use g++ C++ files are usually given the extension.cpp To compile the file test.cpp: g++ test.cpp By default, the compiler will create an executable called a.out. To run it, type./a.out Or, we can give the output a specific name (test): g++ -o test test.cpp./test Input file name Output file name

Hello World Typical beginner’s program Used as first exposure to a new programming language or environment Can be written in loads of different languages: (lists 204 examples of different Hello World programs!)

Hello World in Java class Hello { public static void main (String[] args); { System.out.print("Hello World"); }

Hello World in C #include main() { printf ("Hello World!\n"); } Just as in Java, every C program has a method called main, which is run when the program starts. The printf method is used to write some text on the screen. Header file providing the printf method

Hello World in C++ #include using namespace std; int main() { cout << “Hello World!\n"; return 0; } In C++, cout represents the standard output stream (the screen. The operator << can be read as “send to”. Provides cout Don’t worry about this yet! In C++, main must return an int.

User Input in C++ Just as cout is the standard output stream (the screen), we have cin to represent the standard input (the keyboard). The operator >> means “read from”.

User Input in C++ #include using namespace std; int main() { string name; cout <<“Tell me your name!” << endl; cin >> name; cout << “Hello “ << name << endl; return 0; } Provides cout & cin Provides the string class Declare a string Newline

Flow Control in C++ For loop: for( int i=1; i<100; i++) { cout << i << endl; } While loop: int i=100; while(i>0) { cout << i << endl; i--; }

Flow Control in C++ If statement: int score; cout << “Enter your exam mark” << endl; cin >> score; if(score<40) { cout << “You failed!” << endl; } else { cout << “You passed!” << endl; }

Arrays in C++ double nums[3]; nums[0] = 1; nums[1] = 12; nums[2] = 428; double total = nums[0]+nums[1]+nums[2]; cout << “Total is “ << total << endl; Beware: C++ will let you refer to nums[3] in the above, but that data will rarely make sense and is likely to make your code malfunction. Can also have arrays of arrays! e.g. nums[4][5] Declare an array Assign values

Methods in C++ Easiest way (for now) is to define methods above main. Can also define methods below main, or in separate files, but this is harder. We’ll ignore these options for now. Just like Java, methods have a return type and (optionally) a list of arguments.

Methods in C++ #include using namespace std; // Method to add two numbers together double add(double a, double b) { return a+b; } int main() { cout << add(1,3) << endl; return 0; }

Want to know more about C++? if(answer==no) { Stop listening for a minute! } else { Sign up for the forthcoming C++ course! } C++ Programming Ten Sessions: 12 th April – 17 th May 2011 Tutor: Dr. Robert Oates More info:

Exercises Choice of exercises for today: Either have a go at some new exercises, which introduce some familiar concepts in C++. Or, continue with your Java Address Book.

Coming up in Session 14 An introduction to scripting languages HTML CSS XML/XSLT php javascript