0% found this document useful (0 votes)
5 views23 pages

C++ Unit-3

The document discusses function overloading, friend functions, and constructors in C++. It explains how function overloading allows multiple functions with the same name but different parameters, and how friend functions can access private members of a class. Additionally, it covers the concept of constructors, including default and parameterized constructors, and provides examples for each concept.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

C++ Unit-3

The document discusses function overloading, friend functions, and constructors in C++. It explains how function overloading allows multiple functions with the same name but different parameters, and how friend functions can access private members of a class. Additionally, it covers the concept of constructors, including default and parameterized constructors, and provides examples for each concept.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

MOUDLE –III

31. FUNCTION OVERLOADING:

 Declaring or defining multiple number of functions with same function name and with
different types of arguments/parameters is called function overloading.
 Function overloading is used to perform similar kind of operations on different
parameters with different functions which contains same function name.
Ex: InCalculator we can add integers and float values
int add(int,int); //Addition of two integers
float add(float,float); // Addition of two float values
char* add(char[],char[]); //Addition of two strings

Example1:
#include<iostream>
using namespace std;
void person()
{
cout<<"This is raj"<<endl;
}
void person(int age)
{
cout<<"age is"<<age<<endl;
}
void person(double wt1)
{
cout<<"weight is"<<wt1<<endl;
}
void person(char gen,float wt)
{
cout<<"gender is "<<gen<<" weight is "<<wt<<endl;
}
int main()
{
void person();
void person(double);
void person(int);
void person(char,float);
person();
person(25);
person(125.5);
person('m',68.5);
return 0;
}
Output:
This is raj
age is25
weight is125.5
gender is m weight is 68.5

1
Example2:
#include<iostream>
using namespace std;
double area(float r)
{
return 3.142*r*r;
}
int area(int w,int l)
{
return w*l;
}
float area(float b,float h)
{
return 0.5*b*h;
}
int area(int s)
{
return s*s;
}
main()
{
int s,w,l;
float r,b,h;
cout<<"Enter radius of a circle";
cin>>r;
cout<<"Enter width and length of rectangle";
cin>>w>>l;
cout<<"Enter side of a square";
cin>>s;
cout<<"Enter breadth and height of a triangle";
cin>>b>>h;
cout<<"\n Area of a circle is "<<area(r);
cout<<"\n Area of a rectangle is "<<area(w,l);
cout<<"\n Area of a square is "<<area(s);
cout<<"\n Area of a triangle is "<<area(b,h);
}
Output:
Enter radius of a circle3.2
Enter width and length of rectangle4 6
Enter side of a square5
Enter breadth and height of a triangle3 3

Area of a circle is 32.1741


Area of a rectangle is 24
Area of a square is 25
Area of a triangle is 4.5

2
Example 3:
#include<iostream>
#include<cstring>
using namespace std;
void add()
{
cout<<"This is function overloading"<<endl;
}
int add(int a,int b)
{
return a+b;
}
float add(float x,float y)
{
return x+y;
}
float add(int a,float y)
{
return a+y;
}
float add(float x,int b)
{
return x+b;
}
char* add(char s1[15],char s2[15])
{
int i,j;
strcat(s1,s2);
return s1;
}
int main()
{
char s1[]="Rama";
add();
cout<<add(1,2)<<endl;
cout<<add(2.3f,4.5f)<<endl;
cout<<add(2,4.5f)<<endl;
cout<<add(2.5f,4)<<endl;
cout<<add(s1,"Lakshman")<<endl;
return 0;
}
Output:
This is function overloading
3
6.8
6.5
6.5
RamaLakshman

3
3.2 FRIEND FUNCTION:
Friend function is a non-member function which can access the private members of a class.

Syntax:
class class_name
{
Statement1;
friend returntype funct_name(arguments);
};

 We can declare a function as friend by placing the keyword friend preceding the
function definition inside the class.
 We can define friend function both inside and outside the class, but while defining
friend function outside the class we shouldn’t use class name with scope resolution
operator.
 To access the data members of a class inside a friend function we have to use the
object of that particular class.
 Inmainfunction we can call the friend function directly without using objects.
 While accessing the data members inside a friend function we have to use the object
and that object can be declared in global area orwe can pass the object as argument to
the friend function.

Ex:-friend function by declaring object in global area.


