CS149: Elements of Computer Science. Fundamental C++ objects

Similar documents
C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Data Types and Variables in C language

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Basic Elements of C. Staff Incharge: S.Sasirekha

Programming in C++ 4. The lexical basis of C++

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

VARIABLES AND CONSTANTS

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Programming in C++ 6. Floating point data types

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

Getting started with Java

BASIC ELEMENTS OF A COMPUTER PROGRAM

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Week 3 Lecture 2. Types Constants and Variables

Programming and Data Structures

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

1.1 Introduction to C Language. Department of CSE

Work relative to other classes

Datatypes, Variables, and Operations

Chapter 2 Elementary Programming

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

10/9/2012. Computers are machines that process data. assignment in C# Primitive Data Types. Creating and Running Your First C# Program

Basic Types, Variables, Literals, Constants

Lecture 2 Tao Wang 1

6.096 Introduction to C++ January (IAP) 2009

Fundamentals of C Programming

Variables and Literals

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Java Basic Datatypees

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

C Language, Token, Keywords, Constant, variable

LECTURE 02 INTRODUCTION TO C++

The C++ Language. Arizona State University 1

Chapter 2. Lexical Elements & Operators

CSc Introduction to Computing

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

CS102: Variables and Expressions

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Reserved Words and Identifiers

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

INTRODUCTION 1 AND REVIEW

Chapter 2: Introduction to C++

Number Systems, Scalar Types, and Input and Output

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

CSC 1107: Structured Programming

Introduction to C Language

Zheng-Liang Lu Java Programming 45 / 79

Basics of Java Programming

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Elementary Programming

Getting started with C++ (Part 2)

THE FUNDAMENTAL DATA TYPES

Fundamentals of Programming Session 4

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

BITG 1233: Introduction to C++

QUark Language Reference Manual

A Fast Review of C Essentials Part I

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

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter 2: Basic Elements of C++

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

Fundamentals of Programming

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Presented By : Gaurav Juneja

Computer Programming : C++

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

BSM540 Basics of C Language

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

EEE145 Computer Programming

Unit 3. Constants and Expressions

Basic Types and Formatted I/O

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Fundamental of Programming (C)

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Full file at

Fundamental of C programming. - Ompal Singh

Differentiate Between Keywords and Identifiers

Java Notes. 10th ICSE. Saravanan Ganesh

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

IT 1033: Fundamentals of Programming Data types & variables

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

4 Programming Fundamentals. Introduction to Programming 1 1

Computational Expression

CS242 COMPUTER PROGRAMMING

Chapter 2 THE STRUCTURE OF C LANGUAGE

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Transcription:

Fundamental C++ objects 1. Compiler needs to know in advance how to store different data types 2. Variable name + type, e.g. (price, integer) 3. Types: (a) Integers: short, long, signed (b) Floating Points: float, double (c) Character types 4. Declaration: before usage! (a) {type} variable name1, variable name2, etc.. (b) {type} variable name1 = value, variable name=value2, etc... (c) For example: double radius, diameter January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 1

Declaration: double d, r ; / / r a d i u s. cpp : C a l c u l a t e s r a d i u s from c i r c u m f e r e n c e # i n c l u d e<i o s t r e a m. h> # d e f i n e PI 3. 1 4 1 5 i n t main ( void ){ / / v a r i a b l e d e c l a r a t i o n double d, r ; / / i n p u t c i r c u m f e r e n c e c o u t << C i r c u m f e r e n c e? ; c i n >> d ; / / c a l c u l a t e r a d i u s r = d / ( 2 PI ) ; / / o u t p u t r a d i u s c o u t << Radius = << r << e n d l ; return 0 ; } January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 2

Integers 1. Denoted: int (a) short int (b) int (c) long int (d) signed int, unsigned int 2. Difference: number of bytes alloted 3. size: (a) shortint bytes int bytes longint bytes (b) Determined by compiler January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 3

