KEMBAR78
11zon - OOP Using C++ - Unit - I C | PDF | Object Oriented Programming | Programming
0% found this document useful (0 votes)
14 views22 pages

11zon - OOP Using C++ - Unit - I C

Uploaded by

classcbse947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

11zon - OOP Using C++ - Unit - I C

Uploaded by

classcbse947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT - I

Introduction to C++ - key concepts of Object-Oriented Programming – Advantages –


Object Oriented Languages – I/O in C++ - C++ Declarations. Control Structures: -
Decision Making Statements: If..else, jump, goto, break, continue, Switch case
statements - Loops in C++ :for, while, do - functions in C++ - inline functions – Function
Overloading.

INTRODUCTION TO C++

Programmers write instructions in various programming languages to perform


their computation tasks such as:

(i) Machine level Language


(ii) Assembly level Language
(iii) High level Language
Machine level Language:

Machine code or machine language is a set of instructions executed directly by a


computer's central processing unit (CPU). Each instruction performs a very specific task,
such as a load, a jump, or an ALU operation on a unit of data in a CPU register or
memory. Every program directly executed by a CPU is made up of a series of such
instructions.
Assembly level Language:

An assembly language (or assembler language) is a low-level programming


language for a computer, or other programmable device, in which there is a very strong
(generally one-to-one) correspondence between the language and the architecture's
machine code instructions. Assembly language is converted into executable machine
code by a utility program referred to as an assembler; the conversion process is referred
to as assembly, or assembling the code.
High level Language:

High-level language is any programming language that enables development of a


program in much simpler programming context and is generally independent of the
computer's hardware architecture. High-level language has a higher level of abstraction
from the computer, and focuses more on the programming logic rather than the
underlying hardware components such as memory addressing and registers utilization.

The first high-level programming languages were designed in the 1950s. Now there
are dozens of different languages, including Ada, Algol, BASIC, COBOL, C, C++, JAVA,
FORTRAN, LISP, Pascal, and Prolog. Such languages are considered high-level because
they are closer to human languages and farther from machine languages. In contrast,

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 1
assembly languages are considered low-level because they are very close to machine
languages.

The high-level programming languages are broadly categorized in to two


categories:
1. Procedure Oriented Programming (POP) language.
2. Object Oriented Programming (OOP) language.
Procedure Oriented Programming Language

In the procedure oriented approach, the problem is viewed as sequence of things


to be done such as reading, calculating and printing. Procedure oriented programming
basically consist of writing a list of instruction or actions for the computer to follow and
organizing these instruction into groups known as functions.

Disadvantages of POP languages:

The disadvantages of the procedure oriented programming languages are:

1. Global data access


2. It does not model real word problem very well
3. No data hiding

Characteristics of procedure oriented programming:

1. Emphasis is on doing things (algorithm)


2. Large programs are divided into smaller programs known as functions.
3. Most of the functions share global data
4. Data move openly around the system from function to function
5. Function transforms data from one form to another.
6. Employs top-down approach in program design

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 2
Object Oriented Programming Paradigm

“Object oriented programming as an approach that provides a way of modularizing


programs by creating partitioned memory area for both data and functions that can be
used as templates for creating copies of such modules on demand”.

Features of the Object Oriented programming

1. Emphasis is on doing rather than procedure.


2. Programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data
structure.
5. Data is hidden and can‟ t be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added.
8. Follows bottom-up approach in program design.
Difference between Procedure Oriented Programming (POP) & Object Oriented
Programming (OOP)

Sl. Procedure Oriented Object Oriented Programming


No. Programming (POP) (OOP)
Program is divided into small Program is divided into parts called
1
parts called functions. objects.
Importance is not given to data Importance is given to the data rather
2 but to functions as well as than procedures or functions because
sequence of actions to be done. it works as a real world.
3 It follows Top Down approach. It follows Bottom Up approach.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 3
It does not have any access It has access specifiers named as
4
specifier. Public, Private, Protected, etc.
Data can move freely from Objects can move and communicate
5 function to function in the with each other through member
system. functions.
To add new data and function in OOP provides an easy way to add new
6
POP is not so easy. data and function.
Most function uses Global data In OOP, data can‟ t move easily from
for sharing that can be accessed function to function, it can be kept
7
freely from function to function public or private so we can control
in the system. the access of data.
It does not have any proper way
It provides a proper way for hiding
8 for hiding data, so it is less
data, so it is more secure.
secure.
Overloading is possible here in the
9 Overloading isn‟ t possible. form of Function Overloading and
Operator Overloading.

10 Example: C, VB, Fortran, Pascal Example: C++, Java, VB .Net, C# ,Net.

