📜  在C++中传递和返回对象

📅  最后修改于: 2021-05-30 03:49:56             🧑  作者: Mango

在C++中,我们可以将类的对象作为参数传递,也可以通过传递和返回其他变量的方式从函数返回它们。不需要特殊的关键字或头文件。

将对象作为参数传递

要将对象作为参数传递,我们将对象名称写为参数,同时调用函数的方式与对其他变量的处理方式相同。
句法:

function_name(object_name);

示例:在此示例中,有一个类,该类具有整数变量’a’和带有对象作为参数的函数’add’。该函数由一个对象调用,并以另一个作为参数。在函数内部,将参数对象的整数值添加到调用了“ add”函数的整数值上。在这种方法中,我们可以将对象作为参数传递并更改它们。

CPP
// C++ program to show passing
// of objects to a function
 
#include 
using namespace std;
 
class Example {
public:
    int a;
 
    // This function will take
    // an object as an argument
    void add(Example E)
    {
        a = a + E.a;
    }
};
 
// Driver Code
int main()
{
 
    // Create objects
    Example E1, E2;
 
    // Values are initialized for both objects
    E1.a = 50;
    E2.a = 100;
 
    cout << "Initial Values \n";
    cout << "Value of object 1: " << E1.a
         << "\n& object 2: " << E2.a
         << "\n\n";
 
    // Passing object as an argument
    // to function add()
    E2.add(E1);
 
    // Changed values after passing
    // object as argument
    cout << "New values \n";
    cout << "Value of object 1: " << E1.a
         << "\n& object 2: " << E2.a
         << "\n\n";
 
    return 0;
}


CPP
// C++ program to show passing
// of objects to a function
 
#include 
using namespace std;
 
class Example {
public:
    int a;
 
    // This function will take
    // object as arguments and
    // return object
    Example add(Example Ea, Example Eb)
    {
        Example Ec;
        Ec.a = Ea.a + Eb.a;
 
        // returning the object
        return Ec;
    }
};
int main()
{
    Example E1, E2, E3;
 
    // Values are initialized
    // for both objects
    E1.a = 50;
    E2.a = 100;
    E3.a = 0;
 
    cout << "Initial Values \n";
    cout << "Value of object 1: " << E1.a
         << ", \nobject 2: " << E2.a
         << ", \nobject 3: " << E3.a
         << "\n";
 
    // Passing object as an argument
    // to function add()
    E3 = E3.add(E1, E2);
 
    // Changed values after
    // passing object as an argument
    cout << "New values \n";
    cout << "Value of object 1: " << E1.a
         << ", \nobject 2: " << E2.a
         << ", \nobject 3: " << E3.a
         << "\n";
 
    return 0;
}


输出
Initial Values 
Value of object 1: 50
& object 2: 100

New values 
Value of object 1: 50
& object 2: 150

返回对象作为参数

句法:

object = return object_name;

示例:在上面的示例中,我们可以看到add函数不返回任何值,因为其return-type为void。在以下程序中,add函数返回类型为“ Example”(即类名)的对象,其值存储在E3中。
在这个例子中,我们既可以看到如何传递对象又可以返回对象。当对象E3调用添加函数,它将传递其他两个对象E1和E2作为参数。在函数内部,声明了另一个对象,该对象计算所有三个变量的总和并将其返回给E3。
此代码与上面的代码几乎相同,唯一的区别是,这次add函数返回一个对象,该对象的值存储在相同类“ Example” E3的另一个对象中。在此,对象1显示E1的值,对象2显示E2的值,对象3显示E3的值。

CPP

// C++ program to show passing
// of objects to a function
 
#include 
using namespace std;
 
class Example {
public:
    int a;
 
    // This function will take
    // object as arguments and
    // return object
    Example add(Example Ea, Example Eb)
    {
        Example Ec;
        Ec.a = Ea.a + Eb.a;
 
        // returning the object
        return Ec;
    }
};
int main()
{
    Example E1, E2, E3;
 
    // Values are initialized
    // for both objects
    E1.a = 50;
    E2.a = 100;
    E3.a = 0;
 
    cout << "Initial Values \n";
    cout << "Value of object 1: " << E1.a
         << ", \nobject 2: " << E2.a
         << ", \nobject 3: " << E3.a
         << "\n";
 
    // Passing object as an argument
    // to function add()
    E3 = E3.add(E1, E2);
 
    // Changed values after
    // passing object as an argument
    cout << "New values \n";
    cout << "Value of object 1: " << E1.a
         << ", \nobject 2: " << E2.a
         << ", \nobject 3: " << E3.a
         << "\n";
 
    return 0;
}
输出
Initial Values 
Value of object 1: 50, 
object 2: 100, 
object 3: 0
New values 
Value of object 1: 50, 
object 2: 100, 
object 3: 150
想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”