Introduction to C++ Lecture Notes

C Programming Lecture Notes

C++ is an object oriented programming language developed by Bjarne Stroustrup in 1980. The language is developed in Bell laboratories. The main characters of object oriented programming language are encapsulation, abstraction, inheritance and polymorphism.

Tokens

The smallest individual unit in a program is called token. Tokens are classified into different types such as: Keyword, identifiers, literal, punctuator, and operator.

Keyword

Keywords implement specific C++ features. They are explicitly reserved word. The reserved word used in a program is called keyword. Keywords are not used for declaring normal identifier. Example: if, else, int, void, throw.

Identifier

Identifier refers to the names of variable, function, array, class etc. They are the fundamental requirement of any language. Each language has its own rules for declaring an identifier. The fundamental building block of a program is called identifier. ie, Identifiers are used for declaring variable, object etc.

Literal

The data item that never changes their value, is called literal or constant. Literals are classified into three types such as:

a) Numeric Constant

A constant or literal which contains only digit (with or without fractional part) is called numeric constant. The numeric constants are classified into two-integer constant and floating point constant.

b) Character Constant

A single character enclosed in single quotes is called charcter constant. Example ‘A’.

c) String Constant

A single character or a number of characters enclosed in double quotes is called string constant. Example “Compter”. In the case of string constant, there is an escape character, which is automatically added to the end of the string for representing the end of the string.

Punctuator

The special symbols used for special purpose is called a punctuator. The following are commonly used punctuator in C++.

a) Open and closed round brackets () , indicates the function and function call.
b) Semicolan ; is used to indicate the end of a statement
c) Open and closed curly brackets {} , indicates the compound bracket
d) Hash # , is used for Pre-processor directive.
e) Square brackets [] is used to specify an array.

Operator

Operators are symbols that are used to indicate specified operation. They are classified into two different categories such as

a) Unary operator- The operator that acts on only one operand is called unary operator. Commonly used unary operators are unary plus and unary minus. Example: +a, -a.

b) Binary operator- The operator that acts an two operands is called binary operator. Commonly used binary operators are   Relational operator, arithmetic operator and logical operator.

a) Arithmetic Operator

The operator that is used to indicate some arithmetical operation is called arithmetic operator. The operator that is used to indicate arithmetical sum is called addition operator. The addition operator is used to indicate the symbol +. The subtraction operator is used to indicate the difference of two operands. It is represented with the use of (-) . The operator that indicates the division of two operands is called division operator. It is symbolically represented by /. An operator that is used to find the product of two operands is called multiplication operator. The symbol used to indicate multiplication operator is *. The operator that is used to find modulus of the first operand relative to the second operand is called modulus operator. It is indicated as %.

b) Relational operator

The operator that is used to indicate the relationship among several operands is called relational operator. It is used for comparing different operands. C++ provides 5 major relational operators. They are: >, <, <=, >=, == 

c) Logical Operator

The operator that is used to connect different relational operators is called logical operator. C++ provides three different types of logical operators such as AND operator, OR operator and NOT operator. Logical AND operator is represented with the use of the symbol (&&). The logical AND operator produces true output, when all the inputs are true otherwise it produces false output. Logical OR operator is represented using the symbol II. Logical OR operator produces true outputs when one of the conditions id True. Otherwise it produces false output. Logical NOT operator is represented with the use of the symbol ! . It is a unary operator, that is, it acts as only one operand.

d) Increment and Decrement operator

Instead of the above operators, C++ provides two operators named increment and decrement operators. The increment operator (++) adds one to the variable and decrement operator (--) subtracts one from the variable. Eg: A++, A--

e) Conditional Operators

C++ also offers an operator named conditional operator (?:) that stores a value depending upon the condition. The conditional operators are ternary operators that require 3 operands.
Syntax: Expression ? expression : expression

Eg: (a>b)? large=a:large=b, where the condition is true the variable large will hold the value a and otherwise it will hold the value b.

No comments:

Post a Comment