473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Beginner Help: Joining Multiple classes in multiple files?

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
Jul 22 '05 #1
3 1663
JHenstay wrote:
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.


The basic idea is this:

// file: animal.hpp (header)
class Animal{
public:
int GetAge();
void SetAge();
private:
int Age;
};

// file: animal.cpp (implementation)
int Animal::GetAge(){
return Age;
}
void Animal::SetAge(int iNewAge){
Age = iNewAge;
}

// file: main.cpp
#include "animal.hpp"

int main(){
Animal a;
}

If you want the cats and dogs to be able to use each other, things
become a little more tricky. There's probably a section on "include
guards" in the FAQ for when you get to that point.

HTH,
Jacques

Jul 22 '05 #2
JHenstay writes:
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.

<snip>

Most of what you want to know is specific to the mechanics of a particular
compiler. A command line oriented compiler uses "make" files, a GUI
oriented compilers may use something called a "project". Try re-posting in
a more specific group. Using one of the two hot words above might help
find something in google advanced groups so you can refine your question
before posting again.
Jul 22 '05 #3
Jacques,
Thanks for the reply! The example I gave I think is slightly too
simple because I can get that to work fine. After I posted my message
I went back and tried some other things and the bizarre thing is, I
can get the code to compile/link fine under the free command line
Borland Compiler, but Visual C++ threw all sorts of errors. So I
posted a message to the Visual C++ group with my code asking for more
specific help.

Thanks again!

-JH

Jacques Labuschagne <ja*****@clawshrimp.com> wrote in message news:<Qk********************@news02.tsnz.net>...
JHenstay wrote:
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.


The basic idea is this:

// file: animal.hpp (header)
class Animal{
public:
int GetAge();
void SetAge();
private:
int Age;
};

// file: animal.cpp (implementation)
int Animal::GetAge(){
return Age;
}
void Animal::SetAge(int iNewAge){
Age = iNewAge;
}

// file: main.cpp
#include "animal.hpp"

int main(){
Animal a;
}

If you want the cats and dogs to be able to use each other, things
become a little more tricky. There's probably a section on "include
guards" in the FAQ for when you get to that point.

HTH,
Jacques

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lawrence | last post by:
Dear Peter, Do we know anyone else who writes PHP code? There is too much work to do, especially if Costin and I are going to join our software together. The easiest way for us to join our...
10
by: Robert | last post by:
Where can i find a free web- based VC++ course? (also i've to dowanload it) I'm interested also in VB and C++ thanks, Robert
2
by: Bodger | last post by:
Hi, Bearing in mind that I am new to this game, understand very little about SQL and don't work in IT, I was wondering if someone could give me some friendly advice on how to design...
2
by: Mark | last post by:
I have one VS.NET 2003 solution that contains 3 projects. Two of the projects are ASP.NET projects each with their own .dll. The third project is a class library with its own .dll. We'll refer...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
1
by: Ranginald | last post by:
Hi, I have an asp.net project and I am in the process of trying to make it into a truly object-oriented project -- as I have just learned I cannot have multiple codebehind files in a single...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.