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

Destructor & Friend Function

The document provides an overview of destructors in C++, explaining their purpose, characteristics, and providing examples of their implementation. It also introduces friend functions, detailing their ability to access private and protected members of a class. Several code examples illustrate the concepts of constructors, destructors, and friend functions in C++.

Uploaded by

kalasingayvonne
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)
5 views7 pages

Destructor & Friend Function

The document provides an overview of destructors in C++, explaining their purpose, characteristics, and providing examples of their implementation. It also introduces friend functions, detailing their ability to access private and protected members of a class. Several code examples illustrate the concepts of constructors, destructors, and friend functions in C++.

Uploaded by

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

Object Oriented Programing MSc.

Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University College

Destructor in C++
What is a destructor?
Destructor is a special member function of a class that is executed whenever an object of its class
goes out of scope or whenever the delete expression is applied to a pointer to the object of that
class.
Destructor name is same as class name but preceded by (~)
For example if name of constructor is integer()
Then destructor is ~integer()
Characteristics of Destructor
1. It never takes any argument ////no parameters
2. Only one destructor is need to destroy.
3. Compiler automatically makes destructor.

Simple example for destructor

#include<iostream>

using namespace std;

class gabs

public:

gabs()

cout<< " Constrctor of gabs" <<endl; ////constructor

~gabs()

1
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University College

cout<< " Destrctor of gabs" <<endl; ////Destructor

};

int main()

gabs g1,g2();

return 0;

////////////////////////////////////////////////////////////////////////

Example 2

#include <iostream>

using namespace std;

class Rectangle

private:

int length;

int width;

public:

Rectangle() // Constructor

cout << "Constructor" << endl;

2
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University College

~Rectangle() // Destructor

cout << "Destructor" << endl;

void setDimension(int l, int b)

length = l;

width = b;

int getArea()

return length * width;

};

int main()

Rectangle rt;

rt.setDimension(7, 4);

cout << rt.getArea() << endl;

return 0;

3
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University College

Friend Function

What is a Friend Function in C++?

A friend function in C++ is defined as a function that can access private, protected and a public
member of a class.

The friend function is declared using the friend keyword inside the body of the class.

4
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University College

5
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University College

Example 3:

#include <iostream>

using namespace std;

class boss {

int salary;

public:

boss() //// default constructor

{}

6
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University College

boss(int a) /////parameter constructor

salary=a;

void display()

cout<<"Boss salary="<<salary<<endl;

friend void salary(boss); // friend function

};

int main()

boss b(5000);

b.display();

return 0;

You might also like