Boop - Unit - 2
Boop - Unit - 2
User-defined Function
A function declaration tells the compiler about a function name and how to call the function. The actual
body of the function can be defined separately.
Syntax:
Example:
2) Function definition
Example:
int max(int n1, int n2)
{
int result;
return result;
}
3) Function call
To use a function, you will have to call or invoke that function.
When a program calls a function, program control is transferred to the called function.
Example:
void main( )
{
int a=10,b=20,c;
c=max(a,b); //Function call
cout<< “ Maximum = “<<c;
}
call by value:
#include<iostream.h>
void exch(int a, int b); //function prototype
void main()
{
int x,y;
cout<<”Enter two numbers:”<<endl;
cin>>x>>y;
exch(x,y);
getch( );
}
void exch(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<”After exchange:”<<endl;
cout<<a<<” ”<<b;
}
Call By Reference:
The c++ provides easy and effective solution using the reference variables.
When function is called, it creates a reference for each argument and using references the actual
arguments are accessed.
This method of passing the arguments or parameters to the function is called call by reference.
The call by reference function is written as
Which makes a alias of x and b alias of y and hence while function body exchanges a and b, it is same as
exchanging x and y.
Example:
#include<iostream.h>
void exch(int&, int&); //function prototype
void main( )
{
int x,y;
cout<<”Enter two numbers:”<<endl;
cin>>x>>y;
exch(x,y);
cout<<”After exchange:”<<endl;
cout<<x<<” ”<<y;
}
void exch(int& a,int& b)
{
int temp;
temp=a;
a=b;
b=temp;
}
Example:
#include<iostream.h>
using namespace std;
const float pie=3.14;
float area(float r) //function to compute area of circle
{
return(pie*r*r);
}
float area(float 1,float b) //function to compute area of rectangle
{
return(l*b);
}
void main()
{
float radius;
cout<<”Enter radius:”;
cin>>radius;
cout<<”Area of circle=”<<area(radius)<<endl;
float length, breadth;
cout<<”Enter length and breadth:”;
cin>>length>>breadth;
cout<<”Area of rectangle=”<<area(length, breadth)<<endl;
}
Output:
Enter radius:3.4
Department of Computer Engineering Page 5
Subject Name: Basic Object Oriented Programming Unit No: 2 Subject Code: 4320702
Area of circle:36.298404
Enter length and breadth: 3 4
Area of rectangle:12.000000
Example:
#include<iostream.h>
using namespace std;
const float pie=3.14;
inline float area(float r)
{
return (pie*r*r);
}
void main()
{
float radius;
cout<<”Enter radius:”;
cin>>radius;
cout<<”Area=”<<area(radius)<<endl;
}
Whenever function is called and value for such argument is not passed, compiler uses the default value.
Example:
#include<iostream>
using namespace std;
void set_point(int x,int y=0); //default arguments
void main()
{
int p,q;
cout<<”Enter x coordinate:”;
cin>>p;
set_point(p);
cout<<”Enter x and y coordinates:”
cin>>p>>q;
set_point(p,q);
}
//function to set the point
void set_point(int x,int y)
{
cout<<”(“<<x<<”,”<<y<<”)”<<endl;
}
Output:
Enter x coordinate:5
(5,0)
Enter x and y coordinates:6 8
(6,8)
Structure
2.7 A simple Structure
Structure is a collection of variables of different data types under a single name.
The struct keyword defines a structure type followed by an identifier (name of the structure).
When a structure is created, no memory is allocated.
Example:
struct person
{
char name[50];
int age;
float salary;
};
Syntax:
struct Structure_Name
{
datatype data_member1;
data_type data_member2;
--------------
data_type member N;
} struct_var1,struct_var2,… ;
Where ,
struct - keyword.
structure_Name - the name of structure
data_member1,data_member2 ,.. - the variables of different data types.
struct_var1, struct_var2,… - the variables of structure name type.
Example:
struct student
{
int rollno;
char name [50];
int marks;
}s1,s2;
Example:
struct student
{
int rollno;
char name[50];
int marks;
}s1,s2;
Here, s1 and s2 are structure variable.
Example:
struct student
{
int rollno;
char name [50];
int marks;
};
void main( )
{
struct student s1,s2;
}
Example:
struct student
{
int rollno;
char name [50];
int marks;
};
void main( )
{
struct student s1,s2;
s1.rollno=1; // accessing member rollno
s1.name=”ABC”; // accessing member name
s1.marks=60; // accessing member marks
}
Syntax:
structure_variable.member_name = value;
Example:
struct student
{
int rollno;
char name [50];
int marks;
};
void main( )
{
struct student s1,s2;
s1.rollno=1;
s1.name=”ABC”;
s1.marks=60;
}
Objects:
Object is a basic run-time entity in object oriented system.
Object is a one type of class variable.
Object can be declared as same as the variable declaration.
Declaration of class
The class declaration describes the type and scope of its members.
To declare the class we have to use the class keyword.
Syntax of class declaration:
class class-name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
The variable which is declared inside the class declaration that is known as data members.
The functions which is declared inside the class declarations that is known as member functions.
The data member and member functions are also known as the class members.
Private and public specifies the scope (where it is used) of the class members.
By default, the scope of the class members is private.
The class members which are declared as private can be accessed only from within the same class.
The class members which are declared as public can be accessed from outside the class also.
Only the member function of the class can access the private data member and the private member
functions of the same class.
Here, no and name are the data members which has the private scope.
And getdata( ), putdata( ) are the member functions which has the public scope.
Declaration of object:
Objects can be declared by two way
1) with class declaration
Access specifiers define how the members (variable or function) of a class can be accessed.
In C++, there are three access specifiers:
public - members are accessible from outside the class
private - members cannot be accessed (or viewed) from outside the class
protected - members cannot be accessed from outside the class, however, they can be
accessed in inherited classes.
By default, all members of a class are private if you don't specify an access specifier:
Example:
class student
{
int no; //private specifier
char name[20];
public:
};
void main( )
{
student s1;
s1.getdata( );
s1.putdata( );
}
2.16 Defining member function outside of the class using scope resolution operator
Member function definitions can be defined in two places:
1. Outside the class definition
2. Inside the class definition
Member function can be declared outside the class, if it is only declared inside class.
Definition of member function are like normal function
Whenever the definition of class member function outside the class, the member name must be qualified
by the class name using the scope resolution operator.
Syntax:
return_type class_name::function_name(Arguments)
{
Statements;
}
Example:
class student
{
int no;
char name[20];
public:
void getdata( );
void putdata( ) // definition inside class
{
cout<< “Number : “<<no<<” Name :”<<name;
}
};
void student::getdata( )
{
cout<<”Enter Number: “;
cin>>no;
cout<<”Enter Name: “;
cin>>name;
}
2.17 private member function
A function declared inside the private access specifier of the class, is known as a private member
function.
Normally we define data members as private and function member as public.
But in some cases we require to declare function as private.
Example:
class array
{
private:
int a[5];
void swap(int i,int j) //Private function
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Public:
void getarray( )
{
for(int i=0,i<5;i++)
cin>>a[i];
}
void sort( )
{
for(int i=0,i<4;i++)
{
for(int j=i+1;j<5;j++)
{
if(a[i]>a[j])
swap(i,j);
}
}
}
void putarray( )
{
for(int i=0,i<5;i++)
cout<<a[i];
}
};
void main( )
{
array a1;
a1.getarray( );
a1.sort( );
a1.putarray( );
}
Example:
int item :: count;
where, item is the class-name and count is the static data member.
#include<iostream.h>
#include<conio.h>
class item
{
int code;
static int count;
public:
---------
---------
};
int item::count;
void main()
{
---------
---------
The member function of the class which is declared with the static keyword, it is called as static
member functions.
Example:
#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public:
void setcode( );
void showcode( );
static void showcount( )
{
cout<<"Count : "<<count<<endl;
Department of Computer Engineering Page 17
Subject Name: Basic Object Oriented Programming Unit No: 2 Subject Code: 4320702
}
};
int test::count;
void main( )
{
--------
--------
test::showcount( );
--------
--------
test::showcount( );
--------
--------
}
void main ()
{
books b[3] ;
for(int i=0;i<3;i++)
{
cout<<"Enter details o£ book "<<(i+1)<<"\n";
b[i].getdata();
}
for(int i=0;i<3;i++)
{
cout<<"\nBook "<<(i+l)<<"\n";
b[i].putdata() ;
}
getch( );
}
Example:
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
void readData()
{
cout << "Enter real and imaginary number respectively:"<<endl;
cin >> real >> imag;
}
void addComplexNumbers(Complex comp1, Complex comp2)
{
real=comp1.real+comp2.real;
imag=comp1.imag+comp2.imag;
}
void displaySum()
{
cout << "Sum = " << real<< "+" << imag << "i";
}
};
void main()
{
Complex c1,c2,c3;
c1.readData();
c2.readData();
c3.addComplexNumbers(c1, c2); // Object c1,c2 as a function argument
c3.displaySum();
getch( );
}
Example:
#include<iostream.h>
#include<conio.h>
class ABC; //Forward declaration of class
class XYZ
{
int a;
public:
void setvalue(int x)
{
a=x;
}
When an object is returned by value from a function, a temporary object is created within the
function, which holds the return value.
This value is further assigned to another object in the calling function.
Example:
#include <iostream>
using namespace std;
class Example
{
private:
int temp;
public:
void readData(int i)
{
temp=i;
}
Syntax:
friend class ClassName;
Example:
#include <iostream>
using namespace std;
class A
{
int x =5;
void main()
{
A a;
B b;
b.display(a);
return 0;
}
Output:
X is: 5
When a class is declared a friend class, all the member functions of the friend class become friend
functions.
Since Class B is a friend class, we can access all members of Class A from inside Class B.
However, we cannot access members of Class B from inside Class A. It is because friend relation in
C++ is only granted, not taken.