C++ Learning Community Forum
September 09, 2010, 07:42:16 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: classes and class prototype  (Read 593 times)
oulyt
C++ Freak
***
Posts: 340



View Profile
« on: February 01, 2009, 04:24:57 AM »

i was just reading my C++ book about classes but it did not reallly explain the file set up of them. like the class itself goes in a .h then the prototype where you define in a .cpp? and then you have where main is thats in a .cpp too. but i thought only things with int main() went in .cpp how would i call the .cpp with the prototype? or how would i know to call it. it did not in the book. (i use code::blocks)
Logged
*brammie*
my rank:
C++ Freak
***
Posts: 310



View Profile WWW
« Reply #1 on: February 01, 2009, 01:07:13 PM »

all .cpp files in your project are compiled

so if you define everything in a .h file:
Code
GeSHi (cpp):
class CMyClass
{
   public:
       CMyClass();
       ~CMyClass();
       MyFunction();
}
 
Created by GeSHI 1.0.7.18
you can implement that in a .cpp:
Code
GeSHi (cpp):
#include "CMyClass.h"
CMyClass::CMyClass()
{
}
CMyClass::~CMyClass()
{
}
void CMyClass::MyFunction()
{
   cout << "Lol" << endl;
}
 
Created by GeSHI 1.0.7.18
and then, if you want to use your class:
Code
GeSHi (cpp):
#include "CMyClass.h"
int main()
{
   CMyClass foo;
   foo.MyFunction();
}
 
Created by GeSHI 1.0.7.18

having the definition of a class in a header makes it usable in more than 1 .cpp file
if you get errors about redifinition you can add this your header:
Code
GeSHi (cpp):
#ifndef CMYCLASS_H
#define CMYCLASS_H
//your class
#endif
 
Created by GeSHI 1.0.7.18
Logged
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #2 on: February 01, 2009, 04:12:29 PM »

hmm so all the .cpp are auto compiled and would i need to tell the project thing that the .cpp is part of it? or would it auto know if it was in the same folder?

and why does everyone keep using foo >.<
Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1241



View Profile
« Reply #3 on: February 01, 2009, 06:54:53 PM »

Quote
and why does everyone keep using foo >.<
http://en.wikipedia.org/wiki/Foo  Wink
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #4 on: February 01, 2009, 06:57:37 PM »

Quote
and why does everyone keep using foo >.<
http://en.wikipedia.org/wiki/Foo  Wink

i see thanks Cheesy its finally good to know. after running around the forum and seeing everyone useing foo
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!