c++ classes

Jasonh100

Senior member
Apr 21, 2001
200
0
0
I'm trying to make a class that is a queue of more then one type of object. I can add the diff types of objects using function prototyping but when I'm ready to display them, how can I tell one type from another b4 I assign a pointer to the address that is stored in the queue (there is no class inheritance involved so I can't just use the base class name -- or is there something similar for non inherited functions?). Anyways, it seems to me that I'd have to have sorta of a buffer in the queue that is read, and then in it has info on whether or not what it points to is one thing or another and branch somewhere accordingly.

Bout to go to work so I haven't had time to test yet but let me know what you think. I'm a newbie c++ programmer so I'm probably missing something obvious that will be easy so do feel bad about pointing out something that seems simple/simple to you.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
The easiest way is to have the contained objects "display" themselves. ie. all the object types that you expect this queue container to work with would have a display function with the same function name & argument list, so the queue object wouldn't need to know what the contained objects are.

There are other ways of course, look up RTTI (Run Time Type Identification) as another approach.
 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
How would you call the function that gets it to display itself? Don't you have to have a pointer to the object of the correct type.

thnx for the help
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Jasonh100
How would you call the function that gets it to display itself? Don't you have to have a pointer to the object of the correct type.

thnx for the help

You don't need RTTI for that. Just have all of the classes that this queue object can contain be derived from some abstract base class with a display function. Then you can just use virtual display functions in each of the object classes. Now your pointers will always resolve to the correct display function.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Here's a quick example:

The output is:
Area of Shape: 78.5398
Area of Shape: 25

//*********************************************************************
#include <fstream.h>
#include <string.h>
#include <math.h>

class shape
{
private:
public:
shape(void) {}
virtual void init(double) = 0; //pure virtual function
virtual double area(void) const = 0; //pure virtual function
};

class circle : public shape
{
private:
double radius;
public:
circle(void) {init(0); return;}
circle(double r) {init(r); return;}
virtual void init(double r) {radius = r; return;}
virtual double area(void) const {return M_PI*radius*radius;}
};

class square : public shape
{
private:
double side;
public:
square(void) {init(0); return;}
square(double s) {init(s); return;}
virtual void init(double s) {side = s; return;}
virtual double area(void) const {return side*side;}
};

int main(void)
{
shape *x;

circle c;
square s;

x = &c;
x->init(5);
cout << "Area of Shape:\t" << x->area() << endl;

x = &s;
x->init(5);
cout << "Area of Shape:\t" << x->area() << endl;

return 0;
}
 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
the classes can't be derived from the same base class. The queue is to be able to hold other queue objects along w/ the usual items that could be derived from the same base class.

 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
actually, I guess it could be derived. It just wouldn't make very much since . I think it work ok though. And the queue implementation takes very little memory overhead.
 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
I don't think that way will work either. Here is the class definition and it doesn't work bc I can't create a Queue object inside the class declaration.

class Queue {
struct Node {
class Queue item;
struct Node * next;
};
private:
Node * first;
Node * last;
//if qsize = 0 - derived class
int qsize;
int items;
public:
Queue();
Queue(int qs);
virtual ~Queue();

bool enqueue(const Queue & add);
virtual void dspqueue(void);
};

class String : public Queue {
private:
public:
char * st;
String(const char * cha = "Unint");
~String();
void dspqueue(void);
};
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
First, do yourself a favor, use the STL queue or use it as a member inside your class Second, are you going to store instances of the objects or just pointers to the objects (inside the queue I mean)?
 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
instances.

I'm just doing it to get practice and learn a few tricks because I'm a beginner. Also, I don't know how to use STL yet
 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
Although this may be a good time to learn about STL.

Btw, what happened to the option of editing a message?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: Jasonh100
instances.

I'm just doing it to get practice and learn a few tricks because I'm a beginner. Also, I don't know how to use STL yet

Pointers would be a lot easier if you want to support *any* object (as long as they are all derived from the same base class). Pick up a good book on learning STL. Make your life easier

 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
reading about it currently.

and I forgot that it had to be a pointer to even work w/ other classes derived from the same class.
 

Jasonh100

Senior member
Apr 21, 2001
200
0
0
WOOHOOO!!! I got it

I originally had the item object in the Node structure a pointer and then I took that off bc I forgot why it was like that. So then when I put it back I finally realized that when I was creating new memory to store copy the info pointed to by the pointer sent to the enqueue function I wasn't specifying what which class it was. . .

Queue * temp = new Queue;
Queue * temp = new String;

so now I just pass a character also that determines which type of class is being enqueued.

So now, for no apparent reason, I can make a tree of information:

+Root Queue
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!

or you can enqueue a queue to itself and get . . .

+Root Queue
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!
--Useless string
-+Useless Queue
---+lalala
------lol
----you prob get it by now
--What?
--More useless information!

....

and you can watch your free memory go down and down and down

thnx for your ergerouge and singh!

... now back to learning what STL is
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |