Introduction to C++.

Slides:



Advertisements
Similar presentations
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Advertisements

Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
CHAPTER-6 GETTING STARTED WITH C++ Presented by: Nripendra Kumar PGT (Computer Science) K V Aliganj 2 nd Shift.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Instructor - C. BoyleFall Semester
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Bill Tucker Austin Community College COSC 1315
Numbers in ‘C’ Two general categories: Integers Floats
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Computer Programming BCT 1113
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computing Fundamentals
Basic Elements of C++.
C Language VIVA Questions with Answers
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
Introduction to C Programming
DATA HANDLING.
Basic Elements of C++ Chapter 2.
Visit for more Learning Resources
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
Chapter 2: Basic Elements of Java
Programming Funamental slides
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
elementary programming
CHAPTER 2: COMPONENTS OF PROGRAMMING LANGUAGE
Chapter 2: Introduction to C++.
WEEK-2.
Fundamental Programming
C++ Programming Basics
Course Outcomes of Programming In C (PIC) (17212, C203):
Introduction to C Programming
Presentation transcript:

Introduction to C++

Learning Objectives To make the students familiar with C++ character set, operators etc. To make them understand the basic structure of a C++ program. To enable them to write simple programs. To enable the students to understand the concept of data types.

Character Set Character set is a set of valid characters that a language can recognize. The C++ has the following Character set. Letters A to Z and a to z Digits 0 to 9 Special Symbols - Space, +, -, *, (), {}, [], etc. White Space - Blank space, tab, carriage return, new line, form feed etc. And Many more

Tokens The smallest individual unit in a program is known as a token or a lexical unit. C++ has the following tokens Keywords Identifier Literals Punctuators Operators

Keywords Keywords are those which have a predefined meaning in the language. There are 48 keywords and all these keywords cannot be used as user defined words. Out of the 48 keywords 32 keywords are inherited from C language.

Identifiers Identifier is the fundamental of building blocks of a program and are used in general terminology for the names given to different parts of the program such as. Variables, objects, classes, functions etc. C/C++ is a case sensitive and its treats upper case and lower case differently. Like ‘MYFILE’ and ‘Myfile’ are two different identifiers in C++.

Literals Literals are often known as constant, these are the items that never change their values during program execution. C++ allows several kinds of literals Integer constant Character constant Floating Constant String Constant

Escape Sequence in C++ \a Audible bell \b Backspace \f Form feed \n New line \r Carriage Return \t Horizontal Tab \v Vertical Tab \\ Print ‘\’ as message \’ Print ‘ as message \” Print “ as message \? Print ? As Message \0 Null Character

Punctuators Brackets [] – for single and multidimensional array Parenthesis ( ) – Use for function call and definition Braces { } – Use for open and close any compound of the program. Comma , - Use as separator in a function argument. Semicolon ; - To terminate the statement Colon : - Indicate the labeled statement.

Operators Binary Operator – Are those operators that require two operands to operate upon + - * / % etc Unary Operator – Those Operator which have one side operand. ++ , -- Ternary Operator - ? :

Unary Operator Increment Operator ++ Decrement Operator – These are use two types Pre and Post Here alone a++ and ++a are same meaning But a = b++ and a = ++b and different meaning Let b =5 then in pre increment a will be 6 and in post increment a will be 5.

Examples Let a =5 and b = 6 Then find the value of k , a and b after execution ? k = a++ * ++a * ++a – ++a * ++b * b++ The Answer is a = 9 b = 8 and k = 120 Let a = 5 and what is output of the followings cout<<++a * a++ * ++a; Output is : 288

Ternary Operator What is the value of k K = (30>45?90:80) K will be 80 Similarly using nested case k = (300>85?(80>70?60:50):(90<50:40:30)) Here k will be 60.

Compiler Complier is the system software that converts high level language into machine language. It compiles the program at one go and if any error(s) is found, they are listed at the end with line number and a meaningful message. A program can be executed only when all the errors are rectified.

