Object Oriented Programming Chapter 3: Tokens, Expressions and Control structures

Similar documents
C++ Language Tutorial

C++ INTERVIEW QUESTIONS

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

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

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

Pemrograman Dasar. Basic Elements Of Java

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

The C Programming Language course syllabus associate level

Syllabus OBJECT ORIENTED PROGRAMMING C++

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

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

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

C++FA 5.1 PRACTICE MID-TERM EXAM

How to: Use Basic C++, Syntax and Operators

C++ Programming Language

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

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

MISRA-C:2012 Standards Model Summary for C / C++

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Objective-C Tutorial

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

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

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

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

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

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

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Stacks. Linear data structures

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

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

El Dorado Union High School District Educational Services

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

Binary storage of graphs and related data

C++FA 3.1 OPTIMIZING C++

Object Oriented Software Design

Keil C51 Cross Compiler

Introduction to Java

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Object Oriented Software Design

C++ Input/Output: Streams

Comparative Study of C, C++, C# and Java Programming Languages

Chapter 13 Storage classes

The programming language C. sws1 1

CORBA Programming with TAOX11. The C++11 CORBA Implementation

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

A brief introduction to C++ and Interfacing with Excel

: provid.ir

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

I PUC - Computer Science. Practical s Syllabus. Contents

Moving from C++ to VBA

Chapter 5 Functions. Introducing Functions

Comp151. Definitions & Declarations

Programming languages C

Object Oriented Software Design II

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

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

Formatting Numbers with C++ Output Streams

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Chapter 2 Introduction to Java programming

Moving from CS 61A Scheme to CS 61B Java

Java (12 Weeks) Introduction to Java Programming Language

Java Interview Questions and Answers

Appendix K Introduction to Microsoft Visual C++ 6.0

Qt Signals and Slots. Olivier Goffart. October 2013

Computer Programming Tutorial

C++ program structure Variable Declarations

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

Passing 1D arrays to functions.

C Programming Tutorial

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Applied Informatics C++ Coding Style Guide

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Tutorial on C Language Programming

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

Basics of I/O Streams and File I/O

JavaScript: Control Statements I

Crash Course in Java

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

While Loop. 6. Iteration

Variables, Constants, and Data Types

Chapter 1 Java Program Design and Development

Example of a Java program

13 Classes & Objects with Constructors/Destructors

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

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

VB.NET Programming Fundamentals

C++ Support for Abstract Data Types

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

J a v a Quiz (Unit 3, Test 0 Practice)

C++ Overloading, Constructors, Assignment operator

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Cobol. By: Steven Conner. COBOL, COmmon Business Oriented Language, one of the. oldest programming languages, was designed in the last six

C++ Essentials. Sharam Hekmat PragSoft Corporation

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

OpenOffice.org 3.2 BASIC Guide

Transcription:

Object Oriented Programming Chapter 3: Tokens, Expressions and Control structures Department of Computer Science & Engineering National Institute of Technology Srinagar 1

Tokens(1/4) The smallest individual units in a program are known as tokens. C++ has the following tokens: Keywords Identifiers Constants Strings operators 2

Tokens(2/4) Keywords: They are explicitly reserved words and cannot be used as names for program variables or other user defined program elements. Keywords in C++ asm, auto, bool, break, case, catch, char, class, const,const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while 3

Tokens(3/4) Identifiers and constants: Identifiers refer to the names of variables, functions, arrays, classes created by the programmer. Rules for identifiers Only alphabetic characters, digits and underscores are permitted. The name cannot start with digit Uppercase and lowercase letters are distinct. Keywords cannot be used. 4

Tokens(4/4) Constants: Constants refer to fixed values that don t change during the execution of a program. C++ supports several kinds of literal constants They include integers, characters, floating point numbers and strings. Examples: 123 //decimal constant 12.34 //floating point constant 037 //octal constant 0x2 //hexadecimal constant C++ //string constant a // character constant C++ also recognizes all the backslash character constants available in C. 5

C++ data types User defined data types Structure Union Class Enumerated type Built-in type Integral type int char Floating type float double void Derived type Array Function Pointer reference Basic data types(1/3) 6

Data Types(2/3) ANSI C++ committee has added two more data types, bool and wchar_t. Bool data type: The variable of type bool hold a boolean value, true or false. The bool type variables can be declared as: bool b1; b1=true; bool b2 = false; cout << b1 = << b1 << endl << b2 = << b2; Will display the following output: b1 = 1 b2 = 0 The wchar_t data type: The character type wchar_t has been defined to hold 16-bit wide characters. 7

Void type Data Types(3/3) The type void was introduced in ANSI C. Two uses of void are: To specify the return type of a function when it is not returning any value, and To indicate an empty argument list to a function. 8

