8 Replies - 489 Views - Last Post: 03 February 2013 - 09:51 PM Rate Topic: -----

#1 DahDoctow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 19-June 11

Prototyping Classes in C++

Posted 28 January 2013 - 08:59 PM

Hello, I was wondering if it was possible to prototype classes in C++. Rather than have everything above the main function, I`d like to organize it better and put certain classes below the main function as well.

I know about the possibility of prototyping functions, which does help out quite a bit! Just hoping any of you know of any possibility of doing the same for classes.

I`ll post my code for my project, which is literally just me learning classes in C++. Just incase anybody here needed it.

#include <iostream>

using namespace std;

//The class I wish to appear below the "int main" function.
class TestClass{
public:
    void sayings()
    {
        cout << "Your computer is dead to me." << endl;
    }
};

//Just the main function, which prints out the sayings function within the TestClass class.
int main()
{
    TestClass testObject;
    testObject.sayings();
    
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Prototyping Classes in C++

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Prototyping Classes in C++

Posted 28 January 2013 - 10:36 PM

Yes you can forward declare classes.

i.e.

class Foo;

int main() {
   Foo x;

   return 0;
}

class Foo {
};


Was This Post Helpful? 1
  • +
  • -

#3 DahDoctow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 19-June 11

Re: Prototyping Classes in C++

Posted 28 January 2013 - 10:46 PM

Oh, awesome. No wonder it didn't work, I tried putting in brackets as if it were a function "()" without realizing classes don't have variable containers.

Thanks a lot, and much appreciated!
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1397
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: Prototyping Classes in C++

Posted 28 January 2013 - 11:06 PM

Don't know that this would work in that case.

18	    testObject.sayings();




Header and implementation files are used to organize code.
How to Create a C++ Header File
Was This Post Helpful? 1
  • +
  • -

#5 DahDoctow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 19-June 11

Re: Prototyping Classes in C++

Posted 29 January 2013 - 09:18 PM

View Post#define, on 28 January 2013 - 10:06 PM, said:

Don't know that this would work in that case.

18	    testObject.sayings();




Header and implementation files are used to organize code.
How to Create a C++ Header File


Hmm... You're right it doesn't. Do you happen to know why it didn't work for that particular case?

Nevermind, I accidentally missed your link upon first scroll. So sorry.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1397
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: Prototyping Classes in C++

Posted 29 January 2013 - 10:16 PM

18      testObject.sayings();



When a compiler encounters a call to a function, as here, the compiler should check that the call is correct (parameters types etc). So it needs to have a definition/declaration of the function head beforehand.
Was This Post Helpful? 1
  • +
  • -

#7 DahDoctow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 19-June 11

Re: Prototyping Classes in C++

Posted 01 February 2013 - 09:58 PM

I understand what you're saying, though was still wondering if there were to be a workaround for this.

I've tried:

class TestClass;
void sayings();


As well as

class TestClass;
TestClass.sayings();


But neither seem to work.
Was This Post Helpful? 0
  • +
  • -

#8 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 03-November 11

Re: Prototyping Classes in C++

Posted 01 February 2013 - 10:52 PM

Not sure, but try:
class TestClass;
void TestClass::myFunction();




You should use header files tho.

This post has been edited by DoNotWant: 01 February 2013 - 10:54 PM

Was This Post Helpful? 1
  • +
  • -

#9 DahDoctow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 19-June 11

Re: Prototyping Classes in C++

Posted 03 February 2013 - 09:51 PM

Thanks for that, I got it working haha.

But yeah, the reason it wasn't in a header file was because i'm currently following a tutorial. I wanted to experiment with some new stuff that he didn't explain thoroughly enough, and yeah I really appreciate you guys giving me a much bigger knowledge about what i'm learning.

:) Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1