The Fundamentals of C++

Similar documents
Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++ Language Tutorial

Appendix K Introduction to Microsoft Visual C++ 6.0

Pemrograman Dasar. Basic Elements Of Java

VB.NET Programming Fundamentals

The programming language C. sws1 1

Number Representation

Computer Science 281 Binary and Hexadecimal Review

Chapter One Introduction to Programming

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Informatica e Sistemi in Tempo Reale

CS 106 Introduction to Computer Science I

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Sources: On the Web: Slides will be available on:

Base Conversion written by Cathy Saxton

CSI 333 Lecture 1 Number Systems

Chapter 2: Elements of Java

An Incomplete C++ Primer. University of Wyoming MA 5310

Introduction to Python

.NET Standard DateTime Format Strings

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction to Visual C++.NET Programming. Using.NET Environment

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Variables, Constants, and Data Types

Numbering Systems. InThisAppendix...

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java Basics: Data Types, Variables, and Loops

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Useful Number Systems

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Sequential Program Execution

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Python

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Introduction to Java

The string of digits in the binary number system represents the quantity

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Moving from CS 61A Scheme to CS 61B Java

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

Binary Numbers. Binary Octal Hexadecimal

Chapter 4: Computer Codes

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Formatting Numbers with C++ Output Streams

Basics of I/O Streams and File I/O

Chapter 2 Elementary Programming

6.170 Tutorial 3 - Ruby Basics

Visual Logic Instructions and Assignments

CS106A, Stanford Handout #38. Strings and Chars

Chapter 3. Input and output. 3.1 The System class

Computer Science 217

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Objective-C Tutorial

Curriculum Map. Discipline: Computer Science Course: C++

So far we have considered only numeric processing, i.e. processing of numeric data represented

DNA Data and Program Representation. Alexandre David

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

El Dorado Union High School District Educational Services

Answers to Review Questions Chapter 7

Comp151. Definitions & Declarations

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Chapter 7D The Java Virtual Machine

Appendix M: Introduction to Microsoft Visual C Express Edition

Core Components Data Type Catalogue Version October 2011

HOMEWORK # 2 SOLUTIO

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Data Structures using OOP C++ Lecture 1

Some Scanner Class Methods

Object Oriented Software Design

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

Solution for Homework 2

Welcome to Introduction to programming in Python

2011, The McGraw-Hill Companies, Inc. Chapter 3

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

C++ Input/Output: Streams

Systems I: Computer Organization and Architecture

I PUC - Computer Science. Practical s Syllabus. Contents

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

JavaScript: Control Statements I

Section 1.4 Place Value Systems of Numeration in Other Bases

Moving from C++ to VBA

Part 1 Foundations of object orientation

Object Oriented Software Design

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Compiler Construction

The Answer to the 14 Most Frequently Asked Modbus Questions

Transcription:

The Fundamentals of C++ Basic programming elements and concepts JPC and JWD 2002 McGraw-Hill, Inc. Program Organization Program statement Definition Declaration Action Executable unit Named set of program statements Different languages refer to executable units by different names Subroutine: Fortran and Basic Procedure: Pascal Function : C++ 1

Program Organization C++ program Collection of definitions, declarations and functions Collection can span multiple files Advantages Structured into small understandable units Complexity is reduced Overall program size decreases Object Object is a representation of some information Name Values or properties Data members Ability to react to requests (messages)!! Member functions When an object receives a message, one of two actions are performed Object is directed to perform an action Object changes one of its properties 2

A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings // Author(s): Ima Programmer // Date: 1/24/2001 Comments #include <iostream> #include <string> Provides simple access using namespace std; int main() { cout << "Hello world!" << endl; return 0; } Ends executions of main() which ends program Insertion statement Function Greeting Output 3

#include <iostream> using namespace std; int main() { // Extract length and width cout << "Rectangle dimensions: "; float Length; float Width; cin >> Length >> Width; Area.cpp Definitions Extraction // Compute and insert the area float Area = Length * Width; Definition with initialization } cout << "Area = " << Area << " = Length " << Length << " * Width " << Width << endl; return 0; Visual C++ IDE with Area.cpp 4

Area.cpp Output Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood so that they can be maintained C++ has two conventions for comments // single line comment (preferred) /* long comment */ (save for debugging) Typical uses Identify program and who wrote it Record when program was written Add descriptions of modifications 5

Fundamental C++ Objects C++ has a large number of fundamental or built-in object types The fundamental object types fall into one of three categories Integer objects Floating-point objects Character objects 1 5 1.28345 P Z 3.14 Integer Object Types The basic integer object type is int The size of an int depends on the machine and the compiler On PCs it is normally 16 or 32 bits Other integers object types short: typically uses less bits long: typically uses more bits Different types allow programmers to use resources more efficiently Standard arithmetic and relational operations are available for these types 6

Integer Constants Integer constants are positive or negative whole numbers Integer constant forms Decimal Octal (base 8) Digits 0, 1, 2, 3, 4, 5, 6, 7 Hexadecimal (base 16) Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, F Consider 31 oct and 25 dec Decimal Constants Examples 97 40000L 50000 L or l indicates long integer 23a (illegal) The type of the constant depends on its size, unless the type specifier is used 7

Character Object Types Character type char is related to the integer types Characters are encoded using a scheme where an integer represents a particular character ASCII is the dominant encoding scheme Examples ' ' encoded as 32 '+' encoded as 43 'A' encoded as 65 'Z' encoded as 90 'a' encoded as 97 'z' encoded as 122 Appendix A gives the complete ASCII character set Character Operations Arithmetic and relational operations are defined for characters types 'a' < 'b' is true '4' > '3' is true '6' <= '2' is false 8

Character Constants Explicit (literal) characters within single quotes 'a','d','*' Special characters - delineated by a backslash \ Two character sequences (escape codes) Some important special escape codes \t denotes a tab \n denotes a new line \\ denotes a backslash \' denotes a single quote \" denotes a double quote '\t' is the explicit tab character, '\n' is the explicit new line character, and so on Literal String Constants A literal string constant is a sequence of zero or more characters enclosed in double quotes "We are even loonier than you think" "Rust never sleeps\n" "Nilla is a Labrador Retriever" Not a fundamental type 9

Floating-Point Object Types Floating-point object types represent real numbers Integer part Fractional part The number 108.1517 breaks down into the following parts 108 - integer part 1517 - fractional part C++ provides three floating-point object types float double long double Floating-Point Constants Standard decimal notation 134.123 0.15F F or f indicates single precision floating point value Standard scientific notation 1.45E6 0.979e-3L L or l indicates long double floating point value When not specified, floating-point constants are of type double 10

Names Used to denote program values or components A valid name is a sequence of Letters (upper and lowercase) Digits A name cannot start with a digit Underscores A name should not normally start with an underscore Names are case sensitive MyObject is a different name than MYOBJECT There are two kinds of names Keywords Identifiers Keywords Keywords are words reserved as part of the language int, return, float, double They cannot be used by the programmer to name things They consist of lowercase letters only They have special meaning to the compiler 11

Identifiers Identifiers should be Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard abbreviations) Long enough to be understandable When using multiple word identifiers capitalize the first letter of each word Examples Min Temperature CameraAngle CurrentNbrPoints Definitions All objects that are used in a program must be defined An object definition specifies Type Name General definition form Known type List of one or more identifiers Type Id, Id,..., Id; Our convention is one definition per statement! 12

Examples char Response; int MinElement; float Score; float Temperature; int i; int n; char c; float x; Objects are uninitialized with this definition form (Value of a object is whatever is in its assigned memory location) Arithmetic Operators Common Addition + Subtraction - Multiplication * Division / Mod % Note No exponentiation operator Single division operator Write m*x + b not mx + b Operators are overloaded to work with more than one type of object 13

Integer Division Integer division produces an integer result Truncates the result Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3 Mod Produces the remainder of the division Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4 14

Operators and Precedence Consider mx + b Consider m*x + b which of the following is it equivalent to (m * x) + b m * (x + b) Operator precedence tells how to evaluate expressions Standard precedence order () Evaluate first, if nested innermost done first * / % Evaluate second. If there are several, then evaluate from left-to-right + - Evaluate third. If there are several, then evaluate from left-to-right Operator Precedence Examples 20-4 / 5 * 2 + 3 * 5 % 4 (4 / 5) ((4 / 5) * 2) ((4 / 5) * 2) (3 * 5) ((4 / 5) * 2) ((3 * 5) % 4) (20 -((4 / 5) * 2)) ((3 * 5) % 4) (20 -((4 / 5) * 2)) + ((3 * 5) % 4) 15

Defining and Initializing When an object is defined using the basic form, the memory allotted to it contains random information Better idea to specify its desired value at the same time Exception is when the next statement is an extraction for the object Remember our convention of one definition per statement! Examples int FahrenheitFreezing = 32; char FinalGrade = 'A'; cout << "Slope of line: "; float m; cin >> m; cout << "Intercept: "; float b; cin >> b; cout << "X value of interest: "; float x; cin >> x; float y = (m * x) + b; 16