#include<iostream>
using namespace std;
class family
{
long int c,a;
public:
void read();
friend void show();
void print();
}F;
void family ::read()
{
c=970489;
a=999977;
}
void family ::print()
{
cout<<"inside the member function \n";
cout<<"cell no is "<<c<<endl;
cout<<"atm card is "<<a<<endl;
}
void show() //friend function
{
cout<<"inside the friend function \n";
cout<<"cell no is "<<F.c<<endl;
cout<<"atm card is "<<F.a<<endl;
}

4
main()
{
F.read();
F.print();
show();//no object should be use to call the friend function
}
Output:
inside the member function
cell no is 970489
atm card is 999977
inside the friend function
cell no is 970489
atm card is 999977

Example 2:
Friend function by declaring object as argument and returning object

#include<iostream>
using namespace std;
class sum
{
int a;
int b;
public :
void read();
friend sum add(sum,sum);
void show();
};
void sum::read()
{
cout<<"enter two values";
cin>>a>>b;
}
sum add(sum X,sum Y)
{
sum Z;
Z.a=X.a+Y.a;
Z.b=X.b+Y.b;
return Z;
}
void sum::show()
{
cout<<"sum of two objects of each variable is";
cout<<" a="<<a<<" b="<<b;
}
main()
{
sum A,B,C;
A.read();
B.read();
5
C=add(A,B);
C.show();
}
Output:
enter two values2 3
enter two values4 5
sum of two objects of each variable is a=6 b=8

FUNCTION FRIENDLY TO TWO CLASSES :


#include<iostream>
using namespace std;
class XYZ;//class declaration
class ABC
{
int a;
public:
void read()
{
cout<<"enter a value";
cin>>a; }
friend void sum(ABC,XYZ);
};
class XYZ // class definition
{
int b;
public:
void access()
{
cout<<"enter b value";
cin>>b;
}
friend void sum(ABC,XYZ);
};
void sum(ABC ob1,XYZ ob2)
{
cout<<"sum is"<<(ob1.a+ob2.b);
}
main()
{
ABC A;
XYZ X;
A.read();
X.access();
sum(A,X);
}
Output:
enter a value 5
enter b value 6
sum is 11

6
FRIEND MEMBER FUNCTION/ A MEMBER FUNCTION FRIEND TO ANOTHER
CLASS:
A member function of a class can be a friend to another class, therefore it(friend member
function) can only access the private properties of another class.

Syntax:
class classname1
{
Statement1;
friend returntype classname2::funct_name(<arguments>);
};

Example:
#include<iostream>
using namespace std;
class B;//class declaration
class A
{
int a;
public:
void reada()
{
cout<<"enter a value";
cin>>a; }
void sum(B);
};
class B // class definition
{
int b;
public:
void readb()
{
cout<<"enter b value";
cin>>b;
}
friend void A::sum(B);
};
void A::sum(B obb)//Should define only outside the class but not inside the class
{
cout<<"\n a of class A is "<<a;
cout<<"\n b of class B is"<<obb.b;
cout<<"\n Sum is"<<a+obb.b;
}
main()
{
A oa;
B ob;
oa.reada();
ob.readb();
oa.sum(ob);
}

7
Output:
enter a value 5
enter b value 6
a of class A is 5
b of class B is 6
Sum is 11

FRIEND CLASS:
A friend class is used to access the private properties of another class.
Syntax:

class classname1
{
Statement1;
friend class classname2;
};
Note:
1. In the above syntax classname2 can access the private properties of classname1 but
classname1 cannot access the private properties of classname2.
2. If both the classes want to access the private properties of each other, then both
classes should be friend to each other.

#include<iostream>
using namespace std;
class Girl;
class Boy
{
int b;
public:
void readb()
{
cout<<"Enter boy age";
cin>>b;
}
friend class Girl;
};
class Girl
{
int g;
public:
void readg(Boy &B)
{ B.readb();
cout<<"Enter girl age";
cin>>g;
}
void showg(Boy &B)
{
cout<<"b= "<<B.b;
cout<<"g= "<<g;
}
};

8
main()
{
Boy B; Girl G;
G.readg(B);
G.showg(B);
}
Output:
Enter boy age 19
Enter girl age 18
b= 19 g= 18

3.3 OBJECT AS ARGUMENT:

