Thread: Class + Main() = error cout?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    17

    Question Class + Main() = error cout?

    Can someone help me out with this problem? Basically, I have a "Classes" class inside one of my file. Below is the code:

    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;
        }
    };
    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:
    //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;
    }
    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.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The constructor is called on the creation of each instance of the class at runtime, not at compilation. You create two different instances of your class, a and b, so of course the constructor runs twice.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A constructor is executed once for each instance created of the object, in this case for both a and b. If you want something to only execute once per program, don't use a constructor.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    17
    Oh my, I understand now! Thanks guys

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, you don't need to put public: before everything in the class; just once is fine. This ain't Java.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ya, public/protected/private are sections. After the keyword, every function/variable will be public, protected or private until the end of the class or another keyword.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, using statements in header files are a big no-no. Explicitly qualify access to elements of namespace std instead.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM