Creating Objects of a Class in C++ - Local Objects, Global Objects and Array of Objects

An object is an instance of a class. A class defines the structure and behaviour, while objects are used to access its data members and member functions. In C++, multiple objects can be created from a single class. Each object has its own separate copy of non-static data members.

Object किसी class का instance होता है। Class object का structure और behaviour define करती है, जबकि objects का उपयोग class के data members और member functions को access करने के लिए किया जाता है। C++ में एक ही class से multiple objects create किए जा सकते हैं। प्रत्येक object के पास अपने अलग non-static data members होते हैं।

1. Creating Objects of a Class in C++

An object is created by declaring a variable of the class type. The object can then be used to access the accessible members of that class.

C++ में class type का variable declare करके object create किया जाता है। इसके बाद object की सहायता से class के accessible members को access किया जा सकता है।

Syntax

ClassName objectName;

Example: Creating an Object

#include <iostream>
using namespace std;

class Student
{
public:
    int rollNo;
    string name;

    void display()
    {
        cout << "Roll Number: " << rollNo << endl;
        cout << "Name: " << name << endl;
    }
};

int main()
{
    Student s1;

    s1.rollNo = 101;
    s1.name = "Rahul";

    s1.display();

    return 0;
}

Output:

Roll Number: 101
Name: Rahul

Explanation:

  • Student is a class.

  • Student s1; creates an object named s1.

  • s1.rollNo assigns a value to the roll number.

  • s1.name assigns a name to the student.

  • s1.display() calls the member function.

व्याख्या:

  • Student एक class है।

  • Student s1; से s1 नाम का object create होता है।

  • s1.rollNo से roll number assign किया जाता है।

  • s1.name से student का नाम assign किया जाता है।

  • s1.display() से member function call होता है।

2. Local Objects in C++

A local object is an object declared inside a function or a block. Its scope is limited to the block in which it is declared. An ordinary local object is destroyed when its lifetime ends, generally when execution leaves that block.

Local Object वह object होता है जिसे किसी function या block के अंदर declare किया जाता है। इसका scope उसी block तक सीमित रहता है जिसमें इसे declare किया गया है। सामान्य local object का lifetime block से बाहर निकलने पर समाप्त हो जाता है।

Example: Local Object

#include <iostream>
using namespace std;

class Student
{
public:
    int rollNo;

    void display()
    {
        cout << "Roll Number: " << rollNo << endl;
    }
};

void showStudent()
{
    Student s1;

    s1.rollNo = 101;
    s1.display();
}

int main()
{
    showStudent();

    return 0;
}

Output:

Roll Number: 101

Explanation:

In this example, s1 is declared inside the showStudent() function. Therefore, it is a local object and can be accessed within that function.

इस उदाहरण में s1 को showStudent() function के अंदर declare किया गया है। इसलिए यह एक local object है और इसे उसी function के अंदर access किया जा सकता है।

3. Global Objects in C++

A global object is an object declared outside all functions and classes at namespace scope. It can be accessed by functions in its scope, subject to access and visibility rules. A global object normally exists throughout the execution of the program.

Global Object वह object होता है जिसे सभी functions और classes के बाहर namespace scope पर declare किया जाता है। इसे इसके scope में आने वाले functions से access किया जा सकता है, बशर्ते visibility और access rules अनुमति दें। सामान्यतः global object पूरे program execution के दौरान exist करता है।

Example: Global Object

#include <iostream>
using namespace std;

class Student
{
public:
    int rollNo;

    void display()
    {
        cout << "Roll Number: " << rollNo << endl;
    }
};

// Global Object
Student s1;

int main()
{
    s1.rollNo = 102;

    s1.display();

    return 0;
}

Output:

Roll Number: 102

Explanation:

In this example, s1 is declared outside the main() function. Therefore, it is a global object and can be accessed inside main().

इस उदाहरण में s1 को main() function के बाहर declare किया गया है। इसलिए यह एक global object है और इसे main() के अंदर access किया जा सकता है।

4. Accessing Data Members and Member Functions

The dot operator (.) is used to access accessible data members and member functions through an object.

Dot Operator (.) का उपयोग object की सहायता से class के accessible data members और member functions को access करने के लिए किया जाता है।

Syntax

Accessing Data Member:

objectName.dataMember;

Calling Member Function:

