Unit – 2
Control Structures and Functions
The if-else statement
The if else statement consists of if followed by else. Condition specified in if becomes true then statement in if block executes. When condition mentioned in if becomes false then statements in else execute.
Structure of the if – else.
e.g.
2. Explain nested if-else statement?
The Nested if- else consist of multiple if-else statements in another if – else. In this specified conditions are executed from top. While executing nested if else, when condition becomes true statement related with it gets execute and rest conditions are bypassed. When all the conditions become false last else statement gets execute.
Structure of the nested if – else:
e.g.
Here output is 5. When i=5 condition will match it will give is 5 output if not then last output will be i is not present.
3. Explain goto and break statement?
The break
The break statement is used to terminate the execution where number of iterations is not unknown. It is used in loop to terminate the loop and then control goes to the first statement of the loop. The break statement is also in switch case.
Structure of the break:
e.g.
When the compiler will reach to break it will come out and print the output. Output will be x=1,x=9,x=8,x=7,x=6,break.
The continue
The continue statement work like break statement but it is opposite of break. The Continue statement continues with execution of program by jumping from current code instruction to the next.
Structure of the break:
e.g.
Output is 1 2 3 4 5.
4. Explain switch statement?
The switch statement is a branch selection statement. The switch statement consists of multiple cases and each case has number of statement. When the condition gets satisfied the statements associated with that case execute. The break statement is used inside switch loop to terminate the execution.
Structure of the switch statement:
e.g.
Output is “world”.
5. Explain for loop statements?
The for loop
The for loop executes repeatedly until the condition specified in for loop is satisfied. When the condition becomes false it resumes with statement written in for loop.
Structure of the for loop:
Initialization is the assignment to the variable which controls the loop. Condition is an expression, specifies the execution of loop. Increment/ decrement operator changes the variable as the loop executes.
e.g.
Output is 1 2 3 4 5.
The Nested for loop.
Nested for loop is for within for loop.
Structure of the nested for loop:
6. Explain while loop conditions?
The while loop consists of a condition with set of statements. The Execution of the while loop is done until the condition mentioned in while is true. When the condition becomes false the control comes out of loop.
Structure of the while loop:
e.g.
X is initialized to 1. The loop will execute till x<=6.
Output will be 1 2 3 4 5 6 7 8 9 10.
7. Explain do-while loop?
The do – while loop consist of consist of condition mentioned at the bottom with while. The do – while executes till condition is true. As the condition is mentioned at bottom do – while loop is executed at least once.
Structure of the do – while:
e.g.
Output is: 1 2 3 4 5 6 7 8 9 10.
8. Explain function? What are the parts of the function?
A function is the block of statement. An entire program is divided into multiple blocks to perform specific task simultaneously. Every program has one function; is the main().Each function has declaration , function call and definition.
Function Declaration
Function is declared globally to inform the compiler about function name, function parameters and return type.
Example
Function call
Function call has parameter list with function name. Using function call function can be called anywhere in program.
Example
Function definition.
Function definition consists of block of statements which contains actual execution code. When function gets called in program control comes to the function definition.
Example
9. Explain the types of functions?
Value- Returning Functions
10. Write a program to find the volume of cube and cylinder explain function overloading?
#include<iostream>
using namespace std ;
class Volume //Defines class Volume
{
int x,y,z;
public:
Volume ( int a) // Step 1Constructor with one argument
{
x=a;
}
Volume ( int a, int b) // Step 2 Constructor with two arguments
{
x=a;
y=b;
}
void displaycube();
void displaycylinder();
};
void Volume::displaycube() // Step 3 Function to calculate volume of cube
{
cout<<x*x*x;
}
void Volume::displaycylinder() //Step 4 Function to calculate volume of cylinder
{
cout<<3.14*x*x*y;
}
int main()
{
Volume obj1(10); // Step 5Calls constructor with one argument.
Volume obj2(10,5); // Step 6 Calls constructor with two arguments.
obj1. displaycube (); //Step 7
obj2. displaycylinder ();
return 0 ;
}
Explanation
This program calculates volume of cube and cylinder. For this we use two constructors in a class. One constructor is to initialize data member for cube and other to initialize data members for cylinder.
Step 1 : Defines constructor with one argument. Here x stands for side of a cube.
Volume ( int a) { x=a; } | //Constructor with one argument
|
Step 2 : Defines constructor with two arguments. Here x and y specifies radius and height.
Volume ( int a, int b) { x=a; y=b; } | //Constructor with two arguments
|
Step 3 : Defines Function to calculate volume of cube.
void Volume::displaycube() { cout<<x*x*x; } | //Function to calculate volume of cube
|
Step 4 : Defines Function to calculate volume of cylinder
void Volume::displaycylinder() { cout<<3.14*x*x*y; } | //Function to calculate volume of cylinder
|
Step 5 : Here we want to initialize side of cube so we pass only one argument to invoke constructor with one argument.
Volume obj1(10); |
Step 6 : Here we want to initialize radius and height of cylinder so we pass two arguments to invoke constructor with two arguments.
Volume obj2(10,5); |
Step 7 : Calls functions to calculate volume of a cube and cylinder.
obj1. displaycube (); obj2. displaycylinder (); |
11. Explain virtual functions?
virtual return-type function-name(argument list);
class Base { public: void Data() { cout<<”Base class”; } }; class Derived: public Base { public: void Data() { cout<<”Derived class”; } }; void main() { Base *b; Derived d; b->Data(); b= &d;
b->Data (); } | //Defines Base class
//Defines function Data of Base class
//Defines “Derived” class that is sub class of “Base”.
//Defines function Data of “Derived” class
// Step 1Creates base class pointer //Creates object of derived class // Step 2 Calls Data function with base class pointer // Step 3 Assigns address of derived class object
// Step 4 |
Explanation
Step 1: *b is base class pointer and d is derived class object.
Step 2: b->Data(); calls ”Data ” function of base class..
Step 3: b= &d; points to derived class object.
Step 4: b->Data(); calls member function “Data” of base class even though b points to derived class object.
Output
Base class
Base class
class Base { public: virtual void Data() { cout<<”Base class”; } }; class Derived: public Base { public: void Data() { cout<<”Derived class”; } }; void main() { Base *b,a;
Derived d;
b= &a;
b->Data();
b= &d;
b->Data (); } | //Defines Base class
//Defines virtual function Data of Base class
//Defines “Derived” class that is sub class of “Base”.
//Redefines virtual function “Data”
//Step 1 Creates base class pointer and object
//Creates object of derived class // Step 2 Points to base class
// Step 3 Calls Data function of Base class
// Step 4 Points to Derived class
// Step 5 Calls Data function of Derived class
|
Explanation
Step 1: *b and a represent base class pointer and object respectively.
d is derived class object.
Step 2: b=&a; points to base class.
Step 3: b->Data calls base class version of Data() function.
Step 4: b= &d; points to derived class object.
Step 5: b->Data calls derived class version of Data() function.
Hence the output of above program will be
Output
Base class;
Derived class
Virtual keyword is not required when derived class redefines virtual function. However we can write it but it’s just not mandatory.