0% found this document useful (0 votes)
3 views8 pages

Program For Destructor

The document contains several C++ programming examples demonstrating key concepts such as destructors, call by reference, operator overloading, and the differences between call by value and call by reference. It includes code snippets for creating a Student class with a destructor, swapping values using call by reference, and overloading operators for a Box class. Each example is accompanied by its expected output, illustrating how the concepts work in practice.

Uploaded by

puriamit904
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)
3 views8 pages

Program For Destructor

The document contains several C++ programming examples demonstrating key concepts such as destructors, call by reference, operator overloading, and the differences between call by value and call by reference. It includes code snippets for creating a Student class with a destructor, swapping values using call by reference, and overloading operators for a Box class. Each example is accompanied by its expected output, illustrating how the concepts work in practice.

Uploaded by

puriamit904
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

Program for Destructor

#include<iostream>

using namespace std;

class Student {

public:

string name;

// Constructor

Student(string n) {

name = n;

cout << "Student " << name << " created." << endl;

// Destructor

~Student() {

cout << "Student " << name << " destroyed." << endl;

void display() {

cout << "Student Name: " << name << endl;

};
int main() {

Student s1("Alice");

Student s2("Bob");

[Link]();

[Link]();

cout << "End of main." << endl;

return 0;

---

## Output

Student Alice created.

Student Bob created.

Student Name: Alice

Student Name: Bob

End of main.

Student Bob destroyed.

Student Alice destroyed.


simple C++ program using Call by Reference:

cpp
#include <iostream>
using namespace std;

// Call by Reference — using&symbol


void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
cout << "Inside function: a = " << a << ", b = " << b << endl;
}

void doubleValue(int &num) {


num = num * 2;
cout << "Inside function: num = " << num << endl;
}

int main() {
// Example 1 — Swap two numbers
int x = 10, y = 20;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;

cout << "----------------------------" << endl;

// Example 2 — Double a number


int num = 5;
cout << "Before double: num = " << num << endl;
doubleValue(num);
cout << "After double: num = " << num << endl;

return 0;
}
```
---

## Output
```
Before swap: x = 10, y = 20
Inside function: a = 20, b = 10
After swap: x = 20, y = 10
----------------------------
Before double: num = 5
Inside function: num = 10
After double: num = 10

Call by Value vs Call by Reference

cpp
// Call by VALUE — original NOT changed
void addTen(int a) {
a = a + 10; // only changes the copy
}

// Call by REFERENCE — original IS changed


void addTen(int &a) {
a = a + 10; // changes the actual variable
}
Key Points about Call by Reference:

 Use&before parameter name


 Function works on the original variable directly
 Any change inside the function reflects outside too
 No copy is made — saves memory
 Commonly used for swapping values and modifying multiple variables

Here's a simple C++ program using Operator Overloading:

cpp
#include <iostream>
using namespace std;
class Box {
public:
int length, width, height;

// Constructor
Box(int l, int w, int h) {
length = l;
width = w;
height = h;
}

// Overload + operator (add two boxes)


Box operator+(Box const &b) {
return Box(length + [Link],
width + [Link],
height + [Link]);
}

// Overload == operator (compare two boxes)


bool operator==(Box const &b) {
return (length == [Link] &&
width == [Link] &&
height == [Link]);
}

// Overload>operator (compare volume)


bool operator>(Box const &b) {
int vol1 = length * width * height;
int vol2 = [Link] * [Link] * [Link];
return vol1 > vol2;
}

void display() {
cout << "Length: " << length
<< " Width: " << width
<< " Height: " << height
<< " Volume: " << (length * width * height)
<< endl;
}
};

int main() {
Box b1(3, 4, 5);
Box b2(2, 3, 4);

cout << "Box 1 ->"; [Link]();


cout << "Box 2 ->"; [Link]();

cout << "----------------------------" << endl;

// Using overloaded + operator


Box b3 = b1 + b2;
cout << "b1 + b2 ->"; [Link]();

cout << "----------------------------" << endl;

// Using overloaded == operator


if (b1 == b2)
cout << "Box 1 and Box 2 are EQUAL." << endl;
else
cout << "Box 1 and Box 2 are NOT equal." << endl;

cout << "----------------------------" << endl;

// Using overloaded>operator
if (b1>b2)
cout << "Box 1 is BIGGER than Box 2." << endl;
else
cout << "Box 2 is BIGGER than Box 1." << endl;

return 0;
}
```
---

## Output
```
Box 1 -> Length: 3 Width: 4 Height: 5 Volume: 60
Box 2 -> Length: 2 Width: 3 Height: 4 Volume: 24
----------------------------
b1 + b2 -> Length: 5 Width: 7 Height: 9 Volume: 315
----------------------------
Box 1 and Box 2 are NOT equal.
----------------------------
Box 1 is BIGGER than Box 2.

What is Operator Overloading?

cpp
// Without overloading — you cannot do this
Box b3 = b1 + b2; // ❌ Error — compiler doesn't know how to add objects

// With overloading — now this works!


Box b3 = b1 + b2; // ✅ Works — we defined what + means for Box

Operators You Can Overload

Operator Symbol Example Use


Addition + Add two objects
Subtraction - Subtract two objects
Multiplicatio
* Multiply values
n
Equal to == Compare two objects
Greater than > Compare sizes
Less than < Compare sizes
Assignment = Copy one object to another
Input >> Read object from cin
Output << Print object to cout
Operators You CANNOT Overload

Operator Symbol
Scope resolution ::
Member access .
Ternary ?:
Sizeof sizeof
Key Points about Operator Overloading:

 Use keyword operator followed by the symbol


 Makes code more readable and natural
 Does not change the original meaning for built-in types
 Can overload most operators except the 4 listed above
 Return type depends on what the operator should produce

You might also like