cpp Friend Class Function

Published on: 04 February 2025

Resources

  1. Video Lecture

Aim: Create a program to showcase the use of friend function.

Self Assessment

  1. Declare an Engine Class with one private variable "engineCC" and a public function "setEngine" to declare the value of Engine Class.
  2. Declare a Car Class as friend of Engine Class having a public function "Show" to show the vale of Engine Class variables.

Practice Exercise 3: Write a program to calculate the position of a particle showcasing the use of friend class.

Theory:

A friend class (Show) accesses the private position data of a Particle class. Show class can display the particle's position, which is initially set to (0, 0) and later updated.

Algorithm:

  1. Create a Particle object.
  2. Set the initial position of the Particle using setPosition(0, 0).
  3. Create a Show class object.
  4. Display the initial position using Show class's showPosition method.
  5. Move the Particle by calling move(2.0, 3.0).
  6. Display the updated position using Show class's showPosition method.

Code:

#include <iostream>
using namespace std;

class Particle {
private:
    float x, y;

public:
    void move(float dx, float dy) {
        x += dx;
        y += dy;
    }

    void setPosition(float newX, float newY) {
        x = newX;
        y = newY;
    }

    // Friend class declaration
    friend class Show;
};

// Friend class definition
class Show {
public:
    void showPosition(Particle p) {
        cout << "Particle Position: (" << p.x << ", " << p.y << ")" << endl;
    }
};

int main() {
    Particle p;
    Show show;

    p.setPosition(0, 0);

    // Print initial position using the friend class
    show.showPosition(p);

    // Move the particle
    p.move(2.0, 3.0);

    // Print final position using the friend class
    show.showPosition(p);

    return 0;
}

Practice Exercise 4: Write a program to calculate the position of a particle showcasing the use of friend function.

Theory:

The program models a simple 2D Particle object that can be moved along the x and y axes. It defines a move method to change the particle's position by adding specified displacements to its current coordinates. The printPosition friend function is used to access the Particle's internal data and print its current position. A friend function is a special function in C++ that can access private and protected members of the class, even though it is not a member of the class.

Algorithm:

  1. Create a Particle object with initial position (0.0, 0.0).
  2. Define a move method to adjust the x and y coordinates by specified amounts.
  3. Define a friend function printPosition to print the particle's position.
  4. Call printPosition to display the initial position of the particle.
  5. Move the particle by calling move(2.0, 3.0).
  6. Call printPosition again to display the updated position.

Code:

#include <iostream>
using namespace std;

class Particle {
private:
    float x, y;

public:
    void move(float dx, float dy) {
        x += dx;
        y += dy;
    }

    void setPosition(float newX, float newY) {
        x = newX;
        y = newY;
    }

    // Friend function declaration in public
    friend void printPosition(Particle p);
};

// Friend function definition
void printPosition(Particle p) {
    cout << "Particle Position: (" << p.x << ", " << p.y << ")\n";
}

int main() {
    Particle p;

    p.setPosition(0,0);

    // Print initial position using the friend function
    printPosition(p);

    // Move the particle
    p.move(2.0, 3.0);

    // Print final position using the friend function
    printPosition(p);

    return 0;
}

Extra Self Practice Material

Code:

#include <iostream>
#include <string>

using namespace std;

class Material
{
private:
    string name;
    double modulus_of_elasticity;
    double modulus_of_rigidity;
    double poisson_ratio;
    double unit_weight;

    // Friend function declaration in private
    friend void displayMaterialProperties(Material mat);

public:
    void setValues(string n, double E, double G, double v, double w)
    {
        name = n;
        modulus_of_elasticity = E;
        modulus_of_rigidity = G;
        poisson_ratio = v;
        unit_weight = w;
    }
};

// Friend function definition
void displayMaterialProperties(Material mat)
{
    cout << mat.name << "\t\t\t\t" << mat.modulus_of_elasticity << "\t\t\t\t\t"
         << mat.modulus_of_rigidity << "\t\t\t\t" << mat.poisson_ratio
         << "\t\t\t\t" << mat.unit_weight << endl;
}

int main()
{
    Material brass, cast_iron, copper, glass;

    // Using setter function to set values
    brass.setValues("Brass", 106.0, 40.1, 0.324, 83.8);
    cast_iron.setValues("Cast Iron", 100.0, 41.4, 0.211, 70.6);
    copper.setValues("Copper", 119.0, 44.7, 0.326, 87.3);
    glass.setValues("Glass", 46.2, 18.6, 0.245, 25.4);

    cout << "\n\t\t\t\t\t\t-------------------------------";
    cout << "\n\t\t\t\t\t\t    Material Property Table";
    cout << "\n\t\t\t\t\t\t-------------------------------\n";

    cout << "\n-----------------------------------------------------------------------------------------------------------------------------------------------";
    cout << "\nMaterial\t\t\tModulus of Elasticity(E)\t\tModulus of Rigidity(G)\t\tPoisson's Ratio(v)\tUnit Weight(w)\n\t\t\t\tGPa\t\t\t\t\tGPa\t\t\t\t\t\t\t  kN/m^3";
    cout << "\n-----------------------------------------------------------------------------------------------------------------------------------------------\n";

    // Using friend function to display material properties
    displayMaterialProperties(brass);
    displayMaterialProperties(cast_iron);
    displayMaterialProperties(copper);
    displayMaterialProperties(glass);

    return 0;
}

References

There may be some AI Generated content in this article used for demonstration purposes.