0% found this document useful (0 votes)
11 views93 pages

Oops 2

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)
11 views93 pages

Oops 2

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/ 93

Functions, Classes &

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

1. Built-in functions (C++ Library functions)


2. User-defined functions

1. Built-in functions or C++ Library functions

Some of the built-in library functions are


strlen(), strncpy(), strcmp() available in string.h
pow(),sqrt(),sin(),tan() available in math.h
getch(),clrscr( available in conio.h
2. User-definied Functions:

Function Components

• 1. Function Prototypes (or) Function


Declaration

• 2. Function Definition(declarator & body)

• 3. Function call(actual parameters)

• 4. Function Parameters(formal parameters)

• 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.

* C++ allows function calls with fewer argument values if the


remaining arguments are assigned default values
Example:

#include<iostream.h>
#include<stdio.h>

float value(float p,int n,float r=15);


void printline(char ch=’*’,int len=40);

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);
}

void printline (char ch,int len)


{
for(inti=l;i<=len;i++) cout<<ch<<endl;
}
const arguments
INLINE FUNCTION:
An inline function is a function that is expanded inline when it is invoked. That is the
compiler replaces the function call with the corresponding function code.

The inline functions are defined as follows:-

inline function-header
{
function body;
}

Example:

inline int sqr(int num)


{
return(num*num);
}
Function Overloading
Scope & Extent of
Variables
*The region of source code in which the identifier is visibleis called the scope of the
identifier.

* 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

1. Specify the class

i. Declaration of class
ii. Defintion of member functions

2. Create objects for the class

3. Access the public members of the class using objects


Specifying a class
Creating
Objects
Accessing the Members of the Class
Accessing the Members
Defining Member Functions
Member function definition Outside the class
specification
Access Specifiers/Visibility Modes:

C++ provides 3 types of Visibility


Modes

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.

2.LECTURE NOTES ON Object Oriented Programming Using C++ by Dr. Subasish


Mohapatra, Department of Computer Science and Application College of Engineering
and Technology, Bhubaneswar Biju Patnaik University of Technology, Odisha

3.K.R.
Venugopal, Rajkumar, T. Ravishankar, “Mastering C++”, Tata
McGraw-Hill Publishing Company Limited

4. Object Oriented Programming With C++ - PowerPoint Presentation by Alok Kumar

5. OOPs Programming Paradigm – PowerPoint Presentation by an Anonymous Author

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy