Hướng dẫn passing object as argument in c++ - truyền đối tượng làm đối số trong C++

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    // Creating the demo object
    demo d1 = new demo(6);
    
    // Passing object as an argument to the PassObj method.
    d1.PassObj(d1);

    Bàn luận

    C#

    Đưa ra một đối tượng, bây giờ chúng ta chuyển đối tượng này như một đối số vào phương thức trong C#. Ở đây, một phương thức là một khối mã chứa một loạt các câu lệnh sẽ chỉ thực thi khi phương thức được gọi. Chúng ta có thể truyền dữ liệu đến các phương thức dưới dạng đối số và một đối tượng là một thể hiện của một lớp được tạo động. Các loại dữ liệu cơ bản có thể được truyền dưới dạng đối số cho các phương thức C# giống như cách đối tượng cũng có thể được truyền dưới dạng đối số cho một phương thức. Nhưng hãy nhớ rằng chúng ta không thể truyền trực tiếp một đối tượng lớp trong phương thức. Chúng ta chỉ có thể chuyển tham chiếu đến đối tượng trong phương thức.

    Ví dụ 1:

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    using System;

    class demo

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    Average Marks = 72
    1
    Average Marks = 72
    2

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    Average Marks = 72
    4

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    2
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    3

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    Average Marks = 72
    1
    #include 
    using namespace std;
    
    class Student {
       public:
        double marks1, marks2;
    };
    
    // function that returns object of Student
    Student createStudent() {
        Student student;
    
        // Initialize member variables of Student
        student.marks1 = 96.5;
        student.marks2 = 75.0;
    
        // print member variables of Student
        cout << "Marks 1 = " << student.marks1 << endl;
        cout << "Marks 2 = " << student.marks2 << endl;
    
        return student;
    }
    
    int main() {
        Student student1;
    
        // Call function
        student1 = createStudent();
    
        return 0;
    }
    2

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    Average Marks = 72
    4

    Average Marks = 72
    4

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    5
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    6
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    2
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    8

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    5
    Average Marks = 72
    7
    Average Marks = 72
    8

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    class

    #include 
    using namespace std;
    
    class Student {
       public:
        double marks1, marks2;
    };
    
    // function that returns object of Student
    Student createStudent() {
        Student student;
    
        // Initialize member variables of Student
        student.marks1 = 96.5;
        student.marks2 = 75.0;
    
        // print member variables of Student
        cout << "Marks 1 = " << student.marks1 << endl;
        cout << "Marks 2 = " << student.marks2 << endl;
    
        return student;
    }
    
    int main() {
        Student student1;
    
        // Call function
        student1 = createStudent();
    
        return 0;
    }
    7

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    Marks1 = 96.5
    Marks2 = 75
    7

    Average Marks = 72
    4

    Average Marks = 72
    4

    #include 
    using namespace std;
    
    class Student {
       public:
        double marks1, marks2;
    };
    
    // function that returns object of Student
    Student createStudent() {
        Student student;
    
        // Initialize member variables of Student
        student.marks1 = 96.5;
        student.marks2 = 75.0;
    
        // print member variables of Student
        cout << "Marks 1 = " << student.marks1 << endl;
        cout << "Marks 2 = " << student.marks2 << endl;
    
        return student;
    }
    
    int main() {
        Student student1;
    
        // Call function
        student1 = createStudent();
    
        return 0;
    }
    8
    Average Marks = 72
    7
    Marks1 = 96.5
    Marks2 = 75
    0
    In the above example, first we create a class named “demo”with a constructor to initialize an integer variable. It also has a method named PassObj() which takes one parameter. Now in the main function, we create an object of demo class named “d1” and then pass this object in PassObj() method, and the value is printed.

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    Marks1 = 96.5
    Marks2 = 75
    3
    Marks1 = 96.5
    Marks2 = 75
    4
    Marks1 = 96.5
    Marks2 = 75
    5

    C#

    Đưa ra một đối tượng, bây giờ chúng ta chuyển đối tượng này như một đối số vào phương thức trong C#. Ở đây, một phương thức là một khối mã chứa một loạt các câu lệnh sẽ chỉ thực thi khi phương thức được gọi. Chúng ta có thể truyền dữ liệu đến các phương thức dưới dạng đối số và một đối tượng là một thể hiện của một lớp được tạo động. Các loại dữ liệu cơ bản có thể được truyền dưới dạng đối số cho các phương thức C# giống như cách đối tượng cũng có thể được truyền dưới dạng đối số cho một phương thức. Nhưng hãy nhớ rằng chúng ta không thể truyền trực tiếp một đối tượng lớp trong phương thức. Chúng ta chỉ có thể chuyển tham chiếu đến đối tượng trong phương thức.

    Ví dụ 1:

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    using System;

    class demo

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    Average Marks = 72
    1using6

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    Average Marks = 72
    4

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    2
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    3

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    Average Marks = 72
    1System;6

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    Average Marks = 72
    4

    Average Marks = 72
    4

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    5
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    6
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    2
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    8

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    5
    Average Marks = 72
    7
    Average Marks = 72
    8

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    0

    class

    #include 
    using namespace std;
    
    class Student {
       public:
        double marks1, marks2;
    };
    
    // function that returns object of Student
    Student createStudent() {
        Student student;
    
        // Initialize member variables of Student
        student.marks1 = 96.5;
        student.marks2 = 75.0;
    
        // print member variables of Student
        cout << "Marks 1 = " << student.marks1 << endl;
        cout << "Marks 2 = " << student.marks2 << endl;
    
        return student;
    }
    
    int main() {
        Student student1;
    
        // Call function
        student1 = createStudent();
    
        return 0;
    }
    7

    #include 
    using namespace std;
    
    class Student {
       public:
        double marks1, marks2;
    };
    
    // function that returns object of Student
    Student createStudent() {
        Student student;
    
        // Initialize member variables of Student
        student.marks1 = 96.5;
        student.marks2 = 75.0;
    
        // print member variables of Student
        cout << "Marks 1 = " << student.marks1 << endl;
        cout << "Marks 2 = " << student.marks2 << endl;
    
        return student;
    }
    
    int main() {
        Student student1;
    
        // Call function
        student1 = createStudent();
    
        return 0;
    }
    8
    Average Marks = 72
    7
    Marks1 = 96.5
    Marks2 = 75
    0

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1demo5

    Average Marks = 72
    4

    Average Marks = 72
    4

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    1
    Marks1 = 96.5
    Marks2 = 75
    3
    Marks1 = 96.5
    Marks2 = 75
    4
    Marks1 = 96.5
    Marks2 = 75
    5
    In the above example, first we create a class named “Geeks”with a constructor to initialize an integer variable. It also has a method named PassObj() which takes two parameters and returns the product of two values. Now in the main function, we create two objects of the Geeks class named “d1” and “d2” and then pass this object in PassObj() method, and the value is printed.


    Trong lập trình C ++, chúng ta có thể chuyển các đối tượng đến một hàm theo cách tương tự như truyền các đối số chính quy.

    Ví dụ 1: C ++ chuyển các đối tượng đến chức năng

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }

    Đầu ra

    Average Marks = 72

    Ở đây, chúng tôi đã vượt qua hai đối tượng demo8 Student1 và Student2 làm đối số cho hàm demo9.

    Hướng dẫn passing object as argument in c++ - truyền đối tượng làm đối số trong C++
    Vượt qua các đối tượng để hoạt động trong C ++

    Ví dụ 2: đối tượng trả về C ++ từ một hàm

    #include 
    using namespace std;
    
    class Student {
       public:
        double marks1, marks2;
    };
    
    // function that returns object of Student
    Student createStudent() {
        Student student;
    
        // Initialize member variables of Student
        student.marks1 = 96.5;
        student.marks2 = 75.0;
    
        // print member variables of Student
        cout << "Marks 1 = " << student.marks1 << endl;
        cout << "Marks 2 = " << student.marks2 << endl;
    
        return student;
    }
    
    int main() {
        Student student1;
    
        // Call function
        student1 = createStudent();
    
        return 0;
    }

    Đầu ra

    Ở đây, chúng tôi đã vượt qua hai đối tượng demo8 Student1 và Student2 làm đối số cho hàm demo9.
    Hướng dẫn passing object as argument in c++ - truyền đối tượng làm đối số trong C++
    Return object from function in C++

    Vượt qua các đối tượng để hoạt động trong C ++

    Chúng tôi đã gọi

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    00 từ phương pháp
    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    03.

    // Call function
    student1 = createStudent();

    Ở đây, chúng tôi đang lưu trữ đối tượng được trả về bằng phương pháp

    // C++ program to calculate the average marks of two students
    
    #include 
    using namespace std;
    
    class Student {
    
       public:
        double marks;
    
        // constructor to initialize marks
        Student(double m) {
            marks = m;
        }
    };
    
    // function that has objects as parameters
    void calculateAverage(Student s1, Student s2) {
    
        // calculate the average of marks of s1 and s2 
        double average = (s1.marks + s2.marks) / 2;
    
       cout << "Average Marks = " << average << endl;
    
    }
    
    int main() {
        Student student1(88.0), student2(56.0);
    
      // pass the objects as arguments
       calculateAverage(student1, student2);
    
        return 0;
    }
    00 trong Student1.

    Làm thế nào để bạn vượt qua một đối tượng như một đối số trong phương thức c?

    C# cung cấp Trình sửa đổi tham số Ref để chuyển các đối tượng giá trị vào một phương thức bằng cách tham chiếu và công cụ sửa đổi ra cho các trường hợp bạn muốn truyền trong một biến Ref mà không khởi tạo trước.C# cũng hỗ trợ công cụ sửa đổi params cho phép một phương thức chấp nhận số lượng tham số biến.ref parameter modifier for passing value objects into a method by reference and the out modifier for those cases in which you want to pass in a ref variable without first initializing it. C# also supports the params modifier which allows a method to accept a variable number of parameters.

    Làm thế nào các đối tượng được truyền dưới dạng đối số cho một hàm?

    Các đối số hàm có thể là đối tượng và được truyền theo hai cách cụ thể là vượt qua giá trị và vượt qua tham chiếu.Trong Pass by giá trị, một bản sao của đối tượng dưới dạng đối số thực tế được chuyển đến hàm và đối số chính thức của hàm mà loại lớp nhận được bản sao của đối số thực tế và xử lý nó.pass by value and pass by reference. In pass by value, a copy of the object as actual argument is passed to the function and formal arguments of function which of class type receive the copy of the actual argument and process it.

    Chúng ta có thể chuyển các đối tượng làm đối số cho các hàm thành viên không?

    Các đối tượng của một lớp có thể được truyền dưới dạng đối số cho các hàm thành viên cũng như các hàm không phải là tháng theo giá trị hoặc theo tham chiếu. as well as nonmember functions either by value or by reference.

    Bạn có thể chuyển một đối tượng cho một chức năng?

    Trong lập trình C ++, chúng ta có thể chuyển các đối tượng đến một hàm theo cách tương tự như truyền các đối số chính quy.we can pass objects to a function in a similar manner as passing regular arguments.