C++ Struct Functions

This tutorial explains how to create functions in a C++ Struct.

Often we come across a scenario where we need to group together many variables or functions into a single object. In order to do so, we need to define our own custom data type or “Structures” as we call them in C++ and C.

While C Structs are fairly simple and can only hold variables, a C++ Struct has several additional features, one of which we will be focusing on in this tutorial, which is the ability to hold functions.


Functions in C++ Struct

Let’s take a look at a simple example here. If you already have experience with Classes in C++, then you should know it’s the exact same procedure. The only difference is that by default in Structs, all variables and functions are public, whereas in Classes they are private by default.

The below example creates a simple function in a Struct, that prints out the values of all the variables in it.

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    char gender;

    void print() {
        cout << age << " - ";
        cout << name << " - ";
        cout << gender;
    }
};

int main() {
    Person p1 = { "Bob", 15, 'M' };
    Person p2 = { "Janet", 17, 'F' };

    p1.print();
    p2.print();
}

The above code prints out the following:

15 - Bob - M
17 - Janet - F

Further Examples

Let’s take a look at some other examples and use-cases to further develop the concept.

Operator Overloading with a Struct

Here’s an example that uses Operator Overloading with a Struct. Now obviously if you normally tried to add two objects of the Struct “Pair”, it would give an error right? But not if we overload the + operator, and define behavior for adding two Pair objects together.

#include <iostream>
using namespace std;

struct Pair {
    int x, y;

    void print() {
        cout << "(" << x << "," << y << ")\n";
    }

    Pair operator+ (Pair &p2) {
        Pair temp;
        temp.x = x + p2.x;
        temp.y = y + p2.y;
        return temp;
    }
};

int main() {
    Pair p1 = {5, 3};
    Pair p2 = {1, 2};

    Pair p3 = p1 + p2;
    p3.print();
}

Output:

(6,5)

Want to learn more about Operator Overloading? Check out this tutorial on Operator Overloading in C++.


Function Overloading with a C++ Struct

Here’s an example that uses Function Overloading with a Struct. For those who don’t know, it’s a concept where we create multiple function definitions with the same name, but different type/number of parameters. This allows us to make function calls in multiple ways.

Whichever function definition matches the type and number of parameter, gets executed.

#include <iostream>
using namespace std;

struct Calculator {

    int add(int num1, int num2) {
        return num1 + num2;
    }

    int add(int num1, int num2, int num3) {
        return num1 + num2 + num3;
    }
};

int main() {
    Calculator calculator;

    cout << calculator.add(5, 7) << endl;
    cout << calculator.add(9, 2, 5) << endl;
}

12
16

Want to learn more about Function Overloading? Check out this tutorial on Function Overloading in C++.


Constructor in C++ Struct

Here’s another interesting example where we have created a Constructor in the struct. Instead of using the regular Initializer Lists, we can use the Constructor instead, which many people would likely prefer.

The below examples show both an object created from the standard initializer list, as well as the constructor.

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    char gender;

    void print() {
        cout << age << " - ";
        cout << name << " - ";
        cout << gender << endl;
    }
};

struct Student : Person {
    int RollNo;
    string department;

    Student(string name, int age, char gender, int roll, string dept) 
    {
        this->name = name;
        this->age = age;
        this->gender = gender;
        this->RollNo = roll;
        this->department = dept;
    }
};

int main() {
    Student s1 = { "Bob", 15, 'M', 146, "IT" };
    Student s2 = Student("Janet", 17, 'F', 109, "Medical");

    s1.print();
}

This marks the end of the C++ Struct Functions Tutorial. Any suggestions or contributions are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments