473,699 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1672
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*****@clawsh rimp.com> wrote in message news:<Qk******* *************@n ews02.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
1906
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 software together is through high level wrapper classes that take other classes for arguments. Costin and I could move toward joining the software one package at a time.
10
2049
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
1404
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 relationships and/or forms for the following tables: 1) 'Students' - One record per student. Student can choose six different universities, so as well as all of the other student information, the table has six fields (called 'University 1',
2
315
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 to them as project1.dll, project2.dll and myclass.dll. Both project1 and project2 have references to myclass.dll. The build order builds myclass.dll first. When I copy all three .dll into our BIN folder of our site at the same time, the site...
1
3712
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
3
4399
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 really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing version numbers on anything. The errors come and go with no apparent rhyme or reason. We do not...
1
1666
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 page. (The big picture is that I have a template file that has methods called on page_load from a project.cs file and thus each derived template page is "stuck" with the same codefile directive) Now I need to call additional methods on these...
6
3988
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 string UID; public string PWD; }
22
18143
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 programmer and looking for a starting point in regards to practical projects to work on. What are some projects that beginner programmers usually start with? Please list a few that would be good for a beginner PHP programmer to
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.