Input And Output of C++

Similar documents
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

14. Other Data Types. Compound Data Types: Defined data types (typedef) Unions typedef existing_type new_type_name ;

CHAPTER 3 BASIC INSTRUCTION OF C++

The C++ Language. Arizona State University 1

Pointers, Dynamic Data, and Reference Types

Chapter 2: Introduction to C++

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

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

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

UNIT- 3 Introduction to C++

A First Program - Greeting.cpp

6.096 Introduction to C++ January (IAP) 2009

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

Chapter-8 DATA TYPES. Introduction. Variable:

Objectives. In this chapter, you will:

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

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

LECTURE 02 INTRODUCTION TO C++

Computer Programming : C++

Tokens, Expressions and Control Structures

Variables. Data Types.

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

2 nd Week Lecture Notes

CS242 COMPUTER PROGRAMMING

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

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

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Fundamentals of Programming CS-110. Lecture 2

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

CS 376b Computer Vision

CSCE 110 PROGRAMMING FUNDAMENTALS

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.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

INTRODUCTION 1 AND REVIEW

UNIT-2 Introduction to C++

BITG 1233: Introduction to C++

CSc Introduction to Computing

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

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

Short Notes of CS201

Chapter 4 - Notes Control Structures I (Selection)

Programming with C++ Language

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

CS201 - Introduction to Programming Glossary By

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Introduction to Programming EC-105. Lecture 2

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

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

Getting started with C++ (Part 2)

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

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

EC 413 Computer Organization

VARIABLES AND CONSTANTS

Introduction to C++ with content from

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

C++ basics Getting started with, and Data Types.

CSCI 1061U Programming Workshop 2. C++ Basics

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

DEPARTMENT OF MATHS, MJ COLLEGE

Lecture 2 Tao Wang 1

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

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

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Pointers, Arrays and C-Strings

FORM 2 (Please put your name and form # on the scantron!!!!)

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

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

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

Variables and Constants

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Programming. C++ Basics

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

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

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

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Chapter 2 Basic Elements of C++

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Homework #3 CS2255 Fall 2012

Computer Systems Principles. C Pointers

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

CS2255 HOMEWORK #1 Fall 2012

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

INTRODUCTION TO PROGRAMMING

C++ PROGRAMMING BASICS

CS Spring 05 - MidTerm

EEE145 Computer Programming

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

Introduction to Programming

Scientific Computing

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

WARM UP LESSONS BARE BASICS

Transcription:

Input And Output of C++

Input And Output of C++

Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout << "Hello World\n"; Sends string "Hello World" to display, & escape sequence "\n", skipping to next line cout << "Hello World" << endl; Same result as above

Input Using cin cin for input Differences: ">>" (extraction operator) Object name "cin" used instead of "cout" No literals allowed for cin Must input "to a variable" cin >> num; Waits on-screen for keyboard entry Value entered at keyboard is "assigned" to num

First C++ program Greeting.cpp Preprocessor directives // Program: Display greetings #include <iostream> #include <string> Comments Function named main() indicates start of program int main() { cout << "Hello world!" << endl; return 0; } Ends executions of main() which ends program Insertion statement

Output

C++ Data Types Data types are means to identify the type of data and associated operations of handling it. C++ provides a predefined set of data types for handling the data it uses. When variables are declared of a particular data type then the variable becomes the place where the data is stored and data types is the type of value(data) stored by that variable. Data can be of many types such as character, integer, real etc. since the data to be dealt with are of many types A programming language must provide different data types.

FUNDAMENTAL DATA TYPES DATA TYPES DATA TYPE MODIFIERS DERIVED DATA TYPES USER DEFINED DATA TYPES

INTEGER CHARACTER WIDE CHARACTER FUNDAMENTAL DATA TYPES FLOAT FUNDAMENTAL DATA TYPES ARE THOSE THAT ARE NOT COMPOSED OF OTHER DATA TYPES DOUBLE BOOL VOID

INTEGER DATA TYPE.. 2 bytes Integers are whole numbers with a machine dependent range of values. A good programming language as to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms. A short int requires half the space than normal integer values. Unsigned numbers are always positive and consume all the bits for the magnitude of the number. T he long and unsigned integers are used to declare a longer range of values.

FLOAT DATA TYPE 4 bytes A number having fractional part is a floating- point number. An identifier declared as float becomes a floating-point variable and can hold floatingpoint numbers. floating point variables represent real numbers. They have two advantages over integer data types:- They can represent values between integers. They can represent a much greater range of values. they have one disadvantage also, that is their operations are usually slower.

DOUBLE DATA TYPE 8 bytes The data type double is also used for handling floatingpoint numbers. It is treated as a distinct data type because, it occupies twice as much memory as type float, and stores floatingpoint numbers with much larger range and precision. It is slower than type float.

VOID DATA TYPE 1byte in GCC Compiler otherwise depends upon compiler It specifies an empty set of values. It is used as the return type for functions that do not return a value. No object of type void may be declared. It is used when program or calculation does not require any value but the syntax needs it.

Bool DATA TYPE 1 byte it is an additional data type for representing a Boolean value. A variable associated with a bool data type may be assigned an integer value 1 to the literal true or a value 0 to the literal false.

WIDE CHARACTER DATA TYPE.2 or 4 bytes Wide character is a computer character data type It has generally size more than 8- bit character The increased size allows use of larger code character sets

Area.cpp #include <iostream> int main() { // Extract length and width cout << "Rectangle dimensions: "; float Length; float Width; cin >> Length >> Width; // Compute and insert the area } float Area = Length * Width; cout << "Area = " << Area << " = Length " << Length << " * Width " << Width << endl; return 0;

INTEGER TYPE MODIFIERS DATA TYPE MODIFIERS CHARACTER TYPE MODIFIERS THEY CHANGE SOME PROPERTIES OF THE DATA TYPE FLOATING-POINT MODIFIERS

INTEGER TYPE MODIFIERS C++ offers three types of integer data type:- 1:- short integer- at least two bytes. 2:- int integer at least as big as short. 3:- long integer-at least four bytes. the prefix signed makes the integer type hold negative values also. Unsigned makes the integer not to hold negative values.

TYPE APPROXIMATE SIZE MINIMAL RANGE SHORT 2-32768 to 32767 UNSIGNED SHORT 2 0 to 65,535 SIGNED SHORT 2 Same as short INT 2-32768 to 32767 UNSIGNED INT 2 0 to 65,535 SIGNED INT 2 Same as int LONG 4-2,147,483,648 TO 2,147,483,647 UNSIGNED LONG 4 0 to 4,294,967,295 SIGNED LONG 4 Same as long

CHARACTER TYPE MODIFIER The char can also be signed or unsigned. unlike int,char is not signed or unsigned by default. It is later modified to best fit the type to the hardware properties. TYPE APPROXIMATE SIZE MINIMAL RANGE CHAR 1-128 to 127 UNSIGNED CHAR 1 0 to 255 SIGNED CHAR 1 Same as char

FLOATING POINT TYPE MODIFIERS There are three floating-point types: float, double, and long double. These types represent minimum allowable range of types. Note:- don t use commas in numeric values assigned to variables. TYPE APPROXIMATE SIZE FLOAT 4 7 LONG DOUBLE 8 15 LONG DOUBLE 10 19 DIGITS OF PRECISION

Name Description Size* Range* char Character or small integer. 1byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer. 2bytes int Integer. 4bytes long int (long) Long integer. 4bytes signed: -32768 to 32767 unsigned: 0 to 65535 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits) long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)

