Hi,

Im wondering if someone could clarify a problem im having.
Basically im trying to figure out how i can go about having a Class A object as an attribute of Class B,while still using Class A's functions.

For example if have a class called clockRadio.In turn i have another class called alarm,

Alarm has the functions
- set time.
- view time

Is it possible for me to create an object of clockRadio and use class Alarms functions through this object.

Im sorry if this does not make any sense but im very confused on this and any help would be greatly appreciated.

Yes you can do that, for example

class A
{
      public:
            int func() { return 1;};
};

class B
{
    public:
          A a;
};

int main()
{
         B b;
         b.a.func();
}

Yes, you can do that.

Class a{

int sec;
int min;
}

Class b{

A a = new a;
new sec = a.sec
}

So would i be correct in saying that to do this i will need to declare the class i wish to associate as a public attribute?

For example

class Radio {

      public:
             Alarm a
}


class Alarm {
     private:

           set/view time
}

radio.a.setTime();

Is this known as association?

Not necessarily. You can have the other class as a private or protected member too or a pointer, or reference, depending on how you are going to use it.

I think it is composition, which if I'm not wrong is a type of association, but you should cross check that.

Thanks for the help,im not so confused anymore.And for future reference it is indeed composition.

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.