objectName.memberFunction();

Example: Accessing Class Members

#include <iostream>
#include <string>
using namespace std;

class Employee
{
public:
    int empId;
    string name;
    double salary;

    void display()
    {
        cout << "Employee ID: " << empId << endl;
        cout << "Name: " << name << endl;
        cout << "Salary: " << salary << endl;
    }
};

int main()
{
    Employee e1;

    // Accessing Data Members
    e1.empId = 101;
    e1.name = "Amit";
    e1.salary = 35000;

    // Calling Member Function
    e1.display();

    return 0;
}

Output:

Employee ID: 101
Name: Amit
Salary: 35000

Explanation:

In this program, the object e1 is used to access the data members empId, name and salary. The member function display() is also called using the dot operator.

इस प्रोग्राम में e1 object का उपयोग empId, name और salary data members को access करने के लिए किया गया है। Member function display() को भी dot operator की सहायता से call किया गया है।

Note: Private members cannot be accessed directly using an object from outside the class. They are generally accessed through public member functions.

ध्यान दें: Private members को class के बाहर object की सहायता से directly access नहीं किया जा सकता। इन्हें सामान्यतः public member functions के माध्यम से access किया जाता है।

5. Array of Objects in C++

An array of objects is a collection of multiple objects of the same class stored under a single array name. Each element of the array represents a separate object.

Array of Objects एक ही class के multiple objects का collection होता है, जिन्हें एक single array name के अंतर्गत store किया जाता है। Array का प्रत्येक element एक अलग object को represent करता है।

Syntax

ClassName arrayName[size];

Example: Array of Objects

#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    int rollNo;
    string name;

    void input()
    {
        cout << "Enter Roll Number: ";
        cin >> rollNo;

        cout << "Enter Name: ";
        cin >> name;
    }

    void display()
    {
        cout << "Roll Number: " << rollNo << endl;
        cout << "Name: " << name << endl;
    }
};

int main()
{
    Student s[3];

    cout << "Enter Student Details:" << endl;

    for (int i = 0; i < 3; i++)
    {
        cout << "\nStudent " << i + 1 << endl;
        s[i].input();
    }

    cout << "\nStudent Records:" << endl;

    for (int i = 0; i < 3; i++)
    {
        cout << "\nStudent " << i + 1 << endl;
        s[i].display();
    }

    return 0;
}

Output:

Enter Student Details:

Student 1
Enter Roll Number: 101
Enter Name: Rahul

Student 2
Enter Roll Number: 102
Enter Name: Amit

Student 3
Enter Roll Number: 103
Enter Name: Neha

Student Records:

Student 1
Roll Number: 101
Name: Rahul

Student 2
Roll Number: 102
Name: Amit

Student 3
Roll Number: 103
Name: Neha

Explanation:

  • Student s[3]; creates an array containing three objects of the Student class.

  • s[0], s[1] and s[2] represent the three objects.

  • s[i].input() calls the input function for each object.

  • s[i].display() displays the details of each object.

व्याख्या:

  • Student s[3]; से Student class के तीन objects का array create होता है।

  • s[0], s[1] और s[2] तीनों objects को represent करते हैं।

  • s[i].input() प्रत्येक object के लिए input function call करता है।

  • s[i].display() प्रत्येक object की details display करता है।

6. Difference Between Local Objects and Global Objects

Local and global objects differ mainly in their scope and lifetime.

Local और Global objects में मुख्य अंतर उनके scope और lifetime का होता है।

BasisLocal ObjectGlobal Object
DeclarationInside a function or blockOutside all functions and classes
ScopeLimited to the blockNamespace scope
LifetimeUntil its block execution endsNormally throughout program execution
AccessibilityWithin its scopeWithin its visible scope
InitializationDefault initialization rules applyStatic initialization rules apply

Objects are instances of classes and are used to access class members. C++ allows programmers to create local objects, global objects and arrays of objects according to program requirements. Understanding object creation and member access is essential for developing object-oriented programs in C++.

Objects, classes के instances होते हैं और इनका उपयोग class members को access करने के लिए किया जाता है। C++ में program की आवश्यकता के अनुसार local objects, global objects तथा arrays of objects create किए जा सकते हैं। C++ में Object-Oriented Programs develop करने के लिए object creation और member access को समझना आवश्यक है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)