简体   繁体   English

如何从一个类中获取变量,然后将其放入另一个类中,然后将其放入构造函数中?

[英]c++ How do I get variables from a class and put it into another class, and put it into a constructor?

I have 2 h and cpp files. 我有2 h和cpp文件。 I wanna know how to call variables from the Abe.h file or the Abe class to the Bob.h or the Bob class. 我想知道如何从Abe.h文件或Abe类到Bob.h或Bob类调用变量。 Please help. 请帮忙。

Abe.h 安倍

#include <iostream>
using namespace std;
#ifndef ABE
#define ABE

class Abe
{
    private:
    int num;

    public:
        Abe();
        Abe(int);
        void showNumber();

};
#endif // ABE

Abe.cpp 安倍

#include "Abe.h"
#include <iostream>

using namespace std;

Abe::Abe()
{
    num=45;
}

Abe::Abe(int n)
{
    num=n;
}

void Abe::showNumber()
{
    cout<<num;
}

BOB.h BOB.h

#include "Abe.h"
#include <iostream>

using namespace std;
#ifndef BOB
#define BOB

class Bob
{
private:
    Abe a;

public:
    Bob(Abe);
    void showNum();
};

#endif // BOB

BOB.cpp BOB文件

#include "Abe.h"
#include "Bob.h"
#include <iostream>

using namespace std;

Bob::Bob(Abe a1)
{
    a=a1;
  //^not sure what a=a1 is doing but if you could explain in simple terms or in deatil that would help.
}

void Bob::showNum()
{
    //how do I display it here??
}

so how do I get "num" from the Abe class and use it in the Bob class? 那么如何从安倍类中获取“ num”并在Bob类中使用它? please help. 请帮忙。 Thank You! 谢谢!

The assignment in the Bob constructor is just like any other assignment, it copies the data from a1 into a the same way like eg this code would: 在分配Bob构造就像任何其他的分配,它直接复制数据a1a同样的方式例如像这段代码:

int a1 = 6;

...

int a;
a = a1;

Once you have an initialized the a object in the Bob class, you can call the member functions of a like any other objects. 一旦你有一个初始化a对象中的Bob类,你可以调用的成员函数a像任何其他对象。 However, you can't access private members. 但是,您无法访问私人成员。

One solution is to add a getter member function to the Abe class which returns the num member, something like 一种解决方案是将一个getter成员函数添加到返回num成员的Abe类中,例如

int Abe::getNumber() const
{
    return num;
};

Then you can use it in the Bob class like 然后您可以在Bob类中使用它,例如

std::cout << "the number is " << a.getNumber() << '\n';

a=a1 assigns the Abe passed into Bob 's constructor to Bob 's a member. a=a1将传递给Bob的构造函数的Abe分配给Bob a成员。 Probably a good idea to use more descriptive variable names and there are some optimizations to be gained by using the initializer list. 使用更具描述性的变量名可能是一个好主意,并且使用初始化程序列表可以获得一些优化。 Example: 例:

Bob::Bob(Abe a1):a(a1)
{
}

Now that Bob has an Abe , it can... Do nothing to get the number from Abe. 既然Bob有了Abe ,就可以...不采取任何行动来从Abe取得电话号码。 Abe has to allow Bob to get the number. 安倍必须允许Bob取得电话号码。 To do this redefine Abe in one of two ways: 为此,可以使用以下两种方法之一重新定义Abe

class Abe
{
    private:
    int num;

    public:
        Abe();
        Abe(int);
        void showNumber();
        int getNumber()
        {
            return num;
        }
};

Now Bob can 现在Bob可以

int number = a.getNumber();

or (and this is second because it is the less preferred option because it tightly couples Bob and Abe . 或(这是第二个,因为它是次要的选择,因为它将BobAbe紧密地结合在一起。

class Abe
{
    friend class Bob;
    private:
    int num;

    public:
        Abe();
        Abe(int);
        void showNumber();
};

Bob now has total access to the internals of Abe and can really mess the poor guy up if he wants to. Bob现在可以完全访问Abe的内部资料,如果他愿意的话,他真的可以把那个可怜的家伙搞砸了。 Which is not nice. 哪个不好 Anyway, Bob can now 无论如何, Bob现在可以

int number = a.num;

Bob can read num , change num , set it to hopelessly bad values and generally be a jerk to Abe . Bob可以阅读num ,更改num ,将其设置为绝望的坏值,通常对Abe不利。 Good thing Bob is a friend. 好东西Bob是朋友。 The other downside is Bob now needs to know how Abe works on the inside and if Abe 's code is changed, Bob will have to change as well. 另一个缺点是Bob现在需要知道Abe在内部的工作方式,如果Abe的代码被更改, Bob也将不得不更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM