| | |
Class Declaration
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 5
Reputation:
Solved Threads: 0
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
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
If your whole class, including all your class' function declarations looks like this:
Then you'll have to put the following in the header file:
Edit:: As you classes are spread amongst multiple files, don't forget to compile them all together
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;
}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."
Tux, you should also tell him to place the conditional compilation preprocesor directive so that his files are not included more than once:
This will make sure that this header file will be included only once even if you issue two #include directive.
C++ Syntax (Toggle Plain Text)
#ifdef MYHEADER_H #define MYHEADER_H //content of the files #endif
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
(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
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 (
OR
You have two classes defined in two different files, and you want to make the properties and behaviors of the first class (
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.
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."
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."
•
•
Join Date: Jan 2009
Posts: 5
Reputation:
Solved Threads: 0
Let me explain it clearly for you amrith92,
What i want is;
ClassA
And main should be looking like;
Main
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:
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
What i want is;
ClassA
C++ Syntax (Toggle Plain Text)
class ClassA{ private: ClassB b; . .
Main
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
class BClass { private: int tip; public: BClass:BClass(); BClass:getTip(); BClass:setTip(); }; class AClass { private: int type; public: AClass:AClass(); AClass:getType(); AClass:setType(); };
C++ Syntax (Toggle Plain Text)
class AClass { private: int type; BClass b; public: AClass(){type = 5;} void getType(){return type;} int setType(int type){this->type = type;} }
C++ Syntax (Toggle Plain Text)
class BClass { private: int tip; public: BClass(){tip = 5;} void getTip(){return tip;} int setTip(int tip){this->tip = tip;} }
C++ Syntax (Toggle Plain Text)
#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
#ifndef MYHEADER_H ? "Never argue with idiots, they just drag you down to their level and then beat you with experience."
I think that your problem here is this line here:
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.
C++ Syntax (Toggle Plain Text)
#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.
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."
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."
Shouldn't that be #ifndef MYHEADER_H ?
Yes it should be.
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
(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
•
•
Join Date: Jan 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
I think that your problem here is this line here:
C++ Syntax (Toggle Plain Text)
#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; }

•
•
•
•
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.
Okay, lets see:
// Classes.h
// Classes.cpp
//main.cpp
Just follow the pattern above and employ it in your own code, and see if it helps...
// Classes.h
C++ Syntax (Toggle Plain Text)
#ifndef CLASSES_H_INCLUDED #define CLASSES_H_INCLUDED class BClass { /*...*/ }; // <- I forgot this, my mistake class AClass { /*...*/ }; #endif
// Classes.cpp
C++ Syntax (Toggle Plain Text)
#include <Classes.h> /*Class Defenitions*/
//main.cpp
C++ Syntax (Toggle Plain Text)
/*... #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...
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."
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."
![]() |
Similar Threads
- Class declaration syntax error (C++)
- difference between a class declaration and a class definition (C++)
- plz help with the following class declaration in c++ (C++)
- help (overload << in a template class ) (C++)
- String declaration in class definition (C)
- writing a class without the class declaration? (C++)
- 6 Line class -> Me pounding head into wall (C)
Other Threads in the C++ Forum
- Previous Thread: Please Help
- Next Thread: Classes and Constructors
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






