Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.

Slides:



Advertisements
Similar presentations
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Advertisements

Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Thursday, December 07, 2006 “Vision without action is a daydream, Action without a vision is a nightmare.” - Japanese Proverb.
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.
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 2: Introduction to C++.
Programming Introduction to C++.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
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 Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague,
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Variables, Identifiers, Assignments, Input/Output
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Working with Java.
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
Computing Fundamentals
Programming Introduction to C++.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Chapter 2: Introduction to C++
Basics (Variables, Assignments, I/O)
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables in C Topics Naming Variables Declaring Variables
Chapter 1: Computer Systems
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Introduction to C++ Programming

Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides a comprehensive set of features for handling a wide variety of applications, such as systems development and scientific computation. l C++ is an “extension” of the C language, in that most C programs are also C++ programs. l C++, as opposed to C, supports “object- oriented programming.”

General form of a C++ program // Program description #include directives int main() { constant declarations variable declarations executable statements return 0; }

C++ keywords l Keywords appear in blue in Visual C++. l Each keyword has a predefined purpose in the language. l Do not use keywords as variable and constant names!! l The complete list of keywords is on page 639 of the textbook. l We shall cover the following keywords in this class: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while

Example 0 l Peter: Hey Frank, I just learned how to add two numbers together. l Frank: Cool! l Peter : Give me the first number. l Frank: 2. l Peter : Ok, and give me the second number. l Frank: 5. l Peter : Ok, here's the answer: = 7. l Frank: Sheesh! This guy is unbelievable! l after Frank says “2”, Peter has to keep this number in his mind. 257 First number: Second number: Sum: l after Frank says “5”, Peter also needs to keep this number in his mind.

#include using namespace std; int main() { int first, second, sum; cout << "Peter: Hey Frank, I just learned how to add” << “ two numbers together."<< endl; cout << "Frank: Cool!" <<endl; cout << "Peter: Give me the first number."<< endl; cout << "Frank: "; cin >> first; cout << "Peter: Give me the second number."<< endl; cout << "Frank: "; cin >> second; sum = first + second; cout << "Peter: OK, here is the answer:"; cout << sum << endl; cout << "Frank: Sheesh! This guy is amazing!" << endl; return 0; }

#include using namespace std; int main() { int number_of_pods, peas_per_pod, total_peas; cout << "Press return after entering a number.\n"; cout << "Enter the number of pods:\n"; cin >> number_of_pods; cout << "Enter the number of peas in a pod:\n"; cin >> peas_per_pod; total_peas = number_of_pods * peas_per_pod; Demo Example 1

cout << "If you have "; cout << number_of_pods; cout << " pea pots\n"; cout << "and "; cout << peas_per_pod; cout << " pea in each pod, then \n"; cout << "you have "; cout << total_peas; cout << " peas in all the pods.\n"; return 0; } Demo Example 1

l Identifiers appear in black in Visual C++. n An identifier is a name for a variable, constant, function, etc. n It consists of a letter followed by any sequence of letters, digits, and underscores. Examples of valid identifiers: First_name, age, y2000, y2k Examples of invalid identifiers: 2000y Identifiers cannot have special characters in them. For example: X=Y, J-20, ~Ricky,*Michael are invalid identifiers. Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI, whoami are unique identifiers. C++ identifiers

C++ comments l Comments appear in green in Visual C++. l Comments are explanatory notes; they are ignored by the compiler. l There are two ways to include comments in a program: // A double slash marks the start of a //single line comment. /* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash. */

C++ compiler directives l Compiler directives appear in blue in Visual C++. The #include directive tells the compiler to include some already existing C++ code in your program. l The included file is then linked with the program. There are two forms of #include statements: #include //for pre-defined files #include "my_lib.h" //for user-defined files

Programming Style C++ is a free-format language, which means that: l Extra blanks (spaces) or tabs before or after identifiers/operators are ignored. l Blank lines are ignored by the compiler just like comments. l Code can be indented in any way. l There can be more than one statement on a single line. l A single statement can continue over several lines.

In order to improve the readability of your program, use the following conventions: l Start the program with a header that tells what the program does. l Use meaningful variable names. l Document each variable declaration with a comment telling what the variable is used for. l Place each executable statement on a single line. l A segment of code is a sequence of executable statements that belong together. n Use blank lines to separate different segments of code. n Document each segment of code with a comment telling what the segment does. Programming Style (cont. )

What makes a bad program? l Writing Code without detailed analysis and design l Repeating trial and error without understanding the problem l Debugging the program line by line, statement by statement l Writing tricky and dirty programs

PROGRAMMER'S DRINKING SONG!! 100 little bugs in the code, 100 bugs in the code, fix one bug, compile it again, 101 little bugs in the code. 101 little bugs in the code … Repeat until BUGS = 0 — The Internet Joke Book