FUNCTIONS and OBJECTS (MEMBER FUNCTIONS) IN C++

 Functions and objects

 ·       In Object-Oriented Programming (OOP), functions that belong to a class are called member functions.

·       When these functions are accessed using an object, they operate on the data members of that object.

·       Objects store data, and functions operate on that data


 

Basic Structure of a Class with Functions

class ClassName {

    data_members;

    member_functions;

};

 

Example:

class Student {

    int roll;

    float marks;

 

public:

    void input();

    void display();

};

 

Types of Member Functions

Based on the Definition Location

  1. Inside the class
  2. Outside the class (using scope resolution operator::)

 

Member Function Defined INSIDE the Class

  • Function body written inside the class
  • Automatically treated as inline
  • Suitable for small functions

 

Example:

#include <iostream>

using namespace std;

 

class Student {

    int roll;

public:

    void setRoll(int r) {

        roll = r;

    }

 

    void showRoll() {

        cout << "Roll No: " << roll << endl;

    }

};

 

int main() {

    Student s1;

    s1.setRoll(101);

    s1.showRoll();

    return 0;

}

 

Member Function Defined OUTSIDE the Class

  • Function prototype inside class
  • Definition outside using ClassName::functionName
  • Improves readability for large programs

 

Example:

#include <iostream>

using namespace std;

 

class Student {

    int roll;

public:

    void setRoll(int);

    void showRoll();

};

 

void Student::setRoll(int r) {

    roll = r;

}

 

void Student::showRoll() {

    cout << "Roll No: " << roll << endl;

}

 

int main() {

    Student s1;

    s1.setRoll(102);

    s1.showRoll();

    return 0;

}


 

Accessing Member Functions Using Objects

Syntax:

objectName.functionName();

 

Example:

s1.showRoll();

 


Protected data cannot be accessed using an object

 Functions with Arguments and Return Value

 

Example:

class Calculator {

public:

    int add(int a, int b) {

        return a + b;

    }

};

 

int main() {

    Calculator c;

    cout << c.add(5, 3);

}


this Pointer in Member Functions

  • this is an implicit pointer
  • Refers to the current calling object
  • Used to resolve name conflicts

 

Example:

class Box {

    int length;

 

public:

    void set(int length) {

        this->length = length;

    }

 

    void show() {

        cout << length;

    }

};

 

Inline Member Functions

  • Defined inside class
  • Reduces function call overhead
  • Best for small functions

 

Example:

class Test {

public:

    void show() {

        cout << "Inline Function";

    }

};


Const Member Functions

  • Cannot modify object data
  • Ensures read-only access

 

Example:

class Demo {

    int x;

public:

    void set(int a) {

        x = a;

    }

 

    int get() const {

        return x;

    }

};


Static Member Functions

  • Belong to class, not object
  • Access only static data
  • Called using class name

 

Example:

class Count {

    static int c;

public:

    static void show() {

        cout << c;

    }

};

 

int Count::c = 10;

 

int main() {

    Count::show();

}


 

Passing Objects to Functions

Example:

class Test {

    int x;

public:

    void set(int a) {

        x = a;

    }

 

    void display(Test t) {

        cout << t.x;

    }

};


 

Returning Object from Function

Example:

class Add {

    int a;

public:

    void set(int x) {

        a = x;

    }

 

    Add sum(Add obj) {

        Add temp;

        temp.a = a + obj.a;

        return temp;

    }

 

    void show() {

        cout << a;

    }

};

Summary

Functions inside objects are called member functions
Accessed using the dot operator
They provide data abstraction & encapsulation
They can be inline, static, cost
this pointer links the function to the calling object. 

object can access public data but not protected data

Private members cannot be accessed directly by object. 

================================================================

Post a Comment

0 Comments