|
-
Jan 29th, 2003, 08:39 PM
#1
Thread Starter
Frenzied Member
C++ Class (not school :o)
Hi im back lol thank you all again for all the help. I know how to make and use a class but i was reading this book and got confused allitle. I think it said i can put a class in a header (h) file. Can i ? And i wanted to know if i have a cpp file with some code but not the main code can i still cal it like it was on the main file? or do i have to define it somehow or something
so overview:
1. Can i put a class in a .h
2. How do i use a seperate cpp file that has fuctions and classes in it.
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 29th, 2003, 08:42 PM
#2
Stuck in the 80s
Re: C++ Class (not school :o)
Originally posted by JasonLpz
1. Can i put a class in a .h
Yes.
Originally posted by JasonLpz
2. How do i use a seperate cpp file that has fuctions and classes in it.
Why not use a header file? That's what they're for.
-
Jan 29th, 2003, 09:12 PM
#3
Thread Starter
Frenzied Member
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 29th, 2003, 09:18 PM
#4
Hyperactive Member
Usually the functions and classes themselves a are placed within a .cpp file, header files are used for class and function prototypes.
-
Jan 29th, 2003, 11:05 PM
#5
Thread Starter
Frenzied Member
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 30th, 2003, 01:09 AM
#6
Hyperactive Member
prototype example:
(hi.h)
Code:
void Hi(); //prototype
(hi.c)
Code:
void Hi()
{
printf("Hi\n");
}
include hi.h and use hi() function anywhere in your program.
-
Jan 30th, 2003, 01:24 AM
#7
Thread Starter
Frenzied Member
oh i get it now thanks alot
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
-
Jan 30th, 2003, 03:31 PM
#8
And when using classes it looks like this:
car.h
Code:
// This is to ensure that the class onyl gets declared once
#ifndef INCLUDED_MYPROJECT_CAR_H
#define INCLUDED_MYPROJECT_CAR_H
class car
{
bool driving_;
public:
void start();
void stop();
void crash();
};
#endif
car.cpp
Code:
#include "car.h"
#include <iostream>
void car::start()
{
driving_ = true;
}
void car::stop()
{
driving_ = false;
}
void car::crash()
{
if (driving_) {
std::cout << "CRASH!";
} else {
std::cout << "standing still, can't crash";
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|