KEY CONCEPTS OF OBJECT-ORIENTED PROGRAMMING

1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
1. Objects

Objects are the basic run-time entities in an object-oriented system. They may
represent a person, a place, a bank account, a table of data or any item that the program
has to handle. The fundamental idea behind object oriented approach is to combine both
data and function into a single unit and these units are called objects.

When a program is executed, the objects interact by sending messages to one


another. Each object contains data, and the code to manipulate the data.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 4
Fig: Two ways of representing an object
2. Classes

Class is defined as a User Defined Data type. Objects are variables of the type
class. Once a class has been defined, we can create any number of objects belonging to
that class. Each object is associated with the data of type class with which they are
created. Thus, a class is nothing but a collection of objects of similar types.
Ex:

fruit mango;

Here, fruit is a class and mango is the object of the class fruit.

The class is like a blueprint where the memory space for it, will be allocated only
when an object is created for it.
3. Data abstraction and encapsulation

Abstraction refers to the act of representing essential features without including


the background details or explanations. Classes use the concept of abstraction and are
defined as size, weight and cost, and functions to operate on the attributes. The
attributes are sometimes called data members because they hold information. The
functions that operate on these data are sometimes called methods or member functions.
Since the classes use the concept of data abstraction, they are known as Abstract Data
Types (ADT).

The wrapping up of data and function into a single unit (called class) is known as
encapsulation. The data is not accessible to the outside world and only those functions
which are wrapped in the class can access it. These functions provide the interface
between the object‟ s data and the program. This insulation of the data from direct access
by the program is called Data Hiding or Information Hiding.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 5
4. Inheritance

Inheritance is the process by which objects of one class acquire the properties of
objects of another class. In OOP, the concept of inheritance provides the idea of
reusability. This means that we can add additional features to an existing class without
modifying it. This is possible by designing a new class from the existing one. The new
class will have the combined features of both the classes.
5. Polymorphism

Polymorphism (Greek term) means the ability to take more than one form. An
operation may exhibit different instance. The behaviour depends upon the types of data
used in the operation. The process of making an operator to exhibit different behaviours
in different instances is known as Operator Overloading. The way of using single
function name to perform different types of tasks is known as Function Overloading.
6. Dynamic binding

Dynamic binding means that the code associated with a given procedure call is
not known until the time of the call at run-time. It is associated with polymorphism and
inheritance.
7. Message passing

An object-oriented program consists of a set of objects that communicate with


each other. The process of programming in an OOP language, involves the following
basic steps:

1. Creating classes that define objects and their behavior


2. Creating objects from class definitions, and
3. Establishing communication among objects.

A message for an object is a request for execution of a procedure and therefore will
invoke a function (procedure) in the receiving object that generates the desired result.
Message passing involves specifying the name of the object, the name of the function
(message) and information to be sent.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 6
ADVANTAGES OF OOP

OOP offers several benefits to both the program designer and the user. Object-
oriented contributes to the solution of many problems associated with the development
and quality of software products. The principal advantages are:

1. Through inheritance, we can eliminate redundant code and extend the use of
existing classes.

2. We can build programs from the standard working modules that communicate with
one another, rather than having to start writing the code from scratch. This leads to
saving of development time and higher productivity.

3. The principle of data hiding helps the programmer to build secure programs that
can‟ t be invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist without any
interference.

5. It is easy to partition the work in a project based on objects.

6. Object-oriented systems can be easily upgraded from small to large systems.

7. Message passing techniques for communication between objects makes the interface
description with external systems much simpler.

8. Software complexity can be easily managed.

OBJECT ORIENTED LANGUAGES

OOP is not the right of any particular language. The programming languages
should support several of the OOP concepts to claim that they are object-oriented.
Depending upon the features they support, they can be classified into the following two
categories:

1. Object-based programming languages, and


2. Object-oriented programming languages.

Languages that support programming with objects are said to be object-based


programming languages. They don‟ t support inheritance and dynamic binding. Ex:
ADA.

Languages that support almost all the OOP concepts are said to be object-
oriented programming languages. Ex: C++, Smalltalk, Java.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 7
I/O IN C++

C++ comes with libraries that provide us with many ways for performing input and
output. In C++ input and output are performed in the form of a sequence of bytes or
more commonly known as streams.

Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard)
to the main memory then this process is called input.

Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to
device (for example, display screen) then, this process is called output.
Header files available in C++ for Input/Output operations are:

1. iostream: iostream stands for standard input-output stream. This header file
contains definitions of objects like cin, cout, cerr, etc.
2. iomanip: iomanip stands for input-output manipulators. The methods declared in
these files are used for manipulating streams. This file contains definitions of setw,
setprecision, etc.
3. fstream: This header file mainly describes the file stream. This header file is used
to handle the data being read from a file as input or data being written into the file
as output.
Output Operator:

The statement cout<<“Hello, world” displays the string with in quotes on the
screen. The identifier cout can be used to display individual characters, strings and even
numbers. It is a predefined object that corresponds to the standard output stream.
Stream just refers to a flow of data and the Standard Output stream normally flows to
the screen display. The cout object, whose properties are defined in iostream.h
represents that stream. The insertion operator << also called the „put to‟ operator
directs the information on its right to the object on its left.
Ex: cout<<“C++ is better than C”;

Input Operator:

The statement cin>> number1; is an input statement. The program to wait for the
user to type in a number. The number keyed-in is placed in the variable number1. The
identifier cin is a predefined object in C++ that corresponds to the standard input
stream. Here this stream represents the key board. The operator >> is known as get
from operator. It extracts value from the keyboard and assigns it to the variable on its
right.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 8
C++ DECLARATIONS

All the variables must be declared before they are used in the executable
statements. C++ allows the declaration of a variable anywhere in the scope, i.e., a
variable can be declared right at the place of its first use. This makes the program easier
to understand, because the variables are declared in the context of their use.
Ex:

int main( )
{
float x; // Declaration
float sum = 0;
for(int i=1; i<5; i++) // Declaration
{
cin >> x;
sum = sum + x;
}
float average; // Declaration
average = sum / (i-1);
cout << average;
return 0;
}

The only disadvantage of this style is that we cannot see all the variables used in a
scope at a glance.

CONTROL STRUCTURES

Control Structures or Control Statements are used to transfer control from one
statement to any other statement in a program.

The method of achieving the aim of an accurate, error-resistant and maintainable


code can be done by using one or any combination of the following three control
structures:

1. Sequence Structure (straight line)


2. Selection Structure (branching) – if, if..else, nested if, switch
3. Loop Structure (iteration or repetition) – for, while, do-while

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 9
DECISION MAKING STATEMENTS

1. Simple if statement

The „if statement‟ is the most simple decision-making statement. It is used to


decide whether a certain statement or block of statements will be executed or not i.e if
the given condition is true, then the block of statements is executed, otherwise not.
Syntax:

if (test condition)
{
Statement block(s);
}
next statement;
If the test condition is true, statement block will be executed, otherwise execution
starts from the next statement.
Flowchart:

2. If..else statement

The „if statement‟ alone, tells us that if a condition is true it will execute a block of
statements and if the condition is false it won‟ t do anything. But, if we want to do
something else when the condition is false, then we can use the else statement with the
if statement to execute a block of code when the condition is false. The if-else
statement consists of two blocks, one for false expression and one for true
expression.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 10
Syntax:

if (test condition)
{
True block Statement (s);
}
else
{
False block Statement (s);
}
next statement;
Flowchart:

Example Program
// C program to illustrate if..else statement

#include <iostream.h>
int main()
{
int i = 20;
if (i < 15)
{
cout<<"i is smaller than 15";
}
else
F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 11
{
cout<<"i is greater than 15";
}
return 0;
}
Output

i is greater than 15

3. Nested if statement

Nested if statements mean an if statement inside another if statement. Both C


and C++ allow us to nest if statements within if statement, i.e, we can place an if
statement inside another if statement.
Syntax:

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition 2 is true
}
else
{
// Executes when condition 2 is false
}
}
Example Program

// C++ program to illustrate nested-if statement

#include <iostream.h>
int main()
{
int i = 10;
if (i == 10)
{
if (i < 15)
cout<<"i is smaller than 15\n";
// Nested - if statement will only be executed if statement above is true
if (i < 12)
cout<<"i is smaller than 12 too\n";

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 12
else
cout<<"i is greater than 15";
}
return 0;
}
Output

i is smaller than 15
i is smaller than 12 too

4. Switch Statement

The switch case statement provides a way of choosing between a set of alternatives
based on the values of an expression.
Syntax:

switch (expression)
{
case Label 1:
statements;
break;
case Label 2:
statements;
break;
....
....
....
default:
statements;
break;
}

The switch expression can evaluate either integer or character. It cannot


evaluate any other data type.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 13
Flowchart:

Example Program

// C++ program to illustrate switch statement

#include <iostream.h>
#include <conio.h>
void main()
{
char ch;
cout<<“\n Enter a Character:”;
cin>>ch;
switch(ch)
{
case „a‟ :
cout<<ch<<“ is a vowel”;
break;
case „e‟ :
cout<<ch<<“ is a vowel”;
break;
case „i‟ :
cout<<ch<<“ is a vowel”;
break;
case „o‟ :

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 14
cout<<ch<<“ is a vowel”;
break;
case „u‟ :
cout<<ch<<“ is a vowel”;
break;
default:
cout<<ch<<“ is not a vowel”;
break;
}
getch( );
}
Output

Enter a Character:
a
a is a vowel

JUMP STATEMENTS IN C++

These statements are used in C or C++ for the unconditional flow of control
throughout the functions in a program. They support the following types of jump
statements:
A) break

This loop control statement is used to terminate the loop. As soon as the break
statement is encountered from within a loop, the loop iterations stop there, and control
returns from the loop immediately to the first statement after the loop. Basically, break
statements are used in situations when we are not sure about the actual number of
iterations for the loop or we want to terminate the loop based on some condition.
Syntax:

break;

B) continue

This loop control statement is just like the break statement. The continue
statement is opposite to that of the break statement, instead of terminating the loop, it
forces to execute the next iteration of the loop. When the continue statement is executed
in the loop, the code inside the loop following the continue statement will be skipped and
the next iteration of the loop will begin.
Syntax:

continue;

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 15
C) goto

The goto statement in C/C++ also referred to as the unconditional jump statement
can be used to jump from one point to another within a function.
Syntax:

In the above syntax, the first line tells the compiler to go to or jump to the
statement marked as a label. Here, a label is a user-defined identifier that indicates the
target statement. The statement immediately followed after „label:‟ is the destination
statement. The „label:‟ can also appear before the „goto label;‟ as in the above syntax.

LOOPS IN C++

A block or group of statements executed repeatedly until some condition is


satisfied is called Loop. The loop statements are also called as iteration statements or
repetition statements. The group of statements enclosed within curly brace is called
block or compound statement. There are two categories of looping structures.

If the condition is tested before entering the statement block, then it is called as

Entry Controlled Statement.
If the condition is tested at the exit of the statement block, then it is called as Exit

Controlled Statement. Here, the statement block will be executed at least once.

There are three types of loop statements. They are as follows,

1. while loop statement


2. do-while loop statement
3. for loop statement

1. For Loop (Entry Controlled Statement)

A for loop is a repetition control structure, that allows you to efficiently write a
loop that needs to execute a specific number of times. It is an entry control loop that
provides a more crisp structure.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 16
Syntax:

for(initialization; test expression; increment/decrement)


{
statements;
}

For statement is divided into three expressions each is separated by semi colon;

1. initilization expression is used to initialize variables.


2. test expression is responsible of continuing the loop. If it is true, then the
program control flow goes inside the loop and executes the block of statements
associated with it .If test expression is false loop terminates.
3. increment/decrement expression consists of increment or decrement operator.

This process continues until test condition satisfies.


Example Program:

/*c++ program to find sum of n natural numbers */

#include<iostream.h>
#include<conio.h>
void main()
{
int i, sum=0, n;
cout<<”Enter the number of numbers to be added: \n";
cin>>n;
for(i=1;i<=n;i++) // for(initialization; test expression; increment/decrement)
{
sum = sum + i;
}
cout<<“Sum of first”<<n<<” natural numbers is ”<<sum;
getch();
}
Output

Enter the number of numbers to be added:


5
Sum of first 5 natural numbers is 15
2. While Loop (Entry Controlled Statement)

A while loop statement is an entry controlled loop. The condition is evaluated and
if it is true then body of loop is executed. After execution of body, the condition is once

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 17
again evaluated and if is true, body is executed once again. This goes on until test
condition becomes false.

Syntax:

while(test condition)
{
body of the loop
}

Example Program:

/*c++ program to find sum of n natural numbers */

#include<iostream.h>
#include<conio.h>
void main()
{
int i=1, sum=0, n;
cout<<”Enter the number of numbers to be added: \n";
cin>>n;
while(i<=n)
{
sum = sum + i;
i=i+1;
}
cout<<“Sum of first”<<n<<” natural numbers is ”<<sum;
getch();
}
Output

Enter the number of numbers to be added:


5
Sum of first 5 natural numbers is 15

3. Do-While Loop (Exit Controlled Statement)

Unlike for and while loops, which test the loop condition at the top of the loop,
the do..while loop checks its condition at the bottom of the loop. The while loop does
not allow body to be executed, if test condition is false. A do…while loop is similar to a
while loop, except that a do…while loop is guaranteed to execute at least one time.
Hence, the do while is called as an exit controlled loop.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 18
Syntax:

do
{
body of the loop
} while(test condition);
Example Program:

/*c++ program to find sum of n natural numbers */

#include<iostream.h>
#include<conio.h>
void main()
{
int i=1, sum=0, n;
cout<<”Enter the number of numbers to be added: \n";
cin>>n;
do
{
sum = sum + i;
i=i+1;
} while(i<=n);
cout<<“Sum of first”<<n<<” natural numbers is ”<<sum;
getch();
}
Output

Enter the number of numbers to be added:


5
Sum of first 5 natural numbers is 15

Difference between do-while & while loops

Sl.
do-while loop while loop
No.
Condition is checked at the end of Condition is checked at the beginning of
1
the loop. the loop.

Set of statements within the loop If the condition fails, then the statements
2
will be executed at least once. will not be executed even once

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 19
FUNCTIONS IN C++

1. Inline Functions

When a function is declared as inline, the compiler copies the code of the function
in the calling function. i.e., function body is inserted in place of function call during
compilation.
Syntax:

inline function_name(with or without arguments)


{
statement block to be executed;
}
Following are few situations where inline functions may not work:

1. The function should not be recursive.


2. Function should not contain static variables.
3. Function containing control structure statements such as switch, if, for loop, etc.
4. The function main() cannot be used as inline.

The inline functions are similar to macros of C. The main limitation of macros is
that they are not functions, and errors are not checked at the time of compilation. The
inline function offers better type testing and does not contain limitations as present in
macros.
Example Program:

#include <iostream.h>
#include <conio.h>
int sum(int a, int b, int c)
{
return a+b+c;
}
inline int volume(int a)
{
return(a*a*a);
}
void main()
{
cout<<"The sum of 1, 2 and 5 is "<<sum(1,2,5)<<endl;
cout<<"The volume of cube of side 2cm is "<<volume(2)<<endl;
return 0;
}

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 20
Output

The sum of 1, 2 and 5 is 8


The volume of cube of side 2cm is 8cm
2. Function Overloading

It is possible in C++ to use the same function name for number of times and for
different intentions. Defining multiple functions with same name is known as function
overloading or function polymorphism. Polymorphism means one function having many
forms. The overloaded function must be different in their argument list and with
different data types. The following are examples of overloaded functions. All the
functions defined should be equivalent to their prototypes.

int test() { }
int test(int a) { }
int test(double a) { }
int test(int a, double b) { }
Principles of Function Overloading:

1. If two functions have the similar type and number of arguments (data type), the
function cannot be overloaded. The return type may be similar or void, but argument
data type or number of arguments must be different. For example,

a) sum(int,int,int);
sum(int,int);
Here, the above function sum is overloaded. Though the data type of arguments in
both the functions are similar, number of arguments are different.

b) sum(int,int,int);
sum(float,float,float);

In the above example, number of arguments in both the functions are same, but
data types are different. Hence, the above function can be overloaded.

2. Passing constant values directly instead of variables also results in ambiguity.

3. The compiler attempts to find an accurate function definition that matches in types
and number of arguments and invokes that function. The arguments passed are checked
with all declared function. If matching function is found then that function gets
executed.

4. If there are no accurate match found, compiler makes the implicit conversion of actual
argument. For example, char is converted to int and float is converted to double.

5. If internal conversion fails, user-defined conversion is carried out with implicit


conversion and integral promotion.

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 21
Precautions with Function Overloading

1. Only those functions that basically do the same task, on different sets of data, should
be overloaded. The overloading of function with identical name but for different purposes
should be avoided.

2. In function overloading, more than one function has to be actually defined and each of
these occupy memory.

3. Instead of function overloading, using default arguments may make more sense and
fewer overheads.

4. Declare function prototypes before main() and pass variables instead of passing
constant directly. This will avoid ambiguity that frequently occurs while overloading
functions.
Example Program:

#include <iostream.h>
#include <conio.h>
float volume(int r, int h)
{
return(3.14*r*r*h);
}
int volume(int a)
{
return(a*a*a);
}
int volume(int l, int b, int h)
{
return(l*b*h);
}
void main()
{
cout<<"The volume of cylinder of radius 1cm and height 5cm is "<<volume(1,5)<<endl;
cout<<"The volume of cube of side 2cm is "<<volume(2)<<endl;
cout<<"The volume of cuboid of 1cm, 2cm and 5cm is "<<volume(1,2,5)<<endl;
getch();
}

Output

The volume of cylinder of radius 1cm and height 5cm is 15.7cm


The volume of cube of side 2cm is 8cm
The volume of cuboid of 1cm, 2cm and 5cm is 10cm
******

F.Motcha rani, Asst. Prof., Dept. of CS,Karan Arts & Science College, Tiruvannamalai. Page 22

You might also like