I/O Operation Output Operator “<<“ called stream insertion operator is used to direct a value to standard output. cout<<“The sum is “; cout<<s; Input Operator “>>” known as stream extraction operator is used to read a value from standard input. cin>>var_name;

Quick Recap What is a Keyword? How are input/output operations performed in C++? What do you understand by the term compiler? What are identifiers?

A first look at C++ program #include<iostream.h> void main() { cout<<“Welcome to C++ program “; }

Compilation : ALT + F9

Execution : Ctrl + F9

Comments Comments are pieces of code that the compiler discards or ignores or simply does not execute. There are two ways to insert comments in C++ : Single line comment // Multi line comment /* and */

SINGLE LINE COMMENT #include<iostream.h> void main() { // my first program cout<<“Welcome to C++ program “; }

MULTIPLE LINE COMMENT #include<iostream.h> void main() { /*This my first program using C++ */ cout<<“Welcome to C++ program “; }

Variables Variable represent named storage locations, whose values can be manipulated during program run. There are two values associated with a variable lvalue rvalue Lvalue means the location (address) – where it is stored Rvalue - what is stored.

C++ Data Type Data types are means to identify the type of data and associated operation of handling it Fundamental Data Types Derived Data Types User Defined Data Types

Fundamental Data Types Fundamental datatypes are those that are not composed of other data types. int – used for storing whole number char – used for storing characters float – for numbers with fractional parts

Declaration of Variable Syntax Type_name <variable list separated by comma> Example int a; or float x, y, z;

Initialization of Variable Initialization of variable means, giving a value to the variable at the time of declaration. For Ex. int x = 30; // x assigned value at the time of declaration. Now Dynamic initialization is also possible – When we declare a variable and value is given by any expression or any existing variable. Ex. float avg = sum / count;

Program to perform input/output #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<“Enter two values”; cin>>a>>b; c=a+b; cout<<“a+b =“<<c; getch(); }

Errors Syntax Error – Syntax error occur when grammatical rules of a programming language are voilated. Semantics Error - Semantic error occur when statements are not meaningful. Type Error - When data in a program mismatch then this error will occurs. Run time Error – Run time error occurs during execution of program. It is also called “Exceptions” – It is the causes of some illegal operation takes place. Logical Errors - This type of error also known as ‘Bug’. Due to this types of error - programs run successfully but desired output is not obtained.

Derived Data Types Arrays – Collection of similar data types Functions – a named part of the program that can be invoked from other parts of the program. Pointers – is a variable that hold the address of another cell in the memory References – is an alternative name for an object Constant – used for declaring a constant, whose value cannot be changed

User Defined Derived Data Type There are some derived data types that are defined by the user. These are Class – A class represent a group of similar object. Structure – A structure is a collection of variables under one name providing a convenient means of keeping related information. Union – A union is a memory location that is shared by two or more different variables. Generally of different types at different times. Enumeration – It is an alternative method for naming integer constant if often more convenient than const.

Formatting Output Formatting output is important in the development of output screen. Two Manipulators are setw() and setprecision() Header file for above – iomanip.h

setw( ) This manipulator sets the width of the field assigned for the output. It takes the size of the field as a paratmeter. cout<< setw(6) << “R”; then output will R _ _ _ _ _

setprecision( ) This manipulator can also be used to “Set the number of decimal places to be displayed. cout<< setprecision(4)<< 12.658425 output will 12.6584

Why header files? Every program begins with header file inclusion ie., #include<iostream.h> Why should I include the header file? The header file iostream.h contains the definition for the input/output operations performed. Why do we use the # symbol? The # symbol tells the compiler that it is a pre-processor directive. That is, prior to compilation include the file iostream.h

The main() function The main() is the point from where every C++ program begins its execution. If there is no main() in a program, it cannot be executed. Every function in C++ has its code enclosed in { }