Professional Documents
Culture Documents
BY MR. TARUS
Functions
Self contained block of statements which performs a particular task and
returns a value.
User defined functions must be declared before they are used in the
program.
Program demo for functions in C++
/* functions demo program */
#include<stdio.h> void china() //function definition
void japan(); //function declaration
void china(); {
2. Pass by reference
Pass by value
Call/Pass by value is the process where the copies of the actual parameters
are passed in one to one correspondence to the formal parameters in the
function definition. Therefore, any changes made in the formal parameters do
not affect the actual parameters.
e.g, addition(int a, int b, int c); call
Copies
int addition(int x, int y, int z) definition
If a function is recursive.
It does not require any stack on which we can push or pop the variables as it
does not perform any function calling.
Static declaration of class members means no matter how many objects of the
class are created, there is only one copy of the static member.
All static data is initialized to zero when the first object is created if no other
initialization is present.
• A static member function can only access static data member, other static
member functions and any other functions from outside the class.
• Static member functions have a class scope and they do not have access to
the this pointer of the class. You could use a static member function to
determine whether some objects of the class have been created or not.
//static data member private:
class Box { double length; double breadth; double
public: height;};
static int objectCount; // Initialize static member of class Box
int display(); int Box::objectCount = 0;
// Constructor definition int main(void) {
Box(double l, double b, double h) { Box Box1(3.3, 1.2, 1.5);
cout <<"Constructor called." << endl; Box Box2(8.5, 6.0, 2.0);
length = l; breadth = b; height = h; Box1.Volume();
// Increase every time object is created
Box2.Volume();
objectCount++;
// Print total number of objects.
}
cout<<"Tot object:"<<Box::objectCount;
double Volume() {
return 0;
cout<<"\n AREA = "<<(length*breadth*
height); }
Static Member Function
Functions declared with the keyword static
Function which can be called even if no objects of the class exist, therefore can be
accessed using only the class name and the scope resolution operator ::.
Can only access static data member, other static member functions and any other
functions from outside the class.
Have a class scope.
Do not have access to the this pointer of the class.
Used to determine whether some objects of the class have been created or not.
Properties:
Can only access other static variables or functions present in the same class
Called using the class name. Syntax- class_name::function_name( )
StaticDemo2.cpp
class Box {
int Box::objectCount = 0;
public:
int main() {
static int objectCount;
Box(double l, double b, double h) {
// Print total number of objects before
creating object.
cout<<"Constructor called."<<endl;
cout << "Inital Stage Count: "<<
length = l; breadth = b; height = h;
Box::getCount() << endl;
// Increase every time object is created
Box Box1(3.3, 1.2, 1.5);
objectCount++;
}
Box Box2(8.5, 6.0, 2.0);
double Volume() { Box1.Volume();
cout<<"\nAREA="<<(length*breadth*height); Box2.Volume();
} // Print total number of objects after
static int getCount() { creating object.
return objectCount; cout << "Final Stage Count: " <<
} Box::getCount() << endl;
private: return 0;
double length, breadth, height; }; }
Introduction to Friend Functions
Data hiding is fundamental concept in OOP, i.e, data members declared in
private section of a class. This is to restrict access of data members from outside
the class.
Protected members can only be accessed by the derived classes and are
inaccessible from outside the class.
Consider: friendDemo.cpp
Friend Function: PTO…
Functions that can access the private and protected data of a class.
A function of the class defined outside the class scope but it has the right to
access all the private and protected members of the class.
friend functions appear in the class definition but friends are the member
functions.
Characteristics of a Friend function:
The friend function is declared using friend keyword.
It is not a member of the class but it is a friend of the class.
As it is not a member of the class so it can be defined like a normal function.
Friend functions do not access the class data members directly but they pass an
object as an argument.
• It is not in the scope of the class that declares it as a friend.
Reasons for using friend functions
Used when the class private data needs to be accessed directly without using
object of that class.
Used to perform operator overloading.
Syntax:
class class_name
{
friend data_type function_name(argument/s);
};
Function can be defined anywhere in the program like a normal C++
Function definition does not use either the keyword friend or :: operator
Preceded by the keyword friend.
Advantages of Friend Function in C++
friendFunction.cpp, friendFunction1.cpp
The friend function allows the programmer to generate more efficient codes.
It is widely used in cases when two or more classes contain the interrelated
members relative to other parts of the program.
It allows additional functionality that is not used by the class commonly.
class Distance { Friend function program
private: void addvalue(Distance &d) // argument
int meters; contain the reference
{
public:
d.meters = d.meters+10; //
Distance() // constructor incrementing the value of meters by 10.
{ }
meters = 0; int main()
} {
void display_data() Distance d1;
{ d1.display_data(); // meters = 0
cout << "Meters value: "<<meters<<endl; addvalue(d1); // calling friend function
} d1.display_data(); // meters = 10
friend void addvalue(Distance &d); return 0;
}; }
Friend Class
Assume that class B is the friend class of class A.
Consider:
friendClass1.cpp
friendClass.cpp
Explanation:
The function add() is created in class B. This function returns the sum of numA and
numB. It is easy to create objects of class A inside class B, as the latter is a friend
class.
Friend class program: friendClass.cpp
class A {
int x =5;
int main()
friend class B; // friend class.
{
};
A a;
class B
B b;
{
b.display(a);
public:
return 0;
void display(A &a)
}
{
cout<<"value of x is : "<<a.x;
}
};
C++ Math Functions
mathematicalFun.cpp
Assignment