Can someone help me out with this problem? Basically, I have a "Classes" class inside one of my file. Below is the code:
The class which Ive created included one Class constructor. Now, I created a console application which link to that "Classes.hpp". Below is the codes for the main():Code://Classes Class.hpp #include <iostream> #include <string> using namespace std; class Classes { public: string favouriteLanguage; public: Classes() //Classes' constructor { cout<< "Classes' has been executed"<< endl; } public: void Display() { cout<< "I love "<< favouriteLanguage<< endl; } };
Now, when I compile, the constructor should be automatically executed right? That's what my program did. It executed, but TWICE?~! Can someone help me out? When I compile and build it, the "Classes" constructor executed twice...I want it to be executed once only. Thanks in advance for the help.Code://instantiate object from banner class #include "Classes.hpp" int main() { Classes a; Classes b; a.favouriteLanguage = "C++"; a.Display(); b.favouriteLanguage = "Java"; b.Display(); std::cout<< "The value that dominates all is "<< b.favouriteLanguage<< endl; return 0; }