Need more clarification about object declaration

Hi,

I have 2 classes, First.cpp and Second.cpp

First.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef FIRST_H
#define FIRST_H
#include <second.h>

class First
{
private:
    Second sec;
public:
    First();
    ~First();
    int callFirst();
};

#endif  


First.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "first.h"
#include <iostream>

First::First()
{
    std::cout<<"First Constructor"<<std::endl;
}

First::~First()
{
    std::cout<<"First Destructor"<<std::endl;
}

int First::callFirst()
{
    return sec.callSecond();
}


I'm able to access Second::callSecond() using sec. But I can also re-declare, not sure if it's the correct term to use, the object before the constructor in Frist.cpp as Second sec; and still be able to access Second::callSecond(), making it global.

Since I declared the object in the .h file, do I need to re-declare it in the .cpp file or just access it? Is the approach I'm using above the best way to access an object of a class? The .h and .cpp is causing me some confusion because in java it's all done in one place.

Would greatly appreciate your clarification
Last edited on
Since I declared the object in the .h file

Declared what object in which file?

Do you mean the member variable 'sec' of class First within file First.h?

First.h merely defines a type class First. There are no objects in that file. The definition of class Second has to be available, because it affects the size of class First. For that you have an #include.

The First.cpp implements methods of class First and it has to have the definitions of class First and class Second available. It does, through include.

When you actually create an object of type First somewhere (main() or other function) they too need the definitions. That is why the definitions are put to header files, for easy inclusion to every translation unit that needs them.
Since I declared the object in the .h file, do I need to re-declare it in the .cpp file or just access it?


The #include is just like one had copy / pasted the header file into that point in the cpp file, so there is no need to re-declare it.

When one has a Second as a member variable in First that is a "HAS A" relationship. One should be careful that the relationships make sense. How is a function in a Second being called? It may not be necessary to have it as a member if it is not calling with a member variable of First as an argument.

Also, classes are supposed to have an interface of public functions that one can use to access / manipulate the member data. One could use this interface rather than have member variables of other classes.
If it's a definition and not declaration, how was I able to access sec.callSecond(); without declaring a Second object in First.cpp

As you can see from the code above, I defined sec.callSecond(); in First.h and didn't declare it in First.cpp. However, I was able to use "sec", how is that possible?
Last edited on
The #include is just like one had copy / pasted the header file into that point in the cpp file, so there is no need to re-declare it.


Does that mean that Second sec; is automatically assumed to be global?
Does that mean that Second sec; is automatically assumed to be global?


No, it's a private: member variable in the First class.

As you can see from the code above, I defined sec.callSecond(); in First.h and didn't declare it in First.cpp. However, I was able to use "sec", how is that possible?


sec.callSecond(); is a function call, not either of a declaration nor definition. The declaration of that void function should be in Second.h (header file), the definition of that function should in Second.cpp. (Implementation file).
You were able to use the sec variable because of the #include <second.h> in First.h. Btw that should be #include "second.h" for your own header files, the angle brackets are for system headers.

What is the real life situation here? It's all very well to call classes First and Second, but we need to check that the USING relationship is a valid design.
The #include is a preprocessor directive, just like the #ifndef, #endif and #define.
See http://www.cplusplus.com/articles/Gw6AC542/
Last edited on
Topic archived. No new replies allowed.