Object is used as an argument to pass different type of heterogeneous values from one
function to another to perform a task.
Example:
#include<iostream>
using namespace std;
class student
{
public:
int n;
char g,name[20];
float per;
short age;
}ob;
int main()
{
void show(student);
cout<<"enter rno";
cin>>ob.n;
cout<<"enter name";
cin>>ob.name;
cout<<"enter gender(m/f)";
cin>>ob.g;
cout<<"enter percentage";
cin>>ob.per;
cout<<"enter age";
cin>>ob.age;
show(ob);
cout<<"\n end of main";
return 0;
}
void show(student s)
{
cout<<"name is "<<s.name<<endl;
cout<<"rno is "<<s.n<<endl;
cout<<"per is "<<s.per<<endl;
cout<<"gender is "<<s.g<<endl;
cout<<"age is "<<s.age;
}

9
Output:
enter rno 12
enter name Raj
enter gender(m/f) m
enter percentage 98
enter age 19
name is Raj
rno is 12
per is 98
gender is m
age is 19
end of main

Example:2
#include<iostream>
using namespace std;
class time
{
int hrs;
int min;
public:
void gettime(int h,int m)
{
hrs=h;
min=m;
}
void puttime()
{
cout<<hrs<<"hours "<<min<<"minutes";
}
void sum(time,time);
};
void time::sum(time t1,time t2)
{
min=t1.min+t2.min;
hrs=min/60;
min=min%60;
hrs=hrs+t1.hrs+t2.hrs;
}
int main()
{
time T1,T2,T3;
T1.gettime(2,45);
T2.gettime(3,30);
T3.sum(T1,T2);
cout<<"\n T1=";T1.puttime();
cout<<"\n T2=";T2.puttime();
cout<<"\n Total=";T3.puttime();
return 0;
}

10
Output:

T1=2hours 45minutes
T2=3hours 30minutes
Total=6hours 15minutes

3.4CONSTRUCTORS:
 Constructor is a special member function where classname and function name is same
without any return type.
 Constructors are used to initialize values of data members of it’s own class.
 Constructor invokes automatically whenever object is created and doesn’t matter how
many objects are declared.That means if ‘n’ objects are created then ‘n’ number of
times constructor invokes automatically.
 Constructors have to define only under public visibility mode to access in the main
program.
 Constructors reduce time complexity.

Syntax:

class class_name
{
public:
class_name(args)
{
Statements1;
Statements2;
}
};
Example:
#include<iostream>
using namespace std;
class sum
{
int a;
public:
sum()
{
a=10;
}
void show()
{
cout<<"a="<<a;
}
};
main()
{
sum s1,s2;
s1.show();
s2.show();
return 0;
}
11
Output:
a=10 a=10

Example2:
#include<iostream>
using namespace std;
class test
{
static int a;
public:
test()
{
cout<<"\n Constructor called "<<++a<<" times";
}
}t1;
int test::a;
test t2;
main()
{
cout<<"\n This is main block function ";
test t3;
{
cout<<"\n This is t3 block function ";
test t4;
{
cout<<"\n This is t4 block function ";
test t5;
{
cout<<"\n This is t5 block function ";
}
}

}
}
Output:
Constructor called 1 times
Constructor called 2 times
This is main block function
Constructor called 3 times
This is t3 block function
Constructor called 4 times
This is t4 block function
Constructor called 5 times
This is t5 block function

12
Example 3:
#include<iostream>
using namespace std;
class arithmetic
{
int a,b;
public:
arithmetic();
int sum();
int sub();
int mul();
};
arithmetic::arithmetic()
{
cout<<"\n enter two ints";
cin>>a>>b;
}
int arithmetic::sum()
{
return a+b;
}
int arithmetic::sub()
{
return a-b;
}
int arithmetic::mul()
{
return a*b;
}
int main()
{
arithmetic t1;
cout<<" sum is"<<t1.sum();
arithmetic t2;
cout<<" sub is"<<t2.sub();
arithmetic t3;
cout<<" mul is"<<t3.mul();
return 0;
}
Output:

enter two ints 2 3


sum is 5
enter two ints 5 6
sub is -1
enter two ints 8 9
mul is 72

13
3.5 DEFAULT CONSTRUCTORS:

A constructor which have no parameters/arguments is called default constructor. If there is


