Sunday, January 16, 2022

Class--11 | C++ Concept | Computer Science!

Overview of C++

KEY POINTS:

Introduction to C++

 C++ is the successor of C language & developed by Bjarne Stroustrup at Bell Laboratories,

New Jersey in 1979.

Tokens- smallest individual unit. Following are the tokens

 Keyword-Reserve word that can‘t be used as identifier

 Identifiers-Names given to any variable, function, class, union etc.

 Literals-Value of specific data type

 Variable- memory block of certain size where value can be stored and changed.

 Constant- memory block where value can be stored once but can‘t changed later on

 Operator – performs some action on data

o Arithmetic(+,-,*,/,%)

o Relational/comparison (<,>,<=,>=,==,!=).

o Logical(AND(&&),OR(||),NOT(!).

o Conditional (? :)

o Increment/Decrement Operators( ++/--)

 Precedence of operators:

++(post increment),--(post decrement) Highest

Low

++(pre increment),--(pre decrement),sizeof !(not),-(unary),+unary plus)

*(multiply), / (divide), %(modulus)

+(add),-(subtract)

<(less than),<=(less than or equal),>(greater than), >=(greater than or equal to)

==(equal),!=(not equal)

&& (logical AND)

||(logical OR)

?:(conditional expression)

=(simple assignment) and other assignment operators(arithmetic assignment

operator)

, Comma operator

Data type- A specifier to create memory block of some specific size and type. For example –

int,float,double,char etc.

cout – It is an object of ostream_withassign class defined in iostream.h header file and used to

display value on monitor.

cin – It is an object of istream_withassign class defined in iostream.h header file and used to read

value from keyboard for specific variable.

comment- Used for better understanding of program statements and escaped by the compiler to

compile . e.g. – single line (//) and multi line(/*….*/)

Control structure :

Sequence

control

statement(if)

conditional

statement

(if else)

case control statement

(switch case)

loop control statement

(while ,do… while, for)

Syntax Syntax Syntax Syntax

if(expression)

{

statements;

}

If(expression)

{

statements;

}

else

{

statements;

}

switch(integral expression)

{case (int const expr1):

[statements

break;]

case (int const expr2):

[statements,

break;]

default:Statements;}

while(expression)

{statements;}

do ….while loop

do

{statement;} while(expression);

for loop

for(expression1;expression2;expression3)

{statement;}

Note: any non-zero value of an expression is treated as true and exactly 0 (i.e. all bits contain

0) is treated as false.

Nested loop -loop within loop.

5

exit()- defined in process.h and used to leave from the program.

break- exit from the current loop.

continue- to skip the remaining statements of the current loop and passes control to the next loop

control statement.

goto- control is unconditionally transferred to the location of local label specified by <identifier>.

For example

A1:

cout<<‖test‖;

goto A1;

Some Standard C++ libraries

Header Nome Purpose

iostream.h Defines stream classes for input/output streams

stdio.h Standard input and output

ctype.h Character tests

string.h String operations

math.h Mathematical functions such as sin() and cos()

stdlib.h Utility functions such as malloc() and rand()

Some functions

 isalpha(c)-check whether the argument is alphabetic or not.

 islower(c)- check whether the argument is lowecase or not.

 isupper(c) - check whether the argument is upercase or not.

 isdigit(c)- check whether the argument is digit or not.

 isalnum(c)- check whether the argument is alphanumeric or not.

 tolower()-converts argument in lowercase if its argument is a letter.

 toupper(c)- converts argument in uppercase if its argument is a letter.

 strcat()- concatenates two string.

 strcmp-compare two string.

 pow(x,y)-return x raised to power y.

 sqrt(x)-return square root of x.

 random(num)-return a random number between 0 and (num-1)

 randomize- initializes the random number generator with a random value.

Array- Collection of element of same type that are referred by a common name.

One Dimension array

 An array is a continuous memory location holding similar type of data in single row or single

column

Two dimensional array

 A two diamensional array is a continuous memory location holding similar type of data in of

both row sand columns (like a matrix structure).

Function -Self-contained block of code that does some specific task and may return a value.

Function prototypes-Function declaration that specifies the function name, return type and

parameter list of the function.

syntax: return_type function_name(type var1,type var2,….,type varn );

Passing value to function-

 Passing by value

 Passing by address/reference

Function overloading

 Processing of two or more functions having same name but different list of parameters

Function recursion

 Function that call itself either directly or indirectly.

Local variables

 Declared inside the function.

Global variables

 Declared outside all braces { } in a program

Actual Parameters

Variables associated with function name during function call statement.

Formal Parameters

Variables which contains copy of actual parameters inside the function definition.

6

Structure-Collection of logically related different data types (Primitive and Derived) referenced under

one name.

Nested structure

 A Structure definition within another structure.

 A structure containing object of another structure.

typedef

 Used to define new data type name

#define Directives

 Use to define a constant number or macro or to replace an instruction.

Inline Function

 Inline functions are functions where the call is made to inline functions, the actual code then

gets placed in the calling program.

 What happens when an inline function is written?

 The inline function takes the format as a normal function but when it is compiled it is compiled

as inline code. The function is placed separately as inline function, thus adding readability to

the source program. When the program is compiled, the code present in function body is

replaced in the place of function call.

General Format of inline Function:

The general format of inline function is as follows:

inline datatype function_name(arguments)

inline int MyClass( )

Example:

#include <iostream.h>

int MyClass(int);

void main( )

{int x;

cout << ―\n Enter the Input Value: ‖;

cin>>x;

cout<<‖\n The Output is: ― << MyClass(x);

}

inline int MyClass(int x1)

{return 5*x1;}

The output of the above program is:

Enter the Input Value: 10

The Output is: 50

The output would be the same even when the inline function is written solely as a function.

The concept, however, is different. When the program is compiled, the code present in the

inline function MyClass ( ) is replaced in the place of function call in the calling program. The

concept of inline function is used in this example because the function is a small line of code.

The above example, when compiled, would have the structure as follows:

#include <iostream.h>

int MyClass(int);

void main( )

{int x;

cout << ―\n Enter the Input Value: ‖; cin>>x;

//The MyClass(x) gets replaced with code return 5*x1;

cout<<‖\n The Output is: ― << MyClass(x);

}

#include <iostream.h>

int MyClass(int);

void main( )

{int x;

cout << ―\n Enter the Input Value: ‖;

cin>>x;

//Call is made to the function MyClass

7

cout<<‖\n The Output is: ― << MyClass(x);

}

int MyClass(int x1)

{return 5*x1;}

No comments:

Post a Comment

CMS - 2024 || JAC INTERMEDIATE EXAMINATION - 2024 || 12th COMPUTER SCIENCE!

  Part-A Multiple Choice Questions 1) Who is the developer C++? a) Von Neumann b) Dennis M. Ritchie c) Charles Babbage d) Bjarne Stroustrup ...