ARRAYS FUNCTIONS DERIVED DATA TYPES POINTERS REFERENCES CONSTANTS

ARRAYS An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. For example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier. Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [elements]; NOTE: The elements field within brackets [ ] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution.

VALID OPERATIONS WITH ARRAYS billy[0] = a; billy[a] = 75; b = billy [a+2]; billy[billy[a]] = billy[2] + 5;

PROGRAM: ARRAYS EXAMPLE #include <iostream> using namespace std; int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0; } OUTPUT:- 12206

POINTERS Variable that points to a memory location It holds the address of another variable The general form of declaring the pointer is type*ptr; Example : int *ptr;

REFERENCES It is an alternative name for an object. A reference variable provides an alias for a previously defined variable. It s declaration consists of a base type, an &(ampersand), a reference variable name equated to a variable name. the general form of declaring is:- type &ref-var = var-name; Example : int x ; int & r = x;

CLASS STRUCTURE USER DEFINED DERIVED DATA TYPES UNION ENUMERATION

CLASS Class: A class is a collection of variables and function under one reference name. it is the way of separating and storing similar data together. Member functions are often the means of accessing, modifying and operating the data members (i.e. variables). It is one of the most important features of C++ since OOP is usually implemented through the use of classes.

CLASS Classes are generally declared using the keyword class, with the following format: class class_name { access_specifier_1: member1; access_specifier_2: member2;... } object_names;

STRUCTURES A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures are declared in C++ using the following syntax: struct structure_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3;.. } object_names; where structure_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name.

STRUCTURES Structure is different from an array in the sense that an array represents an aggregate of elements of same type whereas a structure represents an aggregate of elements of arbitrary types..

UNION Unions allow one same portion of memory to be accessed as different data types, since all of them are in fact the same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally different: union union_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; } object_names;

UNION All the elements of the union declaration occupy the same physical space in memory. Its size is the one of the greatest element of the declaration. All of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent of each other. One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. The exact alignment and order of the members of a union in memory is platform dependent. Therefore be aware of possible portability issues with this type of use.

ENUMERATION An enumeration is a set of named integer constants that specify all the permissible values that can be assigned to enumeration variables. These set of permissible values are known as enumerators. Declaring an enum type enum country {US, UN, India, China}; In this statement, an enumeration data-type country (country is a tag name), consisting of enumerators US, UN and so on, is declared. Note that these enumerators represent integer values, so any arithmetic operation can be performed on them.

ENUMERATION By default, the first enumerator in the enumeration data type is assigned the value zero. The value of subsequent enumerators is one greater than the value of previous enumerator. Hence, the value of US is 0, value of UN is 1 and so on. However, these default integer values can be overridden by assigning values explicitly to the enumerators As shown here. enum country {US, UN=3, India, china} ; In this declaration, the value of US is O by default, the value of UN is 3, India is 4 and soon.

ENUMERATION Once an enum type is declared, its variables can be declared using this statement. country countryl, country2; These variables countryl, country2 can be assigned any of the values specified in enum declaration only. For example, consider these statements. countryl = India; // valid country2 = Japan; // invalid Though the enumerations are treated as integers internally in C++, the compiler issues a warning, if an int value is assigned to an enum type. For example, consider these statements. country1 = 3; //warning country1 = UN; / /valid country1 = (country) 3; / /valid

ENUMERATION C++ also allows creating special type of enums known as anonymous enums, that is, enums without using tag name as shown in this statement. enum {US, UN=3, India, China}; The enumerators of an anonymous enum can be used directly in the program as shown here. int count = US;

C++ Statements C++ stmts Exp stmt Labelled stmt Guarding stmt Control stmt Selection stmt Iteration stmt Jump stmt If If else switch while do for break contin ue return