Unit – 2
Control Structures and Functions
A program is consists of set of instructions. These instructions are not always written in linear sequence. Program is designed to make decisions and to execute set of instructions number of times. Control Structure satisfies these requirements.
Decision Making Statements.
Decision making statements consists of two or more conditions specified to execute statement so that any condition becomes true or false another condition executes.
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.
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.
The goto
The goto is an unconditional jump type of statement. The goto is used to jump between programs from one point to another. The goto can be used to jump out of the nested loop.
Structure of the goto:
While executing program goto provides the information to goto instruction defined as a label. This label is initialized with goto statement before.
e.g.
When the compiler will come to goto label then control will goto label defined before the loop and output will be 1 2 3 4 5.
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.
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.
Outout is “world”.
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:
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.
2.10 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.
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
Functions are used to increase readability of the code. Size of program can be reducing by using functions. By using function, programmer can divide complex tasks into smaller manageable tasks and test them independently before using them together.
Value parameters
Write a C++ program to find volume of cube, cylinder.
Solution :
#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 (); |
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.
Program 6.4.1
Write a C++ program to illustrate Virtual functions.
Fig. P. 6.4.1
Solution :
#include<iostream> using namespace std; class Person { protected: int age; char name[20]; public: void accept() { cout<<”Enter name”; cin>>name; cout<<”Enter age ”; cin>>age; } virtual void display() {
} }; class Staff: public Person { protected: int exp; float salary; public: void getdata() { cout<<”Enter experience and salary”; cin>>exp>>salary; } void display() { cout<<”NAME:”<<name<<endl; cout<<”AGE:”<<age<<endl; cout<<”Experience:”<<exp; cout<<”SALARY:”<<salary; } }; class Student: public Person { protected: int marks,grade; public: void getdata1() { cout<<”Enter marks and grade”; cin>>marks>>grade; } void display() { cout<<”NAME:”<<name<<endl; cout<<”AGE:”<<age<<endl; cout<<”GRADE:”<<grade; cout<<”MARKS:”<<marks; } }; int main() { Person *p,b; Staff obj1; Student obj2; b.accept(); obj1.getdata(); p=& obj1; p->display(); obj2.getdata1(); p=& obj2; p->display(); return 0; } | //Declares header file
//Declares and defines base class Person
//Declares age and name under protected section
//Defines accept function under public section
//Accepting age and name of Person
//End of accept function //Declares Virtual function
//Empty virtual function
//End of class person //Create derived class Staff from base class Person //Declares data members under protected section
//Defines getdata function
//Accepting details of salary and experience
//End of getdata function //Defines virtual functiondisplay
//Displaying details of Staff
//End of display function //End of derived class staff //Creates derived class Student from base class Person //Declares data members under protected section
//Defines getdata1 function
//Accepting details of marks and grade //End of getdata function
//Redefines virtual function display()
//Displaying details of Student
//End of display1 function //End of derived class student //Starts main function
//Creates base class pointer and object //Creates object O1 of Staff //Creates object O2 of Student //Calls accept function //Points to Staff class //Calls display() of Staff class
//Points to Student class //Calls display() of Student class //End of main |
Output
Enter name
John Mathew
Enter age 25
Enter experience and salary
5 50000
NAME: John Mathew
AGE:25
Experience:5
SALARY:50000
Enter marks and grade
90 A
NAME: John Mathew
AGE:25
MARKS:90
GRADE:A
Reference Books
1. The C++ Programming language 3/e by Bjarne Stroustrup, Pearson Education.
2. C++, How to Programme, 4e, by Deitel, Pearson Education.
3. Big C++ by Cay Horstmann, Wiley India.
4. C++ Primer, 3e by Stanley B. Lippmann, JoseeLajoie, Pearson Education.
5. C++ and Object Oriented Programming Paradigm, 2e by Debasish Jana, PHI.
6. Programming with C++, 2/e by Ravichandran, Tata McGraw Hill.
7. C++ Programming Black Book by Steven Holzner, Dreamtech Press.