Class prototypes

This is a discussion on Class prototypes within the C++ Programming forums, part of the General Programming Boards category; can you do class prototypes like functions. My problem is that i have two .cpp files and one uses a ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    Class prototypes

    can you do class prototypes like functions. My problem is that i have two .cpp files and one uses a class from another. i have tile.cpp, which has the class tile, and i have layer.cpp, which has the class layer, which needs to use the class tile. I need to make a header file or something to prototype the classes, can this be done.

  2. #2
    alpha
    Guest
    just #include your tile.h file in your layer.cpp file. i think that is what you are asking... or you could #include your tile.h file in your layer.h file...(i think that should work)

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    This is what you do i have the same thing take a look.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <process.h>
    #include "Class_Pub.h"
    #include "Class_Sales.h"
    #include "Class_Book.h"
    #include "Class_Tape.h"
    using namespace std;
    
    
    const int HIGH = 10;
    int main()
    
    {
        
       	Book bk[HIGH];  // array of max amount of book inputs
    	int num_books = 0; // initialize
    	Tape tp[HIGH]; // array of max amount of tape inputs
    	int num_tape = 0; // initialize
        int ch; // for switch statement
    	char again;
    This would bfe like my main file.

    Code:
    #ifndef GUARD_Class_Pub
    #define GUARD_Class_Pub
    
    #include <iostream>
    
    int const MAX = 50;
    ///////////////////////////////////////////////////////////////////////
    class Publication  //base class
    {
    	protected:
    	float price;           //price of book and tape
    	char title [215];          //title of book or tape
    	
    	public:
    
    
    }
    #endif
    This would be like one of my classes i included. Just make sure you make the names the same as the inlude statements.

    note - this is part of my code so it looks kind of funny.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    well i know i could do it that way, but my classes are in .cpp files, and i thought you weren't supposed to #include cpp files, only header files. is there a way to do it this way, or should i just move my classes to header files.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Okay let me clear things up here. You can include a cpp file all you want. The reason why you don't is because it can make your project confusing for others to understand. Also no one suggested that you do so. Here is a very basic example here.

    myheader.h
    Code:
    class MyClass {
    public:
        int GetValue(void) const;
    private:
        int value;
    }
    myclass.cpp
    Code:
    include "myheader.h"
    
    int MyClass::GetValue(void)  {
        return this->value;
    }
    main.cpp
    Code:
    #include "myheader.h"
    #include <iostream>
    using namespace std;
    
    int main(void) {
        MyClass x;
        cout << x.GetValue();
        return 0;
    }
    Of course since MyClass::value is never initialized running this may end up breaking the program but now you know what alpha was talking about.

    There is also something called forword referencing. It is like prototyping for classes.

    Code:
    class myarray;
    class my2darray {
    public:
        myarray array;
    };
    
    class myarray {
    public:
        my2darray array;
    };
    This is a horrible example of why this could be necessary but just put this little bit of knowledge in the back of your brain for when you need it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21