3

I'm Java programmer and I'm new to C++ Programming. In java we have to write all classes in separate files and all method's definitions are right inside the class. But now in C++ I'm wonder that why C++ allow programmers to write method's definition outside a class. Is there any way to write C++ programs like Java?

Milad Khajavi
  • 2,769
  • 9
  • 41
  • 66
  • 1
    You can put them in the class just like in Java. Did you try it? What problem did you have? Keeping them in a separate file can decrease compilation time and allow for people not seeing the implementation. – chris Feb 24 '13 at 04:15
  • 1
    In `C++`, the methods defined outside the `classes` are similar to `static` methods in `java`.
    If you want you can also add those methods in a `class` as `static` methods.
    > In java we have to write all classes in separate files and all > method's definitions are right inside the class. I first learned `C++` and then `Java`.
    As you wondered defining many classes in same file, I surprised and thought that `defining each class in separate file and each method must be compulsory inside a class`, as `stupid` way.
    – Govind Balaji Feb 24 '13 at 04:30
  • @govindo Why this is the stupid way? what's wrong with it? I think, in large projects, when we define method outside class, the project would be unmanageable. Is it true? – Milad Khajavi Feb 24 '13 at 04:36
  • I missed the part about multiple classes in one file. This is possible, and may be useful if you have multiple closely related classes, that you will always use together. But it is much more confusing if you have several unrelated classes in the same file as then you have no idea which file to open when you need to edit one of them. In general I would put one class in each file, and if several are closely related you could create a separate directory and put the cpp/h files there. – Adrian G Feb 24 '13 at 04:45
  • 1
    @Khajavi It actually doesn't effect manageability at all to have them in a separate file. Coming from a C oriented background, NOT having them separated actually feels more difficult to manage, because there's no way to readily scan what public methods a class has! – RonLugge Feb 24 '13 at 04:45
  • @Khajavi I don't think now it is a stupid way, but when I started learning java, i thought like that. – Govind Balaji Feb 24 '13 at 08:39

2 Answers2

6

You can write the code for your classes within the header file if you like. This makes the header file in C++ similar to the java file in java.

MyClass.h

#ifndef _MYCLASS_H_
#define _MYCLASS_H_

#include "OtherClass.h"

class MyClass {
public:
    MyClass() { _otherClass=0; }

    void set(OtherClass* oc) { _otherClass = oc; );
    OtherClass* get(void) { return _otherClass; };

private:
   OtherClass* _otherClass;
};
#endif

But you can also split the header and the code into two files in C++. This allows you to separate the definition and declaration of the methods and reduces compile time header dependencies.

Note that in the example above, any class which includes MyClass.h will automatically include OtherClass.h whether it needs it or not, and changes to OtherClass.h will require recompiling of all clients of MyClass.h.

However in the separated example below, there is a forward declaration of OtherClass.h (this is possible because it is only used as a pointer), and the actual OtherClass.h is only included in the cpp file. Now changes to OtherClass.h will only force recompile of MyClass.cpp, and not clients of MyClass.h (unless they also include OtherClass.h),

It also means that you can make a change to MyClass::get() and you will only need to recompile MyClass.cpp, not all clients of MyClass

MyClass.h

#ifndef _MYCLASS_H_
#define _MYCLASS_H_

class OtherClass;

class MyClass {
public:
    MyClass();

    void set(OtherClass* oc);
    OtherClass* get(void);

private:
   OtherClass* _otherClass;
};
#endif

MyClass.cpp

#include "MyClass.h"
#include "OtherClass.h"

MyClass::MyClass() : _otherClass(0) {}

MyClass::set(OtherClass* oc) { _otherClass=oc; }

OtherClass* MyClass::get() { return _otherClass; };
Adrian G
  • 775
  • 6
  • 11
3

But now in C++ I'm wonder that why C++ allow programmers to write method's definition outside a class.

Two major reasons are compilation times and separating the implementation from the interface. This is covered in more detail in In C++ why have header files and cpp files?

Is there any way to write C++ programs like Java?

You could write your entire implementation in header files, but you shouldn't. When writing code in any language, you should follow that language's idioms as this makes it easier to read and to maintain.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 2
    "You could, but shouldn't" -- great point, and one everyone should keep in mind when moving to a new language. Even if it does feel like a stupid, asinine, annoying way of doing things... There's probably a method to the madness of the people who set up that standard! – RonLugge Feb 24 '13 at 04:46