943,722 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 894
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 19th, 2009
0

Class Declaration

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dweller is offline Offline
5 posts
since Jan 2009
May 19th, 2009
0

Re: Class Declaration

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
Last edited by tux4life; May 19th, 2009 at 2:47 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 19th, 2009
1

Re: Class Declaration

Tux, you should also tell him to place the conditional compilation preprocesor directive so that his files are not included more than once:
C++ Syntax (Toggle Plain Text)
  1. #ifdef MYHEADER_H
  2. #define MYHEADER_H
  3. //content of the files
  4. #endif
This will make sure that this header file will be included only once even if you issue two #include directive.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
May 19th, 2009
0

Re: Class Declaration

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.
Last edited by amrith92; May 19th, 2009 at 2:57 pm.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 19th, 2009
0

Re: Class Declaration

Let me explain it clearly for you amrith92,

What i want is;

ClassA
C++ Syntax (Toggle Plain Text)
  1. class ClassA{
  2. private:
  3. ClassB b;
  4. .
  5. .
And main should be looking like;
Main
C++ Syntax (Toggle Plain Text)
  1. int main(){
  2. ClassA a;
  3. ClassB b;
  4. .
  5. .
  6. .

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:
C++ Syntax (Toggle Plain Text)
  1. class BClass
  2. {
  3. private:
  4. int tip;
  5. public:
  6. BClass:BClass();
  7. BClass:getTip();
  8. BClass:setTip();
  9. };
  10. class AClass
  11. {
  12. private:
  13. int type;
  14. public:
  15. AClass:AClass();
  16. AClass:getType();
  17. AClass:setType();
  18. };
C++ Syntax (Toggle Plain Text)
  1. class AClass
  2. {
  3. private:
  4. int type;
  5. BClass b;
  6. public:
  7. AClass(){type = 5;}
  8. void getType(){return type;}
  9. int setType(int type){this->type = type;}
  10. }
C++ Syntax (Toggle Plain Text)
  1. class BClass
  2. {
  3. private:
  4. int tip;
  5. public:
  6. BClass(){tip = 5;}
  7. void getTip(){return tip;}
  8. int setTip(int tip){this->tip = tip;}
  9. }
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <main.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. AClass a;
  10. BClass b;
  11. cout<<"AClass: "<<a.getType()<<endl;
  12. cout<<"BClass: "<<b.gettip(<<endl;)
  13. system("PAUSE");
  14. return EXIT_SUCCESS;
  15. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dweller is offline Offline
5 posts
since Jan 2009
May 19th, 2009
0

Re: Class Declaration

Click to Expand / Collapse  Quote originally posted by siddhant3s ...
#ifdef MYHEADER_H
#define MYHEADER_H
//content of the files
#endif
Shouldn't that be #ifndef MYHEADER_H ?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 19th, 2009
0

Re: Class Declaration

I think that your problem here is this line here:

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <main.h> // <- See this?? What is this?? Try deleting this
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. AClass a; // <- What is this & where is it from??
  10. BClass b; // <- The same thing ^
  11.  
  12. cout<<"AClass: "<<a.getType()<<endl;
  13. cout<<"BClass: "<<b.gettip(<<endl;)
  14. system("PAUSE");
  15. return EXIT_SUCCESS;
  16. }

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.
Last edited by amrith92; May 19th, 2009 at 4:18 pm.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 19th, 2009
0

Re: Class Declaration

Shouldn't that be #ifndef MYHEADER_H ?
Yes it should be.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
May 19th, 2009
0

Re: Class Declaration

Click to Expand / Collapse  Quote originally posted by amrith92 ...
I think that your problem here is this line here:

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <main.h> // This is my header file as i posted at my previous post
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. AClass a; // This &
  10. BClass b; // This is my classes that i write at different cpp files and declared at header file
  11.  
  12. cout<<"AClass: "<<a.getType()<<endl;
  13. cout<<"BClass: "<<b.gettip(<<endl;)
  14. system("PAUSE");
  15. return EXIT_SUCCESS;
  16. }
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

Quote ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dweller is offline Offline
5 posts
since Jan 2009
May 19th, 2009
1

Re: Class Declaration

Okay, lets see:

// Classes.h
C++ Syntax (Toggle Plain Text)
  1. #ifndef CLASSES_H_INCLUDED
  2. #define CLASSES_H_INCLUDED
  3.  
  4. class BClass
  5. {
  6. /*...*/
  7. }; // <- I forgot this, my mistake
  8.  
  9. class AClass
  10. {
  11. /*...*/
  12. };
  13.  
  14. #endif

// Classes.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <Classes.h>
  2.  
  3. /*Class Defenitions*/

//main.cpp
C++ Syntax (Toggle Plain Text)
  1. /*... #includes go here */
  2. #include <Classes.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ClassA a;
  9. ClassB b;
  10. /*... Do something here ...*/
  11.  
  12. return EXIT_SUCCESS;
  13. }

Just follow the pattern above and employ it in your own code, and see if it helps...
Last edited by amrith92; May 19th, 2009 at 5:07 pm.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please Help
Next Thread in C++ Forum Timeline: Classes and Constructors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC