Seperate files for classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Seperate files for classes

will sombody plis help me understand this seperate files for classes by writing down a piece of code. and indicating the codes that needs to be in 1.the main.cpp, 2.the .h file 3.the .cpp files... i would be very grateful. thank q

15th Sep 2017, 7:27 PM
stephen haokip
stephen haokip - avatar
1 Answer
+ 1
header file test.h ================== #ifndef Test_H_ #define Test_H_ #include <iostream> using namespace std; class Test { private: int id; static int count; public: static int const MAX = 99; Test(); // Auto-generated default constructor prototype. ~Test(); // Auto-generated default destructor prototype. static void showInfo(); int getId(); }; #endif //__Test_H_ Test.cpp file ================== #include "Test.h" int Test::count = 0; Test::Test() { id = ++count; } Test::~Test() { } void Test::showInfo() { cout << count << endl; } int Test::getId() { return id; } main.cpp =============== #include <iostream> #include "Test.h" using namespace std; int main() { cout << Test::MAX << endl; Test::showInfo(); Test test1; cout << "Object 1 ID: " << test1.getId() << endl; Test test2; cout << "Object 2 ID: " << test2.getId() << endl; Test test3; cout << "Object 3 ID: " << test3.getId() << endl; Test test4; cout << "Object 4 ID: " << test4.getId() << endl; Test::showInfo(); return 0; }
15th Sep 2017, 8:54 PM
ChaoticDawg
ChaoticDawg - avatar