Class Declaration

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 5
Reputation: dweller is an unknown quantity at this point 
Solved Threads: 0
dweller dweller is offline Offline
Newbie Poster

Class Declaration

 
0
  #1
May 19th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Class Declaration

 
0
  #2
May 19th, 2009
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Class Declaration

 
1
  #3
May 19th, 2009
Tux, you should also tell him to place the conditional compilation preprocesor directive so that his files are not included more than once:
  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.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Class Declaration

 
0
  #4
May 19th, 2009
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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: dweller is an unknown quantity at this point 
Solved Threads: 0
dweller dweller is offline Offline
Newbie Poster

Re: Class Declaration

 
0
  #5
May 19th, 2009
Let me explain it clearly for you amrith92,

What i want is;

ClassA
  1. class ClassA{
  2. private:
  3. ClassB b;
  4. .
  5. .
And main should be looking like;
Main
  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:
  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. };
  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. }
  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. }
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Class Declaration

 
0
  #6
May 19th, 2009
Originally Posted by siddhant3s View Post
#ifdef MYHEADER_H
#define MYHEADER_H
//content of the files
#endif
Shouldn't that be #ifndef MYHEADER_H ?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Class Declaration

 
0
  #7
May 19th, 2009
I think that your problem here is this line here:

  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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Class Declaration

 
0
  #8
May 19th, 2009
Shouldn't that be #ifndef MYHEADER_H ?
Yes it should be.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: dweller is an unknown quantity at this point 
Solved Threads: 0
dweller dweller is offline Offline
Newbie Poster

Re: Class Declaration

 
0
  #9
May 19th, 2009
Originally Posted by amrith92 View Post
I think that your problem here is this line here:

  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

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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Class Declaration

 
1
  #10
May 19th, 2009
Okay, lets see:

// Classes.h
  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
  1. #include <Classes.h>
  2.  
  3. /*Class Defenitions*/

//main.cpp
  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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC