PDA

Click to See Complete Forum and Search --> : C++ Class (not school :o)


JasonLpz
Jan 29th, 2003, 07:39 PM
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.

The Hobo
Jan 29th, 2003, 07:42 PM
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.

JasonLpz
Jan 29th, 2003, 08:12 PM
oh i c ok thanks a bunch

made_of_asp
Jan 29th, 2003, 08:18 PM
Usually the functions and classes themselves a are placed within a .cpp file, header files are used for class and function prototypes.

JasonLpz
Jan 29th, 2003, 10:05 PM
prototypes?

made_of_asp
Jan 30th, 2003, 12:09 AM
prototype example:

(hi.h)


void Hi(); //prototype


(hi.c)

void Hi()
{
printf("Hi\n");
}



include hi.h and use hi() function anywhere in your program.

JasonLpz
Jan 30th, 2003, 12:24 AM
oh i get it now thanks alot

twanvl
Jan 30th, 2003, 02:31 PM
And when using classes it looks like this:

car.h

// 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

#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";
}
}