Hello there guys,

I'm new to C++ and i've got something to ask. I want to create several classes at different .cpp files. But i want to create objects from some of classes at other classes. let me explain it with an example:

Main.cpp
ClassA.cpp
ClassB.cpp

These are my cpp files. I want to create an object of ClassA at ClassB. Then i want to create them at my main.cpp file and use their functions. I think i need to create a header file, which i cant really seem to be succesful at, to acomplish this but i'm not sure what i really have to do :( If it is done by creating header file, how should it be ?

My other question is, i need a queue class to satisfy my queue needs but i want to allocate queue's space with the number i read from a file. There are queue examples that uses queue library but i cant find any property to make it dynamic. Is it possible to use that library and makke it dynamic at the same time ?

Thanks for your time :)

Recommended Answers

All 13 Replies

If your whole class, including all your class' function declarations looks like this:

class A {
int n;
public:
  A::A(int num);
  void disp_num();
};

A::A(int num) {
    n = num;
}

void disp_num() {
    std::cout << "n = " << n << std::endl;
}

Then you'll have to put the following in the header file:

class A {
int n;
public:
  A::A(int num);
  void disp_num();
};

Edit:: As you classes are spread amongst multiple files, don't forget to compile them all together :)

Tux, you should also tell him to place the conditional compilation preprocesor directive so that his files are not included more than once:

#ifdef MYHEADER_H
#define MYHEADER_H
//content of the files
#endif

This will make sure that this header file will be included only once even if you issue two #include directive.

commented: Right, I forgot :P +5

Okay, I haven't fully understood your first question, but I'll try and clear up parts of it that I have understood. Just tell us what you want to do in context of the following, or, if this isn't it, could you explain what it is that you want to do more clearly?

You have two classes defined in two different files, and you want to create the first class's object ( ClassA ) in the second class ( ClassB ). So basically, you would like to instantiate a single object of one of the classes in the other? Is this what you are trying to do?

OR

You have two classes defined in two different files, and you want to make the properties and behaviors of the first class ( ClassA ) to be made available through the second class ( ClassB ), and you want to create a single object of ClassB in your main() function, through which you can access all the methods provided by both of them?

And the answer to your second question:

Of course its possible to make a queue dynamic. You can do it by implementing a queue as a linked list. If you do this, then there is no need to set a maximum limit to number of data items that you can keep adding to your queue.

A simple implementation of such a system can be found here: http://www.dreamincode.net/forums/showtopic10157.htm

Cheers!

EDIT: Whoops! I didn't see the first 2 replies :$ ... but still, it would be clearer for us if you could clarify your first question.

Let me explain it clearly for you amrith92,

What i want is;

ClassA

class ClassA{
private:
         ClassB b;
         .
         .

And main should be looking like;
Main

int main(){
ClassA a;
ClassB b;
.
.
.

I tried something but cant seem to build all together. I'm using dev c++ and i think its the problem because it gives "3 C:\Documents and Settings\Ciyan\Desktop\deneme\main.cpp main.h: No such file or directory. " error. And here is my codes:

class BClass 
{
      private:
              int tip;
      public:
             BClass:BClass();
             BClass:getTip();
             BClass:setTip();
};
class AClass 
{
      private:
              int type;
      public:
             AClass:AClass();
             AClass:getType();
             AClass:setType();
};
class AClass
{
      private:
             int type;
             BClass b;
      public:
             AClass(){type = 5;}
             void getType(){return type;}
             int setType(int type){this->type = type;}
}
class BClass
{
      private:
             int tip;
      public:
             BClass(){tip = 5;}
             void getTip(){return tip;}
             int setTip(int tip){this->tip = tip;}
}
#include <cstdlib>
#include <iostream>
#include <main.h>

using namespace std;

int main()
{
    AClass a;
    BClass b;
    cout<<"AClass: "<<a.getType()<<endl;
    cout<<"BClass: "<<b.gettip(<<endl;)
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ofcourse there will be lots of change its the base of what i think to do, i just need to figure it out to how to do it.

Secondly; if i use linked list solution for queue, is it dangerous for me to allocate 100.000 records without preallocating ? And which one would be fast ? Because in my project i need to be fast :/

Thanks for your helps anyway guys :)

#ifdef MYHEADER_H
#define MYHEADER_H
//content of the files
#endif

Shouldn't that be #if[B]n[/B]def MYHEADER_H ?

I think that your problem here is this line here:

#include <cstdlib>
#include <iostream>
#include <main.h> // <- See this?? What is this?? Try deleting this

using namespace std;

int main()
{
    AClass a; // <- What is this & where is it from??
    BClass b; // <- The same thing ^

    cout<<"AClass: "<<a.getType()<<endl;
    cout<<"BClass: "<<b.gettip(<<endl;)
    system("PAUSE");
    return EXIT_SUCCESS;
}

Are you completely sure that this compiled?

And the other question - You wouldn't need to allocate memory altogether when you deal with linked lists... memory is allocated dynamically in the heap when a new node is added, and most modern computers can handle a linked consisting of a few 100,000 integers (=200,000 bytes) for example. As for the speed, linked lists are far more efficient than arrays at storing data, so this wouldn't be an issue. But for storing enormous amounts of data, then you would have to look for an alternative way to do this, such as a Hash Table.

Shouldn't that be #ifndef MYHEADER_H ?
Yes it should be.

I think that your problem here is this line here:

#include <cstdlib>
#include <iostream>
#include <main.h> // This is my header file as i posted at my  previous post

using namespace std;

int main()
{
    AClass a; // This &
    BClass b; // This is my classes that i write at different cpp files and declared at header file

    cout<<"AClass: "<<a.getType()<<endl;
    cout<<"BClass: "<<b.gettip(<<endl;)
    system("PAUSE");
    return EXIT_SUCCESS;
}

I know that there is something wrong in my code but as far as i understand, thats the way it should be. Please enlighten me :(

Are you completely sure that this compiled?

And the other question - You wouldn't need to allocate memory altogether when you deal with linked lists... memory is allocated dynamically in the heap when a new node is added, and most modern computers can handle a linked consisting of a few 100,000 integers (=200,000 bytes) for example. As for the speed, linked lists are far more efficient than arrays at storing data, so this wouldn't be an issue. But for storing enormous amounts of data, then you would have to look for an alternative way to do this, such as a Hash Table.

Thanks for he heads up for queue by the way. Your help is much appreciated :)

Okay, lets see:

// Classes.h

#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED

class BClass
{
     /*...*/
}; // <- I forgot this, my mistake

class AClass
{
      /*...*/
};

#endif

// Classes.cpp

#include <Classes.h>

/*Class Defenitions*/

//main.cpp

/*... #includes go here */
#include <Classes.h>

using namespace std;

int main()
{
    ClassA a;
    ClassB b;
    /*... Do something here ...*/

    return EXIT_SUCCESS;
}

Just follow the pattern above and employ it in your own code, and see if it helps...

commented: I like your approach :) +5

OK guys, i finally managed to solve this :) Thanks all for your help. But there is one question that i cant seem to compile my project if i dont declare my header files directory with its full path :S I cant write it like #include<Classes.h>, it wont let me to do :/ Anyway, not that such a big deal but if you can tell me why it is happening, i would be happy :)

OK guys, i finally managed to solve this :) Thanks all for your help. But there is one question that i cant seem to compile my project if i dont declare my header files directory with its full path :S I cant write it like #include<Classes.h>, it wont let me to do :/ Anyway, not that such a big deal but if you can tell me why it is happening, i would be happy :)

Header files in different folders can be included in your project, as long is it is in the same disk partition(C:\, D:\ etc), but you can access it only in relation to the folder from which you are compiling your project from. For eg:

This is a folder Hierarchy:

[-] Folder1
     [-] SomeFolder
          [-] AnotherFolder
               - main.cpp
               - Classes.cpp
          - Classes.h
     - Fuzzy.h
[+] Folder2
etc, etc

So, in this case, your project will be compiled from the Folder "AnotherFolder" which contains the files main.cpp and Classes.cpp . But you would have to #include Classes.h in your file Classes.cpp , but then the header file is in the parent Folder, "SomeFolder". To successfully access the header file, you will have to write your #include statement as : #include "../Classes.h" This tells the compiler to look for Classes.h in the directory immediately above the one in which you are working.

Similarly, if you have to include the header Fuzzy.h , which resides in the topmost directory "Folder1", in main.cpp , the

#include

statement for it would look like: #include "../../Fuzzy.h" Hope this clears your doubt,
Cheers!

>I cant write it like #include <Classes.h> , it wont let me to do

That's because Classes.h isn't in your compiler's header files directory, in that case you'll have to put quotes around it instead of < , if you use quotes like this: #include "Classes.h" , you tell your compiler that Classes.h is in the same directory as your other source files :)

>I cant write it like #include <Classes.h> , it wont let me to do

That's because Classes.h isn't in your compiler's header files directory, in that case you'll have to put quotes around it instead of < , if you use quotes like this: #include "Classes.h" , you tell your compiler that Classes.h is in the same directory as your other source files :)

This worked for me. All of my files were at the same folder ( I compiled the project at Microsoft Visual Studio 2008 Express Edition) and I included my header file like "Classes.h" and it worked.

Thanks guys, i really appreciate your help. Thanks for answering me in a short time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.