Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++.

Similar presentations


Presentation on theme: "Introduction to C++."— Presentation transcript:

1 Introduction to C++

2 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.

3 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

4 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

5 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.

6 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++.

7 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

8 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

9 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.

10 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 - ? :

11 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.

12 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

13 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.

14 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.

15 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;

16 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?

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

18 Compilation : ALT + F9

19 Execution : Ctrl + F9

20 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 */

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

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

23 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.

24 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

25 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

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

27 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;

28 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(); }

29

30

31

32 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.

33 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

34 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.

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

36 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 _ _ _ _ _

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

38 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

39 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 { }


Download ppt "Introduction to C++."

Similar presentations


Ads by Google