0% found this document useful (0 votes)
9 views12 pages

Lecture 6

The document discusses friend functions and copy constructors in C++. Friend functions allow access to private members of a class without being a member themselves, while copy constructors create new objects by initializing them with existing objects of the same class. It also highlights the importance of deep copying in classes with dynamic memory allocation.

Uploaded by

catsofthedayss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Lecture 6

The document discusses friend functions and copy constructors in C++. Friend functions allow access to private members of a class without being a member themselves, while copy constructors create new objects by initializing them with existing objects of the same class. It also highlights the importance of deep copying in classes with dynamic memory allocation.

Uploaded by

catsofthedayss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Lecture 6

Friend Function
Copy constructor

1
Friend Function
A friend function is a function that is not a member of a class but has access to the
class's private and protected members. Friend functions are not considered class
members; they are normal external functions that are given special access privileges.
Programming Example:
#include <iostream>
using namespace std;
class Distance
{
private:
int meter;
public:
friend int func(Distance d);
};
int func(Distance d)
{
d.meter=5;
return d.meter;
}
int main()
{
Distance D;
cout<<"Distace: "<<func(D);
return 0;
}
Friend Function Example
#include <iostream> void printWidth( Box box )
using namespace std; {
class Box cout << "Width of box : " <<
box.width <<endl;
{
}
double width;
int main( )
public: {
friend void printWidth( Box box ); Box b;
void setWidth( double wid ); b.setWidth(10.0);
}; printWidth( b);
// Member function definition return 0;
void Box::setWidth( double wid ) }
{
Output:
width = wid;
Width of box : 10
}
What are the characteristics of friend functions?

 A friend function is not in the scope of the class in which it has been declared as friend.

 It cannot be called using the object of that class.

 It can be invoked like a normal function without any object.

 Unlike member functions, it cannot use the member names directly.

 It can be declared in public or private part without affecting its meaning.

 Usually, it has objects as arguments.


Programming Example
#include<iostream> float mean(base ob)
using namespace std; {
class base return float(ob.val1+ob.val2)/2;
{ }
int val1,val2; main()
public: {
void get() base obj;
{ obj.get();
cout<<“This is an example of Friend Function:” ; cout<<"\nMean value is : "<<mean(obj);
cout<<“Enter two values:\n”; }
cin>>val1>>val2;
}
friend float mean(base ob);
};
Friend Classes
It is possible for one class to be a friend of another class. When this is the case, the
friend class and all of its member functions have access to the private members defined
within the other class. For example,
#include <iostream>
using namespace std; int Min::min(TwoValues x)
class TwoValues { {
return x.a < x.b ? x.a : x.b;
int a; }
int b; int main()
public: {
TwoValues ob(10, 20);
TwoValues(int i, int j) { a = i; b = j; } Min m;
friend class Min; cout << m.min(ob);
return 0;
};
}
class Min {
public:
int min(TwoValues x);
};
Copy constructor
Copy constructor is a constructor which creates an object by
initializing it with an object of the same class, which has
been created previously.

The copy constructor is used to:


• Initialize one object from another of the same type
• Copy an object to pass it as an argument to a function
• Copy an object to return it from a function
• If a copy constructor is not defined in a class the compiler
itself defines one (default copy constructor).
• If the class has pointer variables and has some dynamic
memory allocations, then it is a must to have a copy
constructor.
• It must make a deep copy with an explicitly defined copy
constructor

Common form of copy constructor is shown here:


classname ( classname &obj)
{
// body of constructor
}
copy constructor Example

int main() {
class Example {
Example Object(10,20);
int a,b;
public: //Copy Constructor
Example(int x,int y) { Example Object2=Object;
a=x;
b=y;
// Constructor invoked.
cout<<"\nIm Constructor";
} Object.Display();
Object2.Display();
void Display() {
cout<<"\nValues :"<<a<<"\t"<<b;
return 0;
}
}; }
copy constructor Example
#include <iostream> int main( )
using namespace std;
{
class cc
{ cc ob(100);
int a; cc ob1(ob);
public: ob.show();
cc(int x){
ob1.show();
a=x;
}
}
void show(){
cout<<"a="<<a<<endl;
}
cc(cc &o){
a=o.a;
}
};
Deep Copy
#include <iostream> void set(int m){
*x=m;
using namespace std;
}
class A{
private: void show(){
int *x; cout<<*x<<endl;
}
public:
};
A(int p){
x=new int; int main()
*x=p; {
A ob(10);
A ob1=ob;
} ob.show();
A( A & obj) ob1.show();
{ ob1.set(20);
ob.show();
ob1.show();
x = new int;
*x = *(obj.x); return 0;
} }

You might also like