LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2009, 06:31 AM   #1
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
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:
Printer* Printerptr (or Printer Printerobj);
Is this is way to create multi printer objects..
If this is correct way, which one is good.?
 
Old 10-10-2009, 06:51 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.

Last edited by johnsfine; 10-10-2009 at 06:53 AM.
 
Old 10-10-2009, 10:15 AM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Originally Posted by naveenisback View Post
class Printer: public System
{
char* printertype;
char* printervendor;
Printer* Printerptr (or Printer Printerobj);

public:
//some methods//
};


Is this is way to create multi printer objects..
If this is correct way, which one is good.?
That looks wrong to me; if, as you say, a Printer is connected to a System then it probably shouldn't be inheriting from it.

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic rsync restart when dynamic IP changes joma Slackware 6 04-19-2006 05:50 AM
Dynamic name resolution for dynamic IP merlin740 Linux - Software 2 10-04-2004 05:56 PM
Automatic dynamic DNS update Mc Leod Linux - Software 1 03-25-2004 05:07 PM
Dynamic IP? Gerardoj Linux - Networking 5 05-27-2003 02:20 PM
dynamic IP........ anoop_chandran Linux - General 4 11-25-2001 01:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration