CS2141 Software Development using C/C++ C++ Basics

Similar documents
Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

C++ Programming: From Problem Analysis to Program Design, Third Edition

A First Program - Greeting.cpp

Objectives. In this chapter, you will:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Chapter 2: Introduction to C++

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Integer Data Types. Data Type. Data Types. int, short int, long int

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

LECTURE 02 INTRODUCTION TO C++

The C++ Language. Arizona State University 1

UNIT- 3 Introduction to C++

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to Programming using C++

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Zheng-Liang Lu Java Programming 45 / 79

Fundamentals of Programming CS-110. Lecture 2

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

CS242 COMPUTER PROGRAMMING

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lab # 02. Basic Elements of C++ _ Part1

CSc Introduction to Computing

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CS 376b Computer Vision

Input And Output of C++

Chapter 1 INTRODUCTION

Introduction to Programming EC-105. Lecture 2

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

C++ Programming for Non-C Programmers. Supplement

6.096 Introduction to C++ January (IAP) 2009

CSCE 110 PROGRAMMING FUNDAMENTALS

CS201- Introduction to Programming Current Quizzes

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

BITG 1233: Introduction to C++

Homework #3 CS2255 Fall 2012

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Computer Programming : C++

C++ Programming for Non-C Programmers. Supplement

Programming Fundamentals and Methodology. COMP104: C++ Basics

WARM UP LESSONS BARE BASICS

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

1. In C++, reserved words are the same as predefined identifiers. a. True

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Getting started with C++ (Part 2)

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Chapter 2. Procedural Programming

OBJECT ORIENTED PROGRAMMING

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction to C++ Systems Programming

Review of Important Topics in CS1600. Functions Arrays C-strings

BASIC ELEMENTS OF A COMPUTER PROGRAM

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CHAPTER 3 Expressions, Functions, Output

Final CSE 131B Spring 2004

Your first C++ program

REVIEW. The C++ Programming Language. CS 151 Review #2

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Chapter 15 - C++ As A "Better C"

Programming. C++ Basics

Creating a C++ Program

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Chapter 7. Additional Control Structures

Unit 3. Constants and Expressions

Introduction to C ++

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Module 2 Introducing Data Types and Operators

Lecture 3. More About C

Work relative to other classes

CE221 Programming in C++ Part 1 Introduction

Lab Instructor : Jean Lai

CSE 12 Spring 2016 Week One, Lecture Two

CSE 12 Spring 2018 Week One, Lecture Two

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CS201 - Introduction to Programming Glossary By

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Increment and the While. Class 15

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

A flow chart is a graphical or symbolic representation of a process.

Lecture 2 Tao Wang 1

Chapter 2 Basic Elements of C++

A SHORT COURSE ON C++

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Boolean Algebra Boolean Algebra

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 3. Numeric Types, Expressions, and Output

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Transcription:

CS2141 Software Development using C/C++ C++ Basics

Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short <= int <= long They could all be the same size Commonly at least two of them are the same size The sizeof operator can be used to find out the size: cout << A short int is << sizeof( short int ) << bytes << endl; C++ Basics 2

Integers Signed and Unsigned Integers can also be signed or unsigned Unsigned integers use the sign bit for the number An unsigned int can only hold positive numbers An unsigned int will hold a bigger positive number than a signed int Integers are signed by default An unsigned long int holds the largest positive integer value A signed short int is the shortest C++ Basics 3

Integers Division and Modulus C++ leaves the outcome of a few operations up to the platform Integer division and modulus with negative operands are two of those unspecified operations -23 / 4 could be -5 or -6-23 % 4 could be -3 or 1 It will always be true that: a == (a / b) * b + a % b C++ Basics 4

Characters Basic Types A char is typically only 8 bits C++ only defines a minimum length, so longer characters are allowed A w_char is longer than a char, usually the same as a short Characters can be signed or unsigned char is unsigned by default signed char can be used to store small integers C++ Basics 5

Using Characters Characters can be used in arithmetic expressions: char c = ' ' + '!'; // c will be 'A' int x = '9' - '0'; // x will be 9 There are many ways to represent a character: A character: 'n' ASCII: '\156' Hexadecimal (note the 0x prefix): '\0x6e' An integer: 110 Strings are often stored as an array of characters, terminated by a '\0' (the null character). C++ Basics 6

Booleans A bool is a single bit: 1 for true 0 for false A bool can be used as an integer: bool test = true; int i = 2 + test; test = test - 1; The bool type is relatively new to C++. There used to be various competing designs, which might be encountered in older code. C++ Basics 7

