944,053 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2580
  • C++ RSS
Mar 19th, 2006
0

Abstract class homework problem

Expand Post »
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.
Similar Threads
rio
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rio is offline Offline
3 posts
since Mar 2006
Mar 19th, 2006
0

Re: Abstract class homework problem

Could you be more specific when you say "relationship"? That's a really broad term.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 19th, 2006
0

Re: Abstract class homework problem

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,

C++ Syntax (Toggle Plain Text)
  1. class base;
  2. class intermediary : public base;
  3.  
  4. class derivedA : private intermediary;
  5. class derivedB : public intermediary;
  6. class derivedC : public base;
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Mar 19th, 2006
0

Re: Abstract class homework problem

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.
rio
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rio is offline Offline
3 posts
since Mar 2006
Mar 19th, 2006
0

Re: Abstract class homework problem

"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:
C++ Syntax (Toggle Plain Text)
  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. };
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 20th, 2006
0

Re: Abstract class homework problem

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Mar 20th, 2006
0

Re: Abstract class homework problem

Quote ...
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:
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 29th, 2006
0

Re: Abstract class homework problem

Thanx for the replies sorry for the late reply.
rio
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rio is offline Offline
3 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Little help with lab? Almost done...
Next Thread in C++ Forum Timeline: Monetary / Compound Interest





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC