1

I want to know how to use members from other files in main c++ code file?

I already know that we can put our declarations in header files.

Let me summarize my current knowledge and missing parts:

Example 1 - using a function from another file

other.cpp

int Add(int a,int b)
{
    return a+b;
}

main.cpp

int Add(int,int); //the most important part
int main()
{
    Add(12,2);
}

Example 2 - using a global variable from another file

other.cpp

int something=12;

main.cpp

extern int something; //the most important part
int main()
{
    cout << something;
}

Question 1 - create an object from a class in another file???

other.cpp

class Rectangle
{
public:
    int width;
    int height;
    int area() { return height*width }
};

main.cpp

int main()
{
    Rectangle a; //gives me error,what should i do?
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Amirreza H
  • 63
  • 1
  • 9
  • 1
    Just declare Rectangle in a header file of its own. Then include it in main.cpp. The implementation of the Rectangle functions can then go into its cpp. – Niall Oct 10 '14 at 19:27
  • @Niall Isn't it possible to declare a class without implementation? – Amirreza H Oct 10 '14 at 19:28
  • Do as @Niall says, but also for all other functions you want to share. The implementation-file should always include the declaration-file. – Deduplicator Oct 10 '14 at 19:29
  • Why my question is downvoted? – Amirreza H Oct 10 '14 at 19:29
  • 1
    It is possible, any of the common C++ books will give you some nice examples on how to do this. Check the tag wiki here on SO for links etc. – Niall Oct 10 '14 at 19:31
  • @AmirrezaH: My guess: Lack of research-effort, and lack of usefulness. – Deduplicator Oct 10 '14 at 19:32
  • @Niall I use tutorials from http://cplusplus.com ; there it does not discuss much about such concepts,it only focuses on language syntax and some standard library.Do you know a good C++ book or a good C++ online tutorial which discuss such concept? If yes please introduce it to me. thank you – Amirreza H Oct 10 '14 at 19:34
  • Search, and you will find. The C++ tag-wiki and the C++ book list on SO are a good start. – Deduplicator Oct 10 '14 at 19:37
  • I can't say I know of any good online tutorials, I haven't looked for one in ages, but this is a good list of books http://stackoverflow.com/a/388282/3747990 that will get you very far. – Niall Oct 10 '14 at 19:39

4 Answers4

4

You should start using header files. Typically you would have

Rectangle.h a header file with the declarations

class Rectangle{ 
public:
    Rectangle();
    Rectangle(int x, int y); 
    int width; 
    int height; 
    int area();
};

Rectangle.cpp Then you'd have a corresponding cpp file with the definitions

#include "Rectangle.h"

Rectangle::Rectangle()
{
    width = 0;
    height = 0;
}

Rectangle::Rectangle(int x, int y)
{
    width = x;
    height = y;
}

Rectangle::area()
{
    return height*width;
}

Now in your main.cpp

#include "Rectangle.h"
int main()
{
    Rectangle a;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Why do you hate `struct`? – Deduplicator Oct 10 '14 at 19:33
  • In this case, a `struct` would suffice, but there are obviously many more complex examples which would warrant a full `class`. – Cory Kramer Oct 10 '14 at 19:33
  • You know the only difference between them, right? That makes the phrase "a full class" a bit absurd. Anyway, there are disadvantages to declaring ctors, especially the way you do it. – Deduplicator Oct 10 '14 at 19:35
  • Functionally yes, they are almost identical aside from the private/public defaults. I'm not going to get into a discussion of when to use `struct` vs `class`, I think every C++ developer would be able to identify when they would use one or the other. – Cory Kramer Oct 10 '14 at 19:40
  • Ok, lets table `struct`, it's mostly stylistic. But why do you force ctors? Without them, we would have a nice trivial type. – Deduplicator Oct 10 '14 at 19:42
  • I was just trying to show that they could split up their declarations from definitions, constructors were the first examples that came to mind besides their `area` function, just so there were more than just the one function. – Cory Kramer Oct 10 '14 at 19:43
2

You need to declare a class Rectangle in main.cpp, preferably using a header file, ex:

other.h

class Rectangle
{ public:int width; int height; int area(); };

other.cpp

#include other.h

int Rectangle::area() { return height*width; }

main.cpp

#include "other.h"

int main()
{     
  Rectangle a; //gives me error,what should i do?
}
lisu
  • 2,213
  • 14
  • 22
2

Following your pattern, it should be simply class Rectangle; (forward declaration).

However, you really should consider to create a Rectangle.h and Rectangle.[C|cpp|cc] (any of them), and include the header instead of forward declaring.

The header should contain the declaration, the source the definition.

erenon
  • 18,838
  • 2
  • 61
  • 93
  • 4
    This will not work. You cannot forward declare a class that you intend to create an instance of. You can only forward declare if you only use pointers or references. – Cory Kramer Oct 10 '14 at 19:35
  • Yes.that's correct. forward declaration for classes do not work. I don't say that,Compilers say that !!! – Amirreza H Oct 10 '14 at 19:43
1

Place the declaration of the class in a header file. The definition is usually placed in a source file which uses #include to reference the declaration header. Any other source file which require usage of the class will then simply #include the class declaration header.

Ron Pinkas
  • 357
  • 2
  • 10