| | |
Abstract class homework problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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,
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)
class base; class intermediary : public base; class derivedA : private intermediary; class derivedB : public intermediary; class derivedC : public base;
•
•
Join Date: Mar 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
"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)
class Person {}; class Son; class Father: public Person { Son *child; }; class Son: public Person { Father *parent; };
I'm here to prove you wrong.
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.
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)
class Person { char *Name ; }; class Son ; //forward referencing as class Father will use class son, before its decleration class Father : public Person { Son *pSon ; public: Father() { pSon = NULL ; } Father(Son &obj) { pSon = &obj ; } }; class Son : public Person { Father *pFather ; public: Son() { pFather = NULL ; } Son(Father &obj) { pFather = &obj ; } }; void main(void) { Father obj_Father ; Son obj_Son ; }
•
•
•
•
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 here to prove you wrong.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Little help with lab? Almost done...
- Next Thread: Monetary / Compound Interest
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