Using Integers as Booleans Integers are often used as a boolean type: Zero is false Any other value is true int i = 10; while( i ) { // Do something // until i is 0 i--; } C++ Basics 8

Real Numbers Basic Types Can be float, double, or long double float is the smallest long double is the biggest Most math libraries use doubles, so it is better to use double rather than float Some platforms may provide values like Nan, NEGATIVE_INFINITY, and POSITIVE_INFINITY, but they are not required by the language C++ Basics 9

Conversion Between Data Types C++ will convert operand data types if necessary: int i; double d = 3.14159; i = d; // May create a warning, // but it will work. i = (int)d; // Use cast to avoid warning. Be aware of data types in expressions: int a = 3; int b = 2; float c = (a + b) / 2; // 2.0, not 2.5 C++ Basics 10

Enumerations An enumeration creates a distinct integer type with named values: enum color { red, orange, yellow }; color bgcolor = red; if( bgcolor == orange )... Each of the names can only be used once in any specific namespace: // This will cause an error enum fruit { apple, pear, orange }; Integer values can be specified. If a value is not provided, the previous value is incremented: enum axes { X = 0, Y = 1, Z = 2 }; enum letters { A = 0, B, C }; C++ Basics 11

Basic Stream I/O #include <iostream> // I/O function definitions using namespace std; int a, b; // Variable declarations // Basic integer input cin >> a >> b; // Basic string output cout << Hello world << endl; cout << a << + << b << = << a + b << endl; C++ Basics 12

Declaring Arrays Arrays are declared by the name and the number of elements The new directive does not have to be used to allocate an array The number of elements can be omitted if there is a way for the compiler to determine it int data[100]; char text[] = This is an example of an array ; int evens[] = {2, 4, 6, 8, 10, 12, 16, 18}; C++ arrays do not know their own size, you must keep track of it yourself C++ Basics 13

Using Arrays The number of elements can also be omitted if the array is passed as an argument to a function: double average( int n, double data[] ) { double sum = 0; for( int i = 0; i < n; i++ ) { sum += data[i]; } return sum / n; } Notice that the parameter n is used to pass the size of the array C++ Basics 14

Working with Objects Consider the following Java code: public class TestClass { public int value; } public static void main( String[] args ) { TestClass obj1 = new TestClass( ); TestClass obj2; obj1.value = 12; obj2 = obj1; obj1.value = 18; System.out.println( obj1 value + obj1.value ); System.out.println( obj2 value + obj2.value ); } C++ Basics 15

Working with Objects cont. Java uses reference sematics for assignments, so when the code is run, both obj1 and obj2 are variables that end up referring to the same object: C++ Basics 16

Working with Objects cont. Now consider the C++ version: class TestClass { public: int value; }; int main( ) { TestClass obj1; TestClass obj2; obj1.value = 12; obj2 = obj1; obj1.value = 18; cout << obj1 value << obj1.value << endl; cout << obj2 value << obj2.value << endl; } C++ Basics 17

Working with Objects cont. C++ uses copy semantics for assignments, so when the code is run obj1 and obj2 are two different objects with different values: C++ Basics 18

Working with Objects cont. Difference: In the Java version, obj1 and obj2 are references to a TestClass object In the C++ version, obj1 and obj2 are TestClass objects If access by reference is needed, it is left to the programmer in C++ (more on references later): TestClass obj1; obj1.value = 12; TestClass & obj2 = obj1; obj1.value = 18; C++ Basics 19

Function Definitions C++ allows functions to be defined outside of classes. These are called global functions Functions are invoked by using their name. int max( int i, int j ) { if( i < j ) return j; return i; } int x = 283; int y = 482; int z = max( x, y ); C++ Basics 20

Function prototypes A function prototype simply defines the name and argument types of a function There is no function body Argument names can be used but are not required Prototypes are necessary because the compiler must know a function exists before the function can be invoked The prototype for the max function would be: int max( int, int ); C++ Basics 21

The main function Execution of a C++ program begins in the function main main is not part of any class It should not be declared static The return type must be int Older compilers might accept void Returning zero means successful completion The meaning of other values is up to the compiler or even the programmer C++ Basics 22

The main function cont. There can be zero or two parameters: Zero parameters: int main( ) Two parameters: int main( int argc, char ** argv ) The first parameter is an integer passing the number of arguments to the program. The second parameter is an array of strings passing any command-line arguments to the program. The first argument to a C++ program (argv[0]) is always the program name. C++ Basics 23

Hello World - revisited #include <iostream> using namespace std; int main( int argc, char ** argv ) { cout << Hello World! << endl; cout << From << argv[0] << endl; return 0; } C++ Basics 24