How to define an object from Class A in Class B

2019-05-16 16:25发布

问题:

I'm a beginner in C++ programming and I've a (stupid, I think) doubt about how to pass an object's class into another class. Suppose we have these two classes:

class A {...}
class B {...}

and I want to use an object from A into B. For example:

class B {
    A ab;
    [methods prototypes that include the object ab]
    method_B (A ab); //for example

    ...
}

The question is, can I do this? Does it make sense, thinking about object-oriented programming?

Or, I could define an A's object in main() and after that I would call a method from B that would include A's object as argument?

My question is all about how to use object's from another class into another (functionally independent!) without "violating" the object oriented programming rules.

Thank you for any help,

回答1:

You can do this. Many libraries and languages do this. From OO view you need to need to design class A such that it will expose (public) a minimal and useful inteface to be used but other classes (such as class B).



标签: c++ class object