i have a c++ project and am using has-a inheritance ... the project is to create a class Sensor then makes is-a inheritance to classes Regular Sensor and CriticalSensor .. uptill now no problem ... i want then to create a class Building that (has) sensors so am using has-a inheritance ... the problem is that i dont know how to make the constructos and destructors of the class Building ... any fast help is appreciated
10x n advance :-)

Recommended Answers

All 4 Replies

A has-a relationship between classes is not an inheritance (see, for example, http://en.wikipedia.org/wiki/Has-a).
What's a problem? can you describe it more specifically?
It's not a question: "I don't know how to write constructors and destructors".

well:

class Sensor{
        Sensor();
        Sensor (int ,doubel , ....) 
};
class RegularSensor:public Sensor{........};
class CriticalSensor:public Sensor{......};

uptill this point all is done

class Building{
     RegularSensor rs;
     CriticalSensor cs;
      int x; // for example
public:
      Building();
      Building(int,const RegularSensor &, const CriticalSensor &);
     ~Building();
....
};

how can i create the the constructors and destructors of building ? :-) i should call the constructors of the RegularSensor and CriticalSensor ... but how can i do this ??

As usually, via ctor-initializers list:

Building::Building(int i, 
    const RegularSensor & r,
    const CriticalSensor & c)
    : x(i), rs(r), cs(c) // that's it: learn C++ more carefully;)
{
   ...
}

10x :) i already did this but when i go:
Building* ptr;
ptr=new Building;
am getting a thread ... i thought something wrong with the constructor ... it is a ptr issue maybe .. i tried everything and did not work ... for example i tried to assign NULL to the pointers and it is the same ...
anyway 10x a lot 4 ur time

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.