Association and composition example design

Apr 1, 2015 at 9:17pm
Hi I am really frustrated..How can I design these three classes to interact with each other

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Sale{

     private:
        float total_sale;
        int total_books_bought;
        
     public:
         set_totalSale(float);
         set totalBook(int);
 
         //getters
}

class Book{
     
        string bookTitle;
        int copies;

         public:
            set_title(string)
            set_copies_(int);
            //getters
            void addcopies(int);
            void decreasecopies(int);
}

class Member{
       
         string memID;
         float total_paid;
         int total_books_bought;

         public:
             //setters and getters

}

I am to relate these classes in association to store every sale to a member or a non-member....


I wrote so many lines of code but not working....really frustrated

help!pls
Apr 1, 2015 at 10:35pm
1
2
3
4
5
6
7
8
9
10
11
class Member{
       
         string memID;
         float total_paid;
         int total_books_bought;

         public:
             Sale sales;
             //setters and getters

}


Aceix.
Apr 1, 2015 at 10:41pm
uhm... I have no idea what Aceix is trying to say... :D But. If you want them to interact with eachother, you have to use Inheritance. That allows you to then use polymorphism and other good stuff. You can easily google and learn what this is ,you can check the tutorials on this website, or you can watch youtube videos of people explaining things, which is what I prefer.

Video 52: Inheritance.
Video 55-57 : Poly, Virtual Functions, Pure Virtual Functions.

https://www.youtube.com/watch?v=tvC1WCdV1XU&list=PLAE85DE8440AA6B83&ab_channel=thenewboston

There are also video of composition which you should check out. There are also other youtubers who explain things in a good way. I recommend checking out CodingMadeEasy's youtube channel, playlist is called C++ Made Easy.
Apr 1, 2015 at 10:59pm
What I'm saying is that for every Member, there should be a Sale "book". Inheritance will not allow you to easily access private members.

Aceix.
Apr 1, 2015 at 11:01pm
hmm No, no you did not say that in any way shape or form. You literally just copy pasted his class, like that was gonna do any difference. Just becuase it made sense in your head, doesnt mean it makes sense in others.
Last edited on Apr 1, 2015 at 11:02pm
Apr 2, 2015 at 7:05am
Thanks everyone....How about this?

1
2
3
4
5
6
7
8
9
class Sale{

             private:
                   ........
                   Book *book;
                   Member member;
            public:
                 .......
}


Thanks
Apr 2, 2015 at 7:12am
Now you've created a member object and a pointer to a book object in the sales class, that is a kind of interaction yes.
Apr 3, 2015 at 2:41am
I am doing like Aceix said...

Its going well until now
Topic archived. No new replies allowed.