LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-19-2012, 10:39 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
Inherent a constant feature from a parent


Hi, if I want to write a C++ program with which I want to store all the common details for a family in the parent level once for all childs, rather than one copy for each child.

Normally if a child is instantiated it creates its parent, and all the variables that is to be held by the parent. If all childs belong to the same parent, then wouldn't it make sense to do the common parent variables once only?

The only way I can think of is to have all the childs hold the a pointer to the same parent, rather than using inheritance.

Can someone please suggest if there is a better way to do this?


Code:
class Parent {
  protected:
    std::string familyName;
  public:
    Parent(std::string surname) : familyName(surname) {
    }

    std::string getFamilyName() {
      return familyName;
    }

    std::string getAddress() {
      return "88 Manhattan Ave";
    }
}

class Child {
  private:
    std::string firstName;
    Parent * parent;
  public:
    Child(std::string firstName, Parent * parent) {
      this->firstName = firstName;
      this->parent = parent;
    }
    std::string getLastName() {
      return parent->getFamilyName();
    }
    std::string getParentAddress() {
      return parent->getAddress();
    }
}

int main() {
  Parent * parent("Cunningham");
  Child child1("John", parent);
  Child child2("Amy", parent);
  child1.getLastName();
  child2.getParentAddress();
}
 
Old 07-19-2012, 11:25 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Your design assumes that a child has a parent (the child could be an orphan). Your design also assumes that the parent and child have the same surname and address; this is not always the case.

If you are attempting to define a family tree, then obviously the parent's are children of other parents.

Anyhow, your design is ok if you are merely attempting a loose association between two objects.

How about something like:
Code:
class Address
{
    std::string street;
    std::string city;
    std::string country;
    ...
};

class Person
{
    std::string firstName;
    std::string lastName;
    ...
};

class Parent : public Person
{
    Address addr;
    std::vector<Child*> children;
    ...
};

class Child : public Person
{
    Address addr;
    Parent* parents[2];
    ...
};
I did not put too much thought into the design above, but basically everyone has an Address and is a Person. Parents have 0 or more Child(ren), and Child(ren) has two Parents.

P.S. Perhaps Parent should be a sub-class of Child?

Last edited by dwhitney67; 07-19-2012 at 11:28 AM.
 
  


Reply



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
Git merging feature 1 and feature 2 mzh Programming 1 06-08-2012 02:57 AM
Sendmail - FEATURE(`dnsbl') - Creating exceptions while using FEATURE(`dnsbl') amonamarth Linux - Server 0 01-25-2010 01:21 PM
File descriptors shared between child and parent causing parent to hang. bharadiaam Linux - Newbie 1 03-02-2009 01:01 AM
LXer: The Inherent Danger in "Just Working" LXer Syndicated Linux News 0 01-03-2009 03:44 AM
Oh, that constant pinging... thosm Linux - Networking 4 07-31-2003 01:32 PM

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

All times are GMT -5. The time now is 01:52 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