Oops 2
Oops 2
Objects
Modular Programming
“The process of splitting of a large program into small manageable tasks and designing
them independently is known as Modular Programming or Divide-&-Conquer
Technique.”
C++ Functions
• Self-contained program that performs a specific task.
• “Set of program statements that can be processed independently.”
• Like in other languages, called subroutines or procedures.
Advantages
• Elimination of redundant code
• Easier debugging
• Reduction in the Size of the code
• Leads to reusability of the code
Functions are broadly classified as
Function Components
• 5. return statement
Parameter Passing in
Functions
* actual parameters– used in the function call
* formal parameters used in function declarator &
– definition
Passing Constant Values to Functions
Passing Variable Values to Functions
Memory Allocation for
Functions
Parameter passing by
Pointer
Parameter passing by
reference
Return by Reference
Functions with default arguments
* Usually functions should be passed values during function call.
#include<iostream.h>
#include<stdio.h>
main()
{
float amount;
printline( );
amount=value(5000.00,5);
cout<<”\n final value=”<<amount<<endl; printline(‘=’);
}
//function definitions
float value (float p,int n, float r)
{
float si; si=p+(p*n*r)/100; return(si);
}
inline function-header
{
function body;
}
Example:
* The period of time during which the memory is associated with a variable is called
the
extent of the variable.
Storage Classes
Syntax of declaring variables with storage class
Recursive Functions
*A function calling itself repeatedly until a condition is satisfied
is called a recursive function
Classes &
Objects
Using Class in C++ needs 3 steps to be followed
i. Declaration of class
ii. Defintion of member functions
1. private
2. public
3. protected
Two objects of the class
student
Client-Server model for message
communication
Characteristics of Member Functions:
Write a simple program using class in C++ to input subject mark and prints
it. class marks
{
private :
int roll;
int
ml,m2;
public:
void getdata();
void displaydata();
};
void marks: :getdata()
{ cout<<“enter the roll-no:”;cin>>roll;
cout<<”enter 1st subject mark:”;
cin>>ml;
cout<<”enter 2nd subject mark:”;
cin>>m2;
}
void marks: :displaydata()
{ cout<<“Roll No.”<<roll;
void main()
{
clrscr();
marks x;
x.getdata();
x.displaydata();
}
Nesting of Member Functions
Memory allocation for static member
Array of objects
REFERENCES:
1.E. Balagurusamy, “Object Oriented Programming with C++”, Fourth edition, TMH,
2008.
3.K.R.
Venugopal, Rajkumar, T. Ravishankar, “Mastering C++”, Tata
McGraw-Hill Publishing Company Limited