0% found this document useful (0 votes)
128 views9 pages

Array:-: An Array Is Collection of Identical Data Objects Which Are Stored in

An array is a collection of elements of the same data type stored in contiguous memory locations that can be accessed using an index. Elements in an array are numbered starting from 0. An array can be one-dimensional, containing a single list of elements, or multidimensional, containing multiple lists arranged in a grid. Classes allow grouping of related data and functions into a single unit called an object. Control flow statements like if/else and switch allow a program to conditionally execute blocks of code based on evaluations.

Uploaded by

abhimahajan980
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
128 views9 pages

Array:-: An Array Is Collection of Identical Data Objects Which Are Stored in

An array is a collection of elements of the same data type stored in contiguous memory locations that can be accessed using an index. Elements in an array are numbered starting from 0. An array can be one-dimensional, containing a single list of elements, or multidimensional, containing multiple lists arranged in a grid. Classes allow grouping of related data and functions into a single unit called an object. Control flow statements like if/else and switch allow a program to conditionally execute blocks of code based on evaluations.

Uploaded by

abhimahajan980
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Array:-

An array is collection of identical data objects which are stored in


consecutive memory locations under a common heading or variable
name. The objects are called elements of the array and are numbered
consecutively 0, 1, 2, 3, 4,...... . These numbers are called index value or
subscripts of the array. An array will be written with subscripts as a 0, a2,
a3........ The individual data items can be integers, floating point, character
or they can be user defined data types

If the name of the array is X, then X[0] is the name of the element that
is in position is 0, X[1] is in position 1 and so on . In general form of ith
element is X[i - 1]. If a array has n elements, their names are X[0], X[1],
X[2]......X[n - 1].

E.g. X[7] is an array of all integers then elements are represented as

X
X0] X[1] X[2] X[3] X[4] X[5] X[6]

Name of Last element is


Array is one less
than size of array
Elements of array
Types of array:-
An array are of two types
 One dimensional array
 Multidimensional array

One dimensional array:-


The syntax of one dimensional array is as follow:

Storage Class Datatype Arrayname [arraysize];


Where storage class refers to the scope of array variable such as
external, static or automatic.
The datatype is the user defined data type used arrayname is the name
of array and arraysize specifies the size of the array
Arrayname must be a valid c variable, followed by an integer value
enclosed in square brackets
e.g. int age[5]

multidimensional array:-
An array of array is a called a multidimensional array. It is defined
in same manner as one dimensional arrays, except that a separate pair
of square brackets Are required for each subscripts. Thus two
dimensional array will requires two pair of squares brackets and so on
The syntax is
Storage class datatype arrayname [exp 1] [exp 2]……… [exp n];
e.g. int X [4],[3];
Classes:-
A class is away to bind the data and function together in a single
unit. It is a user defined datatype which fault both the data and
function. The binding of a data and function together into a single class
type variable is called encapsulation.

Why class are used?


 Create multiple instance of class
 Reuse classes in more than one program

Class declaration:-
The class declaration describe the type and scope of its member
class function definations describe how the class function are
implemented. The general form of class declaration is

Class < class name >


{
private :
data members;
member functions;
protected :
data members;
member functions;
public :
data members;
member functions;
}
The body of class contains of variables and functions, collectively
known as members. The variables declared inside a class are known as
data members and the function are known as member functions. These
member are grouped under three sections.
 Private
 Protected
 public

private:-
Private member of a class can be access only from within the class
these can be used to read or write private data member the private
member cannot be access by outer class or outside the class.

Protected:-
Protected member of class can only be accessed by member
functions and friends of that class. Protected members are not
accessible to outside world i.e. out of class. Protected members are
similar to private members, the only difference is that protected
members are inheritable. Whereas private members are non-
inheritable.

Public:-
Public member can be accessed from outside class also. The public
data member can always read or write outside this class. The public
implementation operations are also called member functions or
methods or interfaces of the class. Any function can send message to
an object of this class through these interface functions
No entry private area
to private
area Data member

Member functions

Public area

Data member

Entry allowed
Member functions
to public area

Data hiding in a class

Object:-
It is an entity that has state behavior and identity object are used
to model real world entities that we find everyday life. An object is an
instance of class. In general a class is a user defined data type, while an
object is an instance of class. Once a class has been declared, we can
create variables of that type by using class name.

e.g. Let student be the name of class

student S1, S2;

creates variables S1, S2 of type student. These class variables are


known as object. It is the definition of an object that actually creates
objects in the program by setting aside memory space for its storage.
Objects can also be created by placing their names immediately after
the closing brace
e.g.
class student
{
………………..
………………..
………………..
………………..
} S1, S2, S3;

The above program segment creates objects S1, S2, and S3 of class
student.
Control flow:-
A compound statement is a sequence of statements that is
treated as a single. A computer is a set of instructions. These
instructions are known as statements. These statements are executed
sequentially one after the other as they appear in a program. The flow
of control jumps from one part of the program to another, depending
on calculations performed in the program. Program statements that
cause such jumps are called control statements.

Statements are classified into three types.


 Sequential statements
 Selection or branching statements
 Iteration or looping

Entry Entry Entry

Condition
Statement 1

True condition
Statement 1
Statement 2
Statement 2 Statement 1

Statement 3 Statement 2
Statement 3

Exit Exit Exit


(a) (b) (c)
Sequential statement selection statement Iteration statement

Selection or branching statements:-


When programmers are required to execute a
particular set of statement depending upon a particular text condition,
a branching . Branching statements alter sequential execution of
program statements.
Following are branching statement supported by c++
 If else
 If-else statement
 Switch statement

If statement:-
The if statement is used when conditional processing is
required. A statement or set of statement is executed when the
condition is true, otherwise these are skipped.
The syntax is
If (condition)
Statement ;

Where condition is an integer expression and statement is any


executeable statement

Entry

condition
True
Statments

False

Exit

Flow chart depicting logic of if statement .


Write a program to demonstrate if statement
# include <iostream.h>
# include <conio.h>
Void main ()
{
Int x,y;
Cout << ” Enter the first number x:\n”;
Cin >> x;
Cout << “ Enter the second number y:\n”;
Cin>> y;
If (x>y)
Cout << ”x is greater than y” ;
getch ()
}

The output is :
Enter the first number: 13
Enter the second number y: 10
X is greater than y

If-else statement:-
This statement works only if the condition is true. If it is not
true nothing happens, if-else statement executes one of alternatives
statements, according to the specified condition
The syntax is
If (condition)
Statement1;
else
statement2;

You might also like