Abstract class homework problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 3
Reputation: rio is an unknown quantity at this point 
Solved Threads: 0
rio rio is offline Offline
Newbie Poster

Abstract class homework problem

 
0
  #1
Mar 19th, 2006
The problem we have been set is to make 3 classes which derive from a base class and 2 of these derived classes has a relationship with each other, now i can get only one of the derived classes to have a relationship with the other, but when i try to add a relationship to the other i cant scope problems i think. Any ideas pointers would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 728
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Abstract class homework problem

 
0
  #2
Mar 19th, 2006
Could you be more specific when you say "relationship"? That's a really broad term.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 488
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Abstract class homework problem

 
0
  #3
Mar 19th, 2006
Do you mean to say that 2 of the derived classes share a member which the 3rd one does not?

If so, you probably need an intermediary derived class, which contains those shared member(s) (either public or protected) - and derive the 2 "related" classes from this intermediary class. eg,

  1. class base;
  2. class intermediary : public base;
  3.  
  4. class derivedA : private intermediary;
  5. class derivedB : public intermediary;
  6. class derivedC : public base;
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: rio is an unknown quantity at this point 
Solved Threads: 0
rio rio is offline Offline
Newbie Poster

Re: Abstract class homework problem

 
0
  #4
Mar 19th, 2006
Thanks for the replies so far, the relationship im looking for is a "has a" relationship. The problem i have is similar to this:

class person:
class Father: public person
class Son: public person
class Uncle: public person

Class Father has a Son and Son has a Father, I can create a father has a son but son to father is out of scope.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 728
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Abstract class homework problem

 
0
  #5
Mar 19th, 2006
"Has-A" is typically implemented with data members. To get a circular relationship you can't use straight objects because a class has to be defined before it's instantiated and it can't be instantiated if any of the members have an incomplete type. However, you *can* have a pointer to an incomplete type and use a forward class declaration to create the circular dependency:
  1. class Person {};
  2.  
  3. class Son;
  4.  
  5. class Father: public Person {
  6. Son *child;
  7. };
  8.  
  9. class Son: public Person {
  10. Father *parent;
  11. };
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Abstract class homework problem

 
0
  #6
Mar 20th, 2006
u cant use a claas without declareing it, if u will try to use it will generate a compile time error "missing storage-class type spacifiers"
however u can use a pointer of undeclareed class by giving a forward reference of undeclared class.
this problem arise because when we try to declare an object we tend to allocate memory for it, since class is not declared we dont know what amount of memory should be allocated, however when we declare a pointer we actually do not allocate memort for object therefore it works.

  1. class Person {
  2. char *Name ;
  3. };
  4.  
  5. class Son ; //forward referencing as class Father will use class son, before its decleration
  6.  
  7. class Father : public Person {
  8. Son *pSon ;
  9. public:
  10. Father() {
  11. pSon = NULL ;
  12. }
  13. Father(Son &obj) {
  14. pSon = &obj ;
  15. }
  16. };
  17.  
  18. class Son : public Person {
  19. Father *pFather ;
  20. public:
  21. Son() {
  22. pFather = NULL ;
  23. }
  24. Son(Father &obj) {
  25. pFather = &obj ;
  26. }
  27. };
  28.  
  29. void main(void) {
  30. Father obj_Father ;
  31. Son obj_Son ;
  32. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 728
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Abstract class homework problem

 
0
  #7
Mar 20th, 2006
u cant use a claas without declareing it, if u will try to use it will generate a compile time error "missing storage-class type spacifiers"
however u can use a pointer of undeclareed class by giving a forward reference of undeclared class.
this problem arise because when we try to declare an object we tend to allocate memory for it, since class is not declared we dont know what amount of memory should be allocated, however when we declare a pointer we actually do not allocate memort for object therefore it works.
I'm impressed. It only took you a whole day to repeat my post using different words. :rolleyes:
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: rio is an unknown quantity at this point 
Solved Threads: 0
rio rio is offline Offline
Newbie Poster

Re: Abstract class homework problem

 
0
  #8
Mar 29th, 2006
Thanx for the replies sorry for the late reply.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC