Thread: Building a class in a single file

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Siloam Springs, Arkansas, United States
    Posts
    12

    Building a class in a single file

    I have a homework assignment to build a linked list that mimics the list library. I do understand how to do this, and I understand all the parts of the list. However, the homework assignment is asking us to build this class in a single file rather than using the normal means of .h and .cpp files.

    It also says that I should be using struct, and since a struct is basically a class that has all public members, to make a private member I would need a class in a class. This part confuses me.

    Just to get me started, can someone please point me in the right direction on how to do something so basic as the constructor?

    In a .h file I would write something like
    Code:
    class linkedList{
    public:
    linkedList();
    and then define that linkedlist in the .cpp file.

    But how am I supposed to make that in a single file?

    Can I simply write the same thing in a single file and the define all of it after I close the class? the
    Code:
     // End class
    };
    
    #endif
    Thank you in advance for any help that can be given.
    Last edited by Amoxaphobic; 11-10-2011 at 02:02 PM. Reason: Fix end class code

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can indeed put the .cpp file inside the header just after the definition. However, that is inadvisable (it's the same as including .cpp files).
    You should write the class inline. Like:
    Code:
    class LinkedList
    {
        LinkedList() { /* My code here */ }
        // ...
    };
    Also, to control access to functions and members, you don't need classes or anything such. Just use the public/private/protected keywords.
    A struct simply defaults everything to public, so put protected/private everywhere you need to restrict access and you're good to go.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Siloam Springs, Arkansas, United States
    Posts
    12
    Awesome. I can always count on you Elysia. Thank you!

    BTW... I still liked your previous signature of "You know everything!" LoL.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Amoxaphobic View Post
    BTW... I still liked your previous signature of "You know everything!" LoL.
    That... might have caused some confusion. So to be on the safe side, I removed it.
    (And I don't have to defend or sound arrogant when responding to the question "how do you know everything?")
    (And no, I am not going to answer that if it's asked.)
    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.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Siloam Springs, Arkansas, United States
    Posts
    12
    LoL. I wasn't asking you to defend anything, and I totally understand your reasoning. I was simply saying that I liked it... you do know a lot. I am very glad that you are willing to help others out with your knowledge. It's people like you that make forums like this a very successful and thriving community. =)

    Thank you.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I just figured I'd share my reasoning for removing it with you.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 06-08-2009, 05:33 PM
  2. building problem, need .h file
    By BadWolf in forum C++ Programming
    Replies: 1
    Last Post: 02-14-2009, 12:12 AM
  3. Storing resources into a single file
    By LuckY in forum Game Programming
    Replies: 20
    Last Post: 08-14-2004, 11:28 PM
  4. building a class
    By jlmac2001 in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2003, 11:35 PM
  5. Single EXE file with BGI's
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-12-2002, 02:45 AM