/ / t e s t p o s s i b l e v a l u e s of i n t e g e r # i n c l u d e < i o s t r e a m> i n t main ( void ){ / / d e c l a r a t i o n s s h o r t i n t s h i ; i n t i ; long i n t l i ; signed i n t s i ; unsigned i n t u s i ; / / o u t p u t b y t e s i z e : c o u t << s h o r t i n t : << s i z e o f ( s h i ) <<, ; c o u t << i n t : << s i z e o f ( i ) <<, long i n t : ; c o u t << s i z e o f ( l i ) <<, << s i g n e d i n t : ; c o u t << s i z e o f ( s i ) <<, u n s i g n e d i n t : ; c o u t << s i z e o f ( u s i ) << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 4

/ / t e s t p o s s i b l e v a l u e s of i n t e g e r # i n c l u d e < i o s t r e a m> i n t main ( void ){ / / d e c l a r a t i o n s unsigned s h o r t i n t u s i ; signed s h o r t i n t s s i ; c o u t << Value f o r u n s i g n e d s h o r t i n t : ; c i n >> u s i ; c o u t << u s i v a l u e s t o r e d : << u s i << e n d l ; c o u t << Value f o r s i g n e d s h o r t i n t : ; c i n >> s s i ; c o u t << s s i v a l u e s t o r e d : << s s i << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 5

Characters 1. Denoted: char (a) Per definition: 1 byte (b) Underlying integer representation: i. a < b < c < d ii. A < B < C < 2. Example: char c = A January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 6

Bytes in char type / / o u t p u t s number of b y t e s i n c h a r # i n c l u d e<i o s t r e a m> i n t main ( void ){ char c ; c o u t << b y t e s i n c h a r : << s i z e o f ( c ) << e n d l ; return 0 ; } January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 7

char math / / o u t p u t s number of b y t e s i n c h a r # i n c l u d e<i o s t r e a m> i n t main ( void ){ char c1, c2 ; c1 = a ; c o u t << c1 = << c1 << e n d l ; c2 = c1 +256; c o u t << c2 = << c2 << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 8

Floating Point 1. Representation of numbers that have integer and fractional part, e.g. 3.14 2. Three types: (a) float (b) double (c) long double 3. Size: (a) float double long double (b) Determined by compiler January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 9

/ / o u t p u t s number of b y t e s i n f l o a t s # i n c l u d e<i o s t r e a m> i n t main ( void ){ f l o a t f ; double d ; long double l d ; c o u t << b y t e s i n f l o a t : << s i z e o f ( f ) ; c o u t << b y t e s i n double : << s i z e o f ( d ) ; c o u t << b y t e s i n long double : << s i z e o f ( l d ) << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 10

Types: 1. Integer: (a) int (b) short (c) long (d) signed, unsigned 2. Real numbers: (a) float, (b) double (c) long double 3. Characters: char Data types Some notes: 1. Different byte sizes 2. short int bytes int bytes long int bytes 3. float double long double 4. char: 1 byte January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 11

Data Objects 1. Programs deal with data objects, e.g. numbers, text, etc. 2. These can change, either by input or by computation within program 3. We need to store data, and be able to refer to it conveniently 4. Data Objects: (a) Have a name (b) A type (c) and a value (stored in memory location) 5. Name: used to identify specific data objects 6. Type: tells compiler what species data object is 7. Value: can be set to anything corresponding to type 8. Data objects: a polite way to say variable January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 12

A note on variable/object names Variable name: 1. Label or handle on value stored in a memory location 2. Compiler transparantly translates name to memory location and back Valid names: 1. Sequence of lower and uppercase letters and underscores 2. Name can not start with digit 3. Do not start with underscore or double underscore Examples: 1. Good: diameter2, radius2 Circle, Counter 2. Bad: 2Pac,?Value?, SS# January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 13

More restrictions Keywords: 1. Variable names can not be members of set of C++ keywords 2. Compiler needs to distinguish difference keywords and variables Good Practice: 1. Make your variable names descriptive and distinctive 2. Make them short to avoid typos and confusion 3. t, i, j, usually used as counters 4. Avoid inconsistent capitalization January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 14

Fundamental C++ object types 1. Compiler needs to know in advance how to store different data types 2. Variable name + type, e.g. (price, integer) 3. Types: (a) Integers: short, long, signed (b) Floating Points: float, double (c) Character types 4. Declaration: before usage! (a) {type} variable name1, variable name2, etc.. (b) {type} variable name1 = value, variable name=value2, etc... (c) For example: double radius, diameter January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 15

Declaration: double d, r ; / / r a d i u s. cpp : C a l c u l a t e s r a d i u s from c i r c u m f e r e n c e # i n c l u d e<i o s t r e a m. h> # d e f i n e PI 3. 1 4 1 5 i n t main ( void ){ / / v a r i a b l e d e c l a r a t i o n double d, r ; / / i n p u t c i r c u m f e r e n c e c o u t << C i r c u m f e r e n c e? ; c i n >> d ; / / c a l c u l a t e r a d i u s r = d / ( 2 PI ) ; / / o u t p u t r a d i u s c o u t << Radius = << r << e n d l ; return 0 ; } January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 16

Integers 1. Denoted: int (a) short int (b) int (c) long int (d) signed int, unsigned int 2. Difference: number of bytes alloted 3. size: (a) shortint bytes int bytes longint bytes (b) Determined by compiler January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 17

/ / t e s t p o s s i b l e v a l u e s of i n t e g e r # i n c l u d e < i o s t r e a m> i n t main ( void ){ / / d e c l a r a t i o n s s h o r t i n t s h i ; i n t i ; long i n t l i ; signed i n t s i ; unsigned i n t u s i ; / / o u t p u t b y t e s i z e : c o u t << s h o r t i n t : << s i z e o f ( s h i ) <<, ; c o u t << i n t : << s i z e o f ( i ) <<, long i n t : ; c o u t << s i z e o f ( l i ) <<, << s i g n e d i n t : ; c o u t << s i z e o f ( s i ) <<, u n s i g n e d i n t : ; c o u t << s i z e o f ( u s i ) << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 18

/ / t e s t p o s s i b l e v a l u e s of i n t e g e r # i n c l u d e < i o s t r e a m> i n t main ( void ){ / / d e c l a r a t i o n s unsigned s h o r t i n t u s i ; signed s h o r t i n t s s i ; c o u t << Value f o r u n s i g n e d s h o r t i n t : ; c i n >> u s i ; c o u t << u s i v a l u e s t o r e d : << u s i << e n d l ; c o u t << Value f o r s i g n e d s h o r t i n t : ; c i n >> s s i ; c o u t << s s i v a l u e s t o r e d : << s s i << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 19

Characters 1. Denoted: char (a) Per definition: 1 byte (b) Underlying integer representation: i. a < b < c < d ii. A < B < C < 2. Example: char c = A January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 20

Bytes in char type / / o u t p u t s number of b y t e s i n c h a r # i n c l u d e<i o s t r e a m> i n t main ( void ){ char c ; c o u t << b y t e s i n c h a r : << s i z e o f ( c ) << e n d l ; return 0 ; } January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 21

char math / / o u t p u t s number of b y t e s i n c h a r # i n c l u d e<i o s t r e a m> i n t main ( void ){ char c1, c2 ; c1 = a ; c o u t << c1 = << c1 << e n d l ; c2 = c1 +256; c o u t << c2 = << c2 << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 22

Floating Point 1. Representation of numbers that have integer and fractional part, e.g. 3.14 2. Three types: (a) float (b) double (c) long double 3. Size: (a) float double long double (b) Determined by compiler January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 23

/ / o u t p u t s number of b y t e s i n f l o a t s # i n c l u d e<i o s t r e a m> i n t main ( void ){ f l o a t f ; double d ; long double l d ; c o u t << b y t e s i n f l o a t : << s i z e o f ( f ) ; c o u t << b y t e s i n double : << s i z e o f ( d ) ; c o u t << b y t e s i n long double : << s i z e o f ( l d ) << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 24

Syntax and structure of C++ programs January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 25