Operators in C++(1/4) All C operators are valid in C++. C++ introduces some new operators which are as follows << insertion operator >> extraction operator :: scope resolution operator ::* pointer to member declarator ->* pointer-to-member operator.* pointer to member operator delete memory release operator endl line feed operator new memory allocation operator setw field width operator 9

Operators(2/4) Scope resolution operator( :: ) This operator allows to access the global version of a variable within the inner block. This operator is used as: :: variable-name #include<iostream> Using namespace std; int x = 11; // this x is global int main() { int x = 22; cout << "In main: x = " << x << endl; { int x = 33; cout << "In block inside main: x = " << x << endl; } // access to the global x cout << "In main: ::x = " << ::x << endl; return 0; } 10

Memory management operators New and delete operators Operators(3/4) An object can be created by using new, and destroyed by using delete A data object created inside a block with new, will remain in existence until it is explicitly destroyed by using delete. The new operator can be used to create objects of any type. It takes the following general form: Pointer-variable = new data-type; Pointer variable is a pointer of type data-type. The new operator allocates sufficient memory to hold a data object of type data-type and returns address of that object. Examples: int *p = new int; float q = new float; Initialization using new operator: Pointer-variable = new data-type(value); 11

Operators(4/4) delete operator: When data object is no longer needed, it is destroyed to release the memory space for reuse as : delete pointer-variable; The pointer-variable is the pointer that points to a data object created with new. Examples: delete p; delete q; 12

Manipulators Manipulators: Operators that are used to format the data display. Commonly used manipulators are endl and setw. The endl manipulator, when used in an output statement, causes a linefeed to be inserted. It has same effect as using the newline character \n. The setw manipulator specifies the field width for the output and print the result in right justified form. 13

Manipulators Example: Use of endl int main() { int m=1234,n=56,p=789; cout<< m = << m << endl << n = << n << endl << p = << p << endl return 0; } output: m = 1234 n = 56 p = 789 Use of setw int main() { int m=1234,n=56,p=789; cout<< m = <<setw(5)<< m << endl << n = << setw(5)<<n << endl << p = <<setw(5)<< p << endl // setw(5) specifies a field width 5 for printing the values return 0; } output: m = 1234 n = 56 p = 789 14

Expressions Expression is a combination of operators, constants and variables arranged as per the rules of the language. Every expression has a value A literal is an expression A variable is an expression One or more expression/s connected by an operator is an expression A function call is an expression 15

Types of expressions(1/3) Constant expressions Consist of only constant values Examples: 15 20+5/2.0 x Integral expressions Produce integer results after evaluation. Examples M M*n-5 M* x 5+int(2.0) Where m and n are integer variables 16

Types of expressions(2/3) Float expressions Produce floating point results after evaluation Examples: X+y x*y/10 5+float(10) 10.76 Where x and y are floating-point variables Pointer expressions Produce address values Examples: &m Ptr Ptr+1 Where m is a variable and ptr is a pointer 17

Types of expressions(3/3) Relational expressions: Produce results of type bool which takes a value true or false Examples: X <= y A+b = = c+d M+n > 100 Logical expressions: Logical expressions combine two or more relational expressions using logical operators and produce bool type results. Examples: A > b && x = = 10 X = = 10 y = = 5 Bitwise expressions: Used to manipulate data at bit level Examples: X << 3 //shift 3 bit positions to left Y>> 1 // shift B.Tech 1 bit CSE position NIT srinagarto right 18

Operator overloading Means assigning different meanings to operators. Examples: The operator * when applied to a pointer variable, gives the value pointed to by the pointer. But it is also commonly used for multiplying two numbers. The operators << and >> are used for input and output operations and are also used for shifting of bits. Almost all C++ operators can be overloaded with a few exceptions such as the member-access operators(. And.*), conditional operator(?:), scope resolution operator (::) and the size operator (sizeof). NOTE: this topic will be discussed later in detail. 19

Control structures Control structures(1/5) Branching structure If-else statement Switch statement Loop structure for loop while loop do-while loop 20

Control structures(2/5) If-else statement Flow chart Syntax: if (condition) { } if block statements; True conditi on False else { else block statements; If block statements else block statements } 21

Switch statement: Syntax: switch (expression) { case const1: action1; break; case const2: action2; break; case const3: action3; break;... default: actionn; } Control structures(3/5) 22

Control structures(4/5) Iterative constructs Iterative construct means that some statements will be executed multiple times until some condition is met. while statements Syntax: while (condition) { subtask; } do-while statement Syntax: do { Subtask; } while (condition); 23

Control structures(5/5) for statement Syntax: for (initilization_expression; test_expression; updation_expression) { subtask; } 24