J
JHenstay
I've been doing quite alot of reading on C++ and classes, however,
everything I read just talks about the code itself and not the
location of the code.
My question is, what if you want to seperate classes into their own
CPP files, what changes are needed to the files and how do you
actually compile and build an executable.
For instance, let's say I have the following all in one file, it would
compile and link into an executable fine:
------------START contents of main.cpp START---------------------
#include <iostream.h>
class Animal
{ public:
int GetAge() { return Age;}
void SetAge(int iNewAge) {Age=iNewAge;}
private:
int Age;
};
class Cat : public Animal
{
public:
int GetLives() { return Lives;}
void SetLives(int iNewLife) {Lives=iNewLife;}
void Die() {Lives--;}
private:
int Lives;
};
int main (void)
{
Cat* Frisky = new Cat;
Frisky->SetAge(3);
Frisky->SetLives(9);
cout << "Frisky is " << Frisky->GetAge() << " years old!" << endl;
cout << "Frisky has " << Frisky->GetLives() << " lives left!" <<
endl;
cout << "Kill Frisky!" << endl;
Frisky->Die();
cout << "Frisky now has " << Frisky->GetLives() << " lives left!" <<
endl;
delete Frisky;
}
--------------END contents of main.cpp END-----------------------
Now, how do I break that up so that I have 3 files so I have:
Animal.cpp (Contains the Animal class)
Cat.cpp (Contains the Cat Class)
Main.cpp (Contains the main function of the code)
What changes do I need to make to each block of code and then do I
compile each cpp file seperately and run some kind of link command to
link all of the OBJ files together?
Any insight into how you do this would be greatly appreciated..
-JH
everything I read just talks about the code itself and not the
location of the code.
My question is, what if you want to seperate classes into their own
CPP files, what changes are needed to the files and how do you
actually compile and build an executable.
For instance, let's say I have the following all in one file, it would
compile and link into an executable fine:
------------START contents of main.cpp START---------------------
#include <iostream.h>
class Animal
{ public:
int GetAge() { return Age;}
void SetAge(int iNewAge) {Age=iNewAge;}
private:
int Age;
};
class Cat : public Animal
{
public:
int GetLives() { return Lives;}
void SetLives(int iNewLife) {Lives=iNewLife;}
void Die() {Lives--;}
private:
int Lives;
};
int main (void)
{
Cat* Frisky = new Cat;
Frisky->SetAge(3);
Frisky->SetLives(9);
cout << "Frisky is " << Frisky->GetAge() << " years old!" << endl;
cout << "Frisky has " << Frisky->GetLives() << " lives left!" <<
endl;
cout << "Kill Frisky!" << endl;
Frisky->Die();
cout << "Frisky now has " << Frisky->GetLives() << " lives left!" <<
endl;
delete Frisky;
}
--------------END contents of main.cpp END-----------------------
Now, how do I break that up so that I have 3 files so I have:
Animal.cpp (Contains the Animal class)
Cat.cpp (Contains the Cat Class)
Main.cpp (Contains the main function of the code)
What changes do I need to make to each block of code and then do I
compile each cpp file seperately and run some kind of link command to
link all of the OBJ files together?
Any insight into how you do this would be greatly appreciated..
-JH