no user defined default constructor then the system automatically created a default
constructor with no arguments.
Syntax:

class class_name
{
public:
class_name(void)
{
Statements1;
Statements2;
}
};

Example:
#include<iostream>
using namespace std;
class test
{
int a;
public:
test()
{
a=10;
}
void show()
{
cout<<"a="<<a;
}
};
main()
{
test t;
t.show();
return 0;
}
Output:
a=10

3.6 PARAMETERISED CONSTRUCTORS:

Parameters in constructors are used to pass the values to the data members through the
constructors from where object is created.

Parameterized constructors can invoke in two ways:


1.Implicit Parameterised calling technique
2.Explicit Parameterised calling technique

14
1. IMPLICIT PARAMETERISED CALLING:

This method is used to pass the parameters to the constructors at the time of object creation.
Syntax:
classnameobj(args);
Ex:
Test ob(10,20);
Gitam G(25);

2. EXPLICIT PARAMETERISED CALLING:

This method is used to pass the parameters to the constructors after creating the object to
enter new values into an existing object.
Syntax:
obj=classname(args);
Ex:
ob=Test(100,200);
G=Gitam(125);

Example:
#include<iostream>
using namespace std;
class result
{
int a,b;
public:
result(int p,int q)
{
a=p;
b=q;
}
void sum();
};
void result::sum()
{
cout<<"sum is"<<(a+b);
}
int main()
{
int p,q;
cout<<"enter two integers";
cin>>p>>q;
result r(p,q);
r.sum();
cout<<"\n enter two new integers";
cin>>p>>q;
r=result(p,q);
r.sum();
}

15
Output:

enter two integers 2 3


sum is 5
enter two new integers 5 6
sum is 11

3.7CONSTRUCTOR OVERLOADING OR MULTIPLE CONSTRUCTORS:

Constructor overloading is defining or declaring multiple no.of constructors with same


function name and with different arguments.
syntax:
class class_name
{
public:
class_name(void);
class_name(datatype1);
class_name(datatype1,datatype2);
};

EXample:
#include<iostream>
using namespace std;
class result
{
int a,b;
float r;
public:
result()
{}
result(int p,int q)
{
a=p;
b=q;
}
result(float rd)
{
r=rd;
}
void area_rect()
{
cout<<"area of rectangle"<<(a*b);
}
void area_cir()
{
cout<<"area of circle is"<<(3.14*r*r);
}
};

16
main()
{
result R1,R2;
int l,b;
float f;
cout<<"enter length and breadth";
cin>>l>>b;
cout<<"enter radius";
cin>>f;
R1=result(f);
R2=result(l,b);
R1.area_cir();
R2.area_rect();
}
Output:
enter length and breadth 2 6
enter radius 2.5
area of circle is 19.625
area of rectangle 12

3.8CONSTRUCTORS WITH DEFAULT ARGUMENT:

 Assigning values to the arguments at the time of constructor declaration is known as


constructor with default arguments.
 The main purpose of using default arguments if any value of data member is fixed for all
the objects and with no frequent change then we can use constructor with default
argument.

Syntax:
class classname
{
public:
classname(datatype1=var,datatype2=var1);
};

Example:
#include<iostream>
using namespace std;
class bank
{
float per;
double bal;
public:
bank(double,float=4.5);
void show()
{
cout<<" interest per yr is"<<(bal*per)/100;
}
};

17
bank::bank(double b,float p)
{
bal=b;
per=p;
}
main()
{
bank sbi(20000);
bank ubi(40000,5.5);
cout<<"\n in sbi bank";
sbi.show();
cout<<"\n in ubi bank";
ubi.show();
return 0;
}
Output:

in sbi bank interest per yr is 900


in ubi bank interest per yr is 2200

3.9COPY CONSTRUCTOR:
Copy constructor is copying the selected items from one object to another object of same
class

Syntax:
class name(classname&obj)
{
datavar1=obj.datavar1;
datavar2=obj.datavar2;
}
 Copy constructor should define using reference object as parameter.
 In copy constructor we can use only one argument.
 copy constructor can invoke in two methods
1. By passing object as argument using braces at the time of object creation like
classname ob2(ob1);
2. By using assignment operator at the time of object creation
classname ob2=ob1;