Types: 1. Integer: (a) int (b) short (c) long (d) signed, unsigned 2. Real numbers: (a) float, (b) double (c) long double 3. Characters: char Data types Some notes: 1. Different byte sizes 2. short int bytes int bytes long int bytes 3. float double long double 4. char: 1 byte January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 26

Constants: Constants 1. Data objects or variables store changing values 2. This means we need to write down values and assign them to these variables on occasion 3. Need for notation standard Correspond to type: 1. String and character constants 2. Integer constants 3. Floating point constants January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 27

String and character constants 1. Sequence of zero or more characters enclosed in double quotes 2. Example: Hello World! 3. Size: (a) char: 1 byte (b) every string terminates with NULL char (o) (c) Length in Memory: number of chars + 1 January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 28

Escape characters 1. Purpose: (a) Escape mechanism: to include characters that do not have visible representation (b) To avoid confusion with language syntax (e.g. close string) 2. Non-exhaustive list: (a) New line: \n (b) Horizontal tab: \t (c) backslash: \\ (d) single quote: \ (e) double quote: \ January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 29

Example: / / program t o t e s t s t r i n g l i t e r a l s # i n c l u d e<i o s t r e a m> i n t main ( void ){ } c o u t << 1\ t 2 \ t 3 \n ; c o u t << 5\ t 6 \ t 7 \n ; c o u t << 8\ t 3 \ t 9 \n ; return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 30

1. Single character 2. Enclosed by single 3. Example: A, ;, \n Char constant Representation: / / program t o t e s t c h a r c o n s t a n t # i n c l u d e<i o s t r e a m> i n t main ( void ){ char c ; c = A ; c o u t << c << e n d l ; return 0 ; } January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 31

Integer Constant Denotes integer number: 1. Example: 23 or 45 or 78 or 1001 2. First must be non-zero for base 10: 3. Different bases (a) octal (base 8): start 0, e.g. 012 (10) (b) hexadecimal: start 0xFF (255) January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 32

Integer Constant Example / / program t o t e s t c h a r c o n s t a n t # i n c l u d e<i o s t r e a m> i n t main ( void ){ i n t i, o, hx ; i = 2 3 ; o =031; hx = 0 xff ; c o u t << dec = << i << e n d l ; c o u t << o c t = << o << e n d l ; c o u t << hexadec = << hx << e n d l ; return 0 ; } January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 33

Floating Point Constants Two methods to write out floating-point constants: 1. Digits.Digits{f,F,L,l} (a) Example: 2.34F, 3.1415L, 29.00l,.45f (b) double is default, f,f = float, l,l=long double 2. Scientific Notation: Digits.Digits{f,F,L,l} (a) Example: 234E-2, 31415E-4, 29.001E0 (b) Default is double, suffix f,f=double, l,l = long double January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 34

Floating Point Example / / t e s t f l o a t i n g p o i n t c o n s t a n t s # i n c l u d e<i o s t r e a m> i n t main ( void ){ f l o a t f1, f2 ; double d ; long double l d ; f1 = 2. 3 4 f ; f2 = 3 4 E 2F ; c o u t << f1 = << f1 << \ t << f2 = << f2 << \ n ; d = 3 1 4 1 5 9 2 E 6; c o u t << d= << d << \ n ; l d = 5. 3 1 1 0 2 0 L ; c o u t << l d = << l d << \ n ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 35

Floating Point 1. Representation of numbers that have integer and fractional part, e.g. 3.14 2. Three types: (a) float (b) double (c) long double 3. Size: (a) float double long double (b) Determined by compiler January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 36

/ / o u t p u t s number of b y t e s i n f l o a t s # i n c l u d e<i o s t r e a m> i n t main ( void ){ f l o a t f ; double d ; long double l d ; c o u t << b y t e s i n f l o a t : << s i z e o f ( f ) ; c o u t << b y t e s i n double : << s i z e o f ( d ) ; c o u t << b y t e s i n long double : << s i z e o f ( l d ) << e n d l ; } return 0 ; January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 37

Syntax and structure of C++ programs January 18, 2004 Johan Bollen - http://www.cs.odu.edu/ jbollen Page 38