473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is it possible to derive a tree from two different parent classes in C++?

Hi guys, I’ve been given a task in which my teachers even cannot help me through! I wonder how can I derive a tree from two different parent classes in C++; i.e. which parameters should be from the first and which from the second? I tried putting the left and right pointers in the second and the data in the first, but due to the difference in types, I am not able to use them?
Thanks by the way,
May 9 '11 #1
3 1678
weaknessforcats
9,208 Expert Mod 8TB
I would need to see your class declaration code.

I assume all of your data is private, that you are using virtul base classes and following the multiple inheritance constructor rules.

Multiple inheritance is not a requirement in OOP but it can lead to elegant solutions.
May 9 '11 #2
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<iomanip.h>
  3. #include<assert.h>
  4. class treenode{
  5.     friend class tree;
  6.     public:
  7.  
  8.         treenode(const int &d):leftptr(0),data(d),rightptr(0){cout<<"base"<<endl;}
  9.         int getdate() const{return data;}
  10.  
  11.     private:
  12.         treenode *leftptr;
  13.         int data;
  14.         treenode *rightptr;
  15. };
  16. //////////////////////////////////////////////////////////1
  17. class tree{
  18.     public:
  19.         tree();
  20.         void insertnode(const int &);
  21.         void preorder() const;
  22.         void inorder() const;
  23.         void postorder() const;
  24.     private:
  25.         treenode  *rootptr;
  26.         void inserthelp(treenode**,const int &);
  27.         void prehelp(treenode*)const;
  28.         void inhelp(treenode* )const;
  29.         void posthelp(treenode*)const;
  30. };
  31. //////////////////////////////////////////////////////////2
  32. tree::tree(){rootptr=0;cout<<"drive"<<endl;}
  33. //////////////////////////////////////////////////////////3
  34. void tree::insertnode(const int &value){
  35.     inserthelp(&rootptr,value);
  36. }
  37. /////////////////////////////////////////////////////////4
  38. int h=0;
  39. void tree::inserthelp(treenode**ptr,const int &value){
  40.     //cout<<"st  " <<ptr<<"  "<<value<<endl;
  41.     if(*ptr==0){//tree is empty
  42.         *ptr=new treenode(value);
  43.         //cout<<"pt  "<<ptr<<"  "<<value<<endl;
  44.         assert(*ptr!=0);
  45.     }
  46.  
  47.     else
  48.         if(value<(*ptr)->data){//cout<<"lef   "<<(&((*ptr)->leftptr))<<endl;
  49.         inserthelp(&((*ptr)->leftptr),value);}
  50.         else
  51.             if(value>(*ptr)->data){//cout<<"ri   "<<(&((*ptr)->rightptr))<<endl;
  52.                 inserthelp(&((*ptr)->rightptr),value);
  53.  
  54.             }
  55.             else
  56.                 cout<<value<<"is duplicated"<<endl;
  57.             //cout<<"h  "<<h<<endl;h++;
  58. }
  59. ///////////////////////////////////////////////////////////5
  60. void tree::preorder()const{
  61.     prehelp(rootptr);
  62. }
  63. ///////////////////////////////////////////////////////////6
  64. void tree::prehelp(treenode* ptr)const{
  65.     if(ptr!=0){
  66.         cout<<ptr->data<<"  ";
  67.         prehelp(ptr->leftptr);
  68.         prehelp(ptr->rightptr);
  69.     }
  70. }
  71. /////////////////////////////////////////////////////////7
  72. void tree::inorder()const{
  73.     inhelp(rootptr);
  74. }
  75. ///////////////////////////////////////////////////////////8
  76. void tree::inhelp(treenode *ptr)const{
  77.     if(ptr!=0){
  78.         inhelp(ptr->leftptr);
  79.         cout<<ptr->data<<"  ";
  80.         inhelp(ptr->rightptr);
  81.  
  82.     }
  83. }
  84. ///////////////////////////////////////////////////////////9
  85. void tree::postorder()const{
  86.     posthelp(rootptr);
  87. }
  88. ///////////////////////////////////////////////////////////10
  89. void tree::posthelp(treenode *ptr)const{
  90.     if(ptr!=0){
  91.         posthelp(ptr->leftptr);
  92.         posthelp(ptr->rightptr);
  93.         cout<<ptr->data<<endl;
  94.     }
  95. }
  96. ///////////////////////////////////////////////////////////11
  97. void main(){
  98.  
  99.     tree inttree;
  100.     int intval;
  101.     cout<<"enter 5 int";
  102.         for(int i=0;i<6;i++)
  103.         {
  104.             cin>>intval;
  105.             inttree.insertnode(intval);
  106.         }
  107.         cout<<"preo\n";
  108.         inttree.preorder();
  109.         cout<<"\ninorder\n";
  110.         inttree.inorder();
  111.         cout<<"\npostorder\n";
  112.         inttree.postorder();
  113. }
May 9 '11 #3
Actually, the task looked like sth like this:
Create a multiple inheritance to derive the below algebraic hierarchical computation: -given a vertical shaped networks over 3 node. Derive the lowest module single object and process the vertical hierarchical data.

I'm starting to think I may have perceived the task incorrectly; maybe, what it requires is not a multiple inheritance indeed.
I am so confused and do not know what to do.
May 9 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Havatcha | last post by:
Does anyone know the syntax for accessing assignment operators in a Parent Class from a Child Class? If so, would be great if you could post a brief example here. Thanks.
5
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write...
3
by: steve bull | last post by:
If I have two classes both of which have unmanaged resources that need to be cleaned up. For example class B is a child of class A and I create an instance of class B. Then, if I call B.Dispose -...
0
by: MilanB | last post by:
Hello, I understand that it is possible to set application level and page level behavior of session state cookieless. Is it possible to set different behavior for session level. In my case some...
2
by: Thomas Beyerlein | last post by:
Is it possible to have different columns of a datagrid bound to different datatables? I am working on a project and the specks have suddenly changed. Now a item can have multiple reports/...
3
by: craig | last post by:
Given two existing but different classes OldA and OldB (that can not be made to derive from any new base class); is there a way to make them both "observer" objects so that they can be put in one...
18
by: Gernot Frisch | last post by:
This code works on VC7.1. But VC6 refuses to compile. class foo { class bar { friend class foo; foo & m_f; public: bar(foo & f) : m_f(f) {}
2
by: Arielle | last post by:
This is an example of what I'm trying to accomplish but anyway.. <table id="myTable1"> <tr> <td><input type="checkbox" onClick="doSomething(this)" value="1"/></td> <td>One</td> </tr> <tr>...
4
by: KishorAditya | last post by:
Hi All, Consider the following scenario: class Top { }; class Left: virtual public Top { }; class Right: virtual public Top { }; class Bottom: public Left, public Right {}; Many books propose...
3
by: 7stud | last post by:
When I run the following code and call super() in the Base class's __init__ () method, only one Parent's __init__() method is called. class Parent1(object): def __init__(self): print...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.