Consider a situation in which we execute a single statement or block of statements repeatedly for required number of times. Such kind of problems can be solved using looping statements in C++.
statements repeatedly until the given condition is FALSE.
C++ language provides three looping statements...
while statement
do-while statement
for statement
The while statement is used to execute a single statement or block of statements repeatedly as long as the given condition is TRUE. The while statement is also known as Entry control looping statement.
working
The condition is checked before the loop starts.
If the condition is TRUE, the block of code is executed.
After the execution of the block, the condition is checked again.
If the condition remains TRUE, the block is executed again.
This process repeats until the condition evaluates to FALSE.
When the condition is FALSE, the loop terminates, and control moves to the next statement after the loop.
The do-while statement is used to execute a single statement or block of statements repeatedly as long as given the condition is TRUE. The do-while statement is also known as Exit control looping statement.
Working:
The block of code inside the do is executed first, regardless of the condition.
After the execution of the block, the condition is checked.
If the condition is TRUE, the block is executed again.
This process repeats until the condition evaluates to FALSE.
Once the condition evaluates to FALSE, the loop terminates, and control moves to the next statement after the loop.
The for statement is used to execute a single statement or a block of statements repeatedly as long as the given condition is TRUE.
The for statement has the following syntax and execution flow diagram
In c++, there are control statements which does not need any condition to control the program execution flow. These control statements are called as unconditional control statements or jumping statements. C++ programming language provides the following unconditional control statements:
break
continue
goto
The above three statements does not need any condition to control the program execution flow.
The break statement is used to perform the following two things:
break statement is used to terminate switch case statement
break statement is also used to terminate looping statements like while, do- while and for.
When a break statement is encountered inside the switch case statement, the execution control moves out of the switch statement directly.
The continue statement is used to skip the current iteration of a loop and move the control back to the loop's condition to check if the loop should continue.
When continue statement encountered inside a loop, the continue statement skips the remaining statements in the loop and immediately proceeds to the next iteration.
The continue statement can be used with while, do-while, and for loops.
When we use continue statement with while and do-while statements the execution control directly jumps to the condition. When we use continue statement with for statement the execution control directly jumps to the modification portion (increment / decrement / any modification) of the for loop.
The goto statement is used to transfer control to a specific part of the program by jumping to a labeled line.
A label is a user-defined name associated with a line or instruction in the program.
When the goto statement is executed, the control jumps to the line that has the specified label, bypassing the normal flow of the program.
Key Points:
The goto statement provides an unconditional jump to the labeled line.
The label is defined with an identifier followed by a colon (:), e.g., label_name:
It can jump to any point in the program, either forward or backward, based on where the label is placed.
A class is similar to a structure. It is a data type defined by the programmer, consisting of variables and functions.
The building block of C++ that leads to Object Oriented programming is a Class. It is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.
A class is a blueprint, or prototype which defines and describes the member attributes and member functions.
A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end
A class is collection of data members and member variables. A Class Contains
Data Members or Simply Called As Variables
Member Functions or Simply Called as Functions
When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;
The data members and member functions of class can be accessed using the dot(.) operator with the object.
For example if the name of object is obj and you want to access the member function with the name display() then you will have to write obj.display().
Access modifiers are used to implement important feature of Object Oriented Programming known as Data Hiding.
Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
There are 3 types of access modifiers available in C++:
Public
Private
Protected
Note: If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be Private.
Public: All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class.
Protected: Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.
Member functions of a class can be defined either outside the class definition or inside the class definition. In both the cases, the function body remains the same, however, the function header is different.
Inside the class definition
Outside the class definition
Inside the class:
When a member function is defined inside the class, no need to declare a function just we have to write definition of the function.
Consider the following syntax for defining member function inside
return_type function_name(argument list)
{
- - - - - - - - - -
body of function;
- - - - - - - - - -
}
Outside the Class:
Defining a member function outside a class requires the function declaration (function prototype) to be provided inside the class definition. The member function is declared inside the class like a normal function.
This declaration informs the compiler that the function is a member of the class and that it has been defined outside the class. After a member function is declared inside the class, it must be defined (outside the class) in the program.
A public member function can also be defined outside of the class with a special type of operator known as Scope Resolution Operator (SRO), SRO represents by :: (double colon)
syntax
Return_type class_name :: function_name (parameter_list)
{
// body of the member function
}
Here Scope Resolution Operator (SRO) tells to the compiler that function name belongs to which class.
Normally we are declaring data members under private section and all the functions in public section, sometimes we have to declare member functions in private section.
Accessing member functions declared under private inside the class we have to declare one more member function as public.
Based on the public member functions only we can access private data members and member functions.
Syntax:-
class classname
{
private:
Data members;
Pr_functions()
{
//statements;
}
public:
Public_functions()
{
//statements; Pr_functions();
}
};
When we declare a normal variable (data member) in a class, different copies of those data members create with the associated objects.
In some cases when we need a common data member that should be same for all objects, we cannot do this using normal data members. To fulfill such cases, we need static data members.
It is a variable which is declared with the static keyword, it is also known as class member, thus only single copy of the variable creates for all objects.
Any changes in the static data member through one member function will reflect in all other object’s member functions.
Declaration
static data_type member_name;
Defining the static data member
It should be defined outside of the class following this syntax: data_type class_name :: member_name =value;
Accessing static data member
A static data member can also be accessed through the class name ,here we need an Scope Resolution Operator (SRO) :: to access the static data member without static member function.
Syntax:
class_name :: static_data_member;
A static member function is a special member function, which is used to access only static data members, any other normal data member cannot be accessed through static member function. Just like static data member, static member function is also a class function; it is not associated with any class object.
We can access a static member function with class name, by using following
syntax:
class_name::function_name(perameter);
If you are calling a static data member within a member function, member function should be declared as static (i.e. a static member function can access the static data members)
An object of class represents a single record in memory, if we want more than one record of class type, we have to create an array of class or object. As we know, an array is a collection of similar type, therefore an array can be a collection of class type.
Arrays of variables of type "class" is known as "Array of objects".
Syntax for Array of object:
class-name obj[ size ];