automatic(dynamic ) Instantiating in c++
Hi to all,
language : C++ Os:Linux I have an example but i dont kno how to implement it in c++. Ex: Suppose I have a System and have many devices (keyboard, printer, scanner network like this) are connected to it. So I can use either composition or Inheritance. but for my application Inheritance is suitable. But my problem is If I more than 1 printer or any device connected dynamically.. that time how can instantiate it dynamically. Note that System is a class and Printer is derived from it. class Printer: public System { char* printertype; char* printervendor; Printer* Printerptr (or Printer Printerobj); public: //some methods// }; Quote:
If this is correct way, which one is good.? |
Try some easier problems first.
I'm making that suggestion as a result of looking at several of your other threads. Your question in this thread apparently demonstrates a lack of understanding of the basic concepts of classes in C++. I don't think you will get that understanding attacking problems that are way beyond your C++ knowledge. Find a C++ tutorial. Maybe you can skip the really basic parts, but when you get to the part about classes, go through that in detail. I think you also need to work through some examples to get an understanding of pointers and references. |
Quote:
The first line of your code says that a Printer is a System, i.e. a Printer provides everything that a System can do and more. And every time you create a new Printer, you are essentially creating a new System. What you probably want is for the Printer class to have a reference to a System. If you have multiple devices, of which Printer is one, then you should have Printer inherit from Device, which should in turn have a reference to the System. This assumes that the Printer needs to know about the System at all; you haven't said why you need a System class. Unless you need it, leave it out. It may also make sense for the System to have a vector of Printer or Device objects, particularly if you want to cope with networked printers shared between systems (or you could have one Printer object per printer per system). You can't put an instance of a Printer inside a Printer object; you would get an infinite loop in construction. You could put a pointer reference to another Printer but it's hard to see why you'd want to. For a linked list of printers, use a separate list<Printer> object. |
All times are GMT -5. The time now is 01:32 PM. |