Originally Posted by
vaibhav
Here I am having one file say time.cpp whose object i want to declare in other file.That's why i am including time file.Is there any other clean or more acceptable method?
Yes, the proper method is to define the class in a header library, implement the class in the second file and include the header in both the main file and the implementation. Add the both to a project, compile and link.
Code:
// classProto.h
#ifndef CLASS_PROTO_H
#define CLASS_PROTO_H
class foo {
public:
foo(int);
int getData() const;
void setData(int);
private:
int Data;
};
#endif
Code:
// ClassImplement.cpp
#include "classProto.h"
foo::foo(int DATA = 0) {
Data = DATA;
}
int foo::getData() const {
return Data;
}
void foo::setData(int DATA) {
Data = DATA;
}
Code:
// main.cpp
#include <iostream>
#include "classProto.h"
int main() {
foo bar(2);
bar.setData(5);
std::cout << bar.getData();
return 0;
}