Note:
1. Copy constructor should invoke only at the time of object creation but not after creation
2. In copy constructor we have to implement constructor overloading.
3. If copy constructor is not defined then the compiler supplies its own copy constructor and
copies all the values into another object.

18
Example:
#include<iostream>
using namespace std;
class test
{
int a,b,c,d;
public:
test(){} //Empty or dummy constructor
test(test &ob)//User defined Copy Constructor
{
cout<<"copy constructor called \n";
a=ob.a;
c=ob.c;
}
test(int x,int y,int z,int p)
{
a=x;
b=y;
c=z;
d=p;
}
void show()
{
cout<<"a="<<a<<" b="<<b<<endl;
cout<<"c="<<c<<" d="<<d<<endl;
}
};
main()
{
test a;
a=test(10,20,30,40);
a.show();
test b=a;//copy constructor invoked
b.show();
test c(a);//copy constructor invoked
c.show();
test d=a;
d.show();
return 0;
}

Output:
a=10 b=20
c=30 d=40
copy constructor called
a=10 b=7081808
c=30 d=4247087
copy constructor called
a=10 b=1
c=30 d=0
copy constructor called

19
a=10 b=4
c=30 d=4199951

3.10 DYANAMIC CONSTRUCTOR:

Dynamic constructor is allocating memory dynamically to object through the constructors


using new operator.
Syntax:

classname(args)
{
datavar=new datatype[size];
}
Example:
#include<iostream>
#include<cstring>
using namespace std;
class test
{
char *name;
int n;
public:
test(int x)
{
n=x;
name=new char[n];
}
void read();
void show();
};
void test::read()
{
cout<<"\n Enter ur name";
cin>>name;
}
void test::show()
{
cout<<"\n Name is "<<name;
strrev(name);
cout<<"\n Reverse of the name is "<<name;
}
main()
{
cout<<"\n Enter length of ur name ";
int len;
cin>>len;
test t(len);
t.read();
t.show();
return 0;
}

20
Output:
Enter length of ur name 4
Enter ur name rani
Name is rani
Reverse of the name is inar

3.11 DESTRUCTORS:

 Destructor is a special member function where the class name and function name is same
with no return type and no arguments.
 Destructor is represented with (~) tilde symbol at preceding destructor definition.
 Destructor invokes automatically whenever the scope of the object is completed by the
system.
 Destructor is used to destroy the object which is created by the constructor.
Syntax:
class classname
{
classname()
{
Statement1;
Statement2;
}
~classname()
{
Statement1;
Statement2;
}
};
Note:
Destructor also should define in public visibility mode.

Example:
#include<iostream>
using namespace std;
class test
{
static int count;
public:
test()
{
cout<<"\n constructor invoked";
cout<<"count="<< ++count;
}
~test()
{
cout<<"\n destructor invoked";
cout<<"count="<< --count;
}
};
int test::count;

21
main()
{
test A;//Obj A created so constructor invokes automatically
{
test B; //Obj B created
{
test C; //Obj C created
{
test D; //Obj D created
}// scope of obj D completed, so Destructors invokes here automatically
}// scope of obj C completed
}// scope of obj B completed
}// scope of obj A completed

Output:
constructor invoked count=1
constructor invoked count=2
constructor invoked count=3
constructor invoked count=4
destructor invoked count=3
destructor invoked count=2
destructor invoked count=1
destructor invoked count=0

Example2:
#include<iostream>
#include<cstring>
using namespace std;
class test
{
char *name;
int n;
public:
test(int x)
{
n=x;
name=new char[n];
cout<<"Obj created by the constructor \n";
}
~test()
{
delete name;
cout<<"\n Obj destroyed by the destructor ";
cout<<"& Name is "<<name;//Prints Garbage value
}
void read();
void show();
};

22
void test::read()
{
cout<<"\n Enter ur name";
cin>>name;
}
void test::show()
{
cout<<"\n Name is "<<name;
strupr(name);
cout<<"\n Upper case of the name is "<<name;
}
main()
{
cout<<"\n Enter length of ur name ";
int len;
cin>>len;
test t(len);
t.read();
t.show();
return 0;
}
Output:

Enter length of ur name 4


Obj created by the constructor
Enter ur name viky
Name is viky
Upper case of the name is VIKY
Obj destroyed by the destructor & Name is ▬

*** End of Module III ***

23

You might also like