Wednesday, January 7, 2026

Function, Class and Object

  I. Functions in C++

A function is a block of code that performs a specific task.

It helps in code reusability, modularity, and readability.

General Syntax

Copy code

Cpp

return_type function_name(parameters)

{

    // statements

}

1. Inline Functions

An inline function is a function whose function call is replaced by the function code itself during compilation.

Purpose

Avoids function call overhead

Increases execution speed

Used for small functions

Syntax

Copy code

Cpp

inline return_type function_name(parameters)

Example

Copy code

Cpp

#include <iostream>

using namespace std;


inline int square(int x)

{

    return x * x;

}


int main()

{

    cout << square(5);

    return 0;

}

Advantage

✔ Faster execution

Limitation

❌ Not suitable for large functions

2. Function Overloading

Function overloading allows multiple functions with the same name but different parameters.

Purpose

Improves code clarity

Implements compile-time polymorphism

Rules

Functions must differ in:

Number of arguments

Type of arguments

(Return type alone is NOT enough)

Example

Copy code

Cpp

#include <iostream>

using namespace std;


int add(int a, int b)

{

    return a + b;

}


float add(float a, float b)

{

    return a + b;

}


int main()

{

    cout << add(10, 20) << endl;

    cout << add(2.5f, 3.5f);

    return 0;

}

II. Classes and Objects

Class

A class is a blueprint that contains data members and member functions.

Object

An object is an instance of a class.

1. Declaring Objects

Syntax

Copy code

Cpp

class_name object_name;

Example

Copy code

Cpp

class Student

{

public:

    int roll;

};


Student s1;

2. Defining Member Functions

Member functions can be defined:

Inside the class

Outside the class using scope resolution operator (::)

Example (Outside class)

Copy code

Cpp

#include <iostream>

using namespace std;


class Sample

{

public:

    void show();

};


void Sample::show()

{

    cout << "Hello C++";

}


int main()

{

    Sample s;

    s.show();

    return 0;

}

3. Static Member Variables and Functions

Static Member Variable

Shared by all objects

Only one copy exists

Static Member Function

Can access only static members

Called using class name

Example

Copy code

Cpp

#include <iostream>

using namespace std;


class Count

{

public:

    static int c;


    static void show()

    {

        cout << "Count = " << c;

    }

};


int Count::c = 10;


int main()

{

    Count::show();

    return 0;

}

4. Array of Objects

Used when multiple objects of the same class are needed.

Example

Copy code

Cpp

#include <iostream>

using namespace std;


class Student

{

public:

    int roll;

};


int main()

{

    Student s[3];

    s[0].roll = 101;

    s[1].roll = 102;

    s[2].roll = 103;


    for(int i=0;i<3;i++)

        cout << s[i].roll << endl;


    return 0;

}

5. Friend Functions

A friend function can access private and protected members of a class.

Purpose

Allows external function to access class data

Example

Copy code

Cpp

#include <iostream>

using namespace std;


class Sample

{

private:

    int x;

public:

    Sample()

    {

        x = 10;

    }

    friend void show(Sample s);

};


void show(Sample s)

{

    cout << s.x;

}


int main()

{

    Sample s;

    show(s);

    return 0;

}

6. Overloading Member Functions

Member functions of a class can be overloaded.

Example

Copy code

Cpp

#include <iostream>

using namespace std;


class Demo

{

public:

    void display(int a)

    {

        cout << a << endl;

    }


    void display(float b)

    {

        cout << b << endl;

    }

};


int main()

{

    Demo d;

    d.display(10);

    d.display(5.5f);

    return 0;

}

7. Bit Fields and Classes

Bit fields allow storing data in bits, saving memory.

Syntax

Copy code

Cpp

data_type variable : size;

Example

Copy code

Cpp

#include <iostream>

using namespace std;


class Flags

{

public:

    unsigned int a : 1;

    unsigned int b : 1;

};


int main()

{

    Flags f;

    f.a = 1;

    f.b = 0;


    cout << f.a << " " << f.b;

    return 0;

}

8. Constructor and Destructor with Static Members

Constructor

Called automatically when object is created

Destructor

Called when object is destroyed

Example

Copy code

Cpp

#include <iostream>

using namespace std;


class Test

{

public:

    static int count;


    Test()

    {

        count++;

        cout << "Object Created: " << count << endl;

    }


    ~Test()

    {

        cout << "Object Destroyed" << endl;

    }

};


int Test::count = 0;


int main()

{

    Test t1, t2;

    return 0;

}

⭐ Importance (Why We Use These Concepts)

Concept

Purpose

Inline function

Faster execution

Function overloading

Code clarity & polymorphism

Class & object

Real-world modeling

Static members

Shared data

Friend function

Controlled data access

Array of objects

Handle multiple objects

Bit fields

Memory optimization

Constructor & destructor

Resource management

No comments:

Post a Comment

Function, Class and Object

  I. Functions in C++ A function is a block of code that performs a specific task. It helps in code reusability, modularity, and readability...