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.

Recommended Answers

All 7 Replies

Could you be more specific when you say "relationship"? That's a really broad term.

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,

class base;
class intermediary : public base;

class derivedA : private intermediary;
class derivedB : public intermediary;
class derivedC : public base;

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.

"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:

class Person {};

class Son;

class Father: public Person {
  Son *child;
};

class Son: public Person {
  Father *parent;
};

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.

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 impressed. It only took you a whole day to repeat my post using different words. :rolleyes:

Thanx for the replies sorry for the late reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.