Thread: Multi-file in C++ with class

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Multi-file in C++ with class

    Hi,
    I am very new to c++ programming ,i getting struct now with working multi-file with class.I am facing problem to access the class which i was declared in other file.for example



    Please i kindly seeking your help to execute more than one source file with class.
    thanks &regards
    Munisamy

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Read the posting rules.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Place the class declaration in a header file. place the class function definations in a cpp file which includes the header file.
    In your other files which use the class include the class header file.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    A couple of points:

    1) An include statement causes the contents of the specified file to be inserted at the spot of the include statement.

    2) Before a name can be used in a file it has to be declared.

    A .cpp file may start off with a definition like this:
    Code:
    void MyClass::someFunc()
    {
        ....
    }
    The problem is neither the name MyClass nor someFunc has been declared in that file, which violates point 2). But if you include the header file for MyClass at the top of the file, it declares both the name MyClass and someFunc, and then you can use those names.

    Since you presumably create some class objects in main(), if you do this:

    MyClass a;

    once again MyClass has to be declared before you can use that name. So, you need to include MyClass.h in main() as well.
    Last edited by 7stud; 12-05-2005 at 09:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM