473,748 Members | 2,891 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 1678
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
1909
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
2053
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
1405
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
3717
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
4411
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
1667
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
3990
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
18149
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
8987
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
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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
6793
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
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
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.