Association

Pages: 12
I'm really struggling to find out what association is, there aren't any decent tutorials on the internet. I understand that I use it to link two classes. I'm going to use a customer and loyalty card class. But I don't understand what association does that inheritance doesn't and even how I use association. Anyone got a good explanation or a link to a good tutorial?
Sorry I think you might have misread my post :). I understand inheritance but not association, I know it links two classes, but why and how, also how is it different to inheritance :/
With a loyalty card class and a customer class would I associate the customer identity number with the loyalty card number.

Also would I do this by declaring the loyalty card number within the constructor of the customer class and creating an object for this in the methods of the customer class?

Proper don't understand this at all and the tutorials on the net are crap.
Last edited on
Decided to associate Customer ID number with loyalty points, as I believe association is the updating of one member in a class with its associated member in another class.

Any tutorial links on how to do this?
Please

Try to write something and then share them with us.
What would you like? I've already written all the classes, Customer, Person, Staff, Loyalty Card and my main.

I'm just wondering how I go about associating two objects I have within the classes, CustomerID and LoyaltyPoints. I've also got working set and get functions for all of these.

I've also got inheritance up and running for customer and person
Last edited on
you share them with us...
loyaltycard.cpp
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
#include "customer.h"
#include "staff.h"
#include "person.h"
#include <string.h>
#include "loyaltycard.h"



void Loyalty::setLoyaltyNumber(double loyaltyNumber)
{
	loyalty_number = loyaltyNumber;
}
double Loyalty::getLoyaltyNumber()
{
	return loyalty_number;
}


void Loyalty::setLoyaltyPoints(int loyaltyPoints)
{
	loyalty_points = loyaltyPoints;
}
int Loyalty::getPoints()
{
	return loyalty_points;
}


void Loyalty::setLoyaltyExpiry(char loyaltyExpiry[])
{
	 strcpy(loyalty_expiry, loyaltyExpiry);
}
char* Loyalty::getLoyaltyExpiry()
{
	return loyalty_expiry;
}


loyaltycard.h

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
#ifndef LOYALTY_H_INCLUDED  // must be unique!
#define LOYALTY_H_INCLUDED

class Loyalty 
{
      public:
             
      Loyalty();
      
      Loyalty(double loyaltyNumber, int loyaltyPoints, char loyaltyExpiry[]);

      void setLoyaltyNumber(double loyaltyNumber);
      double getLoyaltyNumber();     
      
      void setLoyaltyPoints(int loyaltyPoints);
      int getPoints();       
      
      void setLoyaltyExpiry(char loyaltyExpiry[]);
      char* getLoyaltyExpiry();       
      
            
      private:
      double loyalty_number;
      int loyalty_points;
      char loyalty_expiry[50];
};

#endif 


customer.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "customer.h"
#include "staff.h"
#include "person.h"
#include <string.h>



Customer::Customer(int identity)
{
   human_identity = identity;
}

void Customer::setID(int identity)
{
	human_identity = identity;
}
int Customer::getID()
{
	return human_identity;
}

    
void Customer:: setCardNumber(double payCard_number)
{
         card_number = payCard_number;
}
     double Customer::getCardNumber()
{
     return card_number;
}
    

void Customer:: setExpiry(char card_expiry[])
{
         strcpy(cardExpiry, card_expiry);
}
     char* Customer::getExpiry()
{
     return cardExpiry;
}

void Customer:: setValidDate(char card_fromdate[])
{
         strcpy(cardFromDate, card_fromdate);
}
     char* Customer::getValidDate()
{
     return cardFromDate;
}

void Customer:: setAccountNumber(double accountNumber)
{
        account_number  = accountNumber;
}
     double Customer::getAccountNumber()
{
     return account_number;
}

void 


customer.h
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
#ifndef CUSTOMER_H_INCLUDED  // must be unique!
#define CUSTOMER_H_INCLUDED
#include "person.h"

class Customer : public Person
{


    public:
    Customer();
    Customer (int identity, double payCard_number, char card_expiry[], char card_fromdate[], double accountNumber);
    Customer (int human_identity);
    
    void setID(int identity);
    int getID();
    
    void setCardNumber(double payCard_number);
    double getCardNumber();
    
    void setExpiry(char card_expiry[]);
    char* getExpiry();
    
    void setValidDate(char card_fromdate[]);
    char* getValidDate();
    
    void setAccountNumber(double accountNumber);
    double getAccountNumber();

    protected:
    int human_identity;
    double card_number;
    char cardExpiry[50];
    char cardFromDate[50];
    double account_number;
};

#endif


Don't understand why you need my code to explain how to link them.

I can copy my other class and main if you like but my main is pretty long.

Association can have lots of meanings. Any reference from one class to another is a kind of association. "Has-a" is one; it could also be "uses."


Has-a" is often called aggregation or composition. in terms of C++, it is commonly implemented by a member variable of the owned type or by having a pointer or reference in the owning class to the owned class.

http://publib.boulder.ibm.com/infocenter/rsdhelp/v7r0m0/index.jsp?topic=/com.ibm.xtools.viz.cpp.doc/topics/cassociation.html
Hmm we have been told not to include pointers. I've just read that an updating type of relationship would be a dependency relationship. That's from:
http://publib.boulder.ibm.com/infocenter/rsdhelp/v7r0m0/index.jsp?topic=/com.ibm.xtools.viz.cpp.doc/topics/cassociation.html

What I need to include is association into my program. Have I done that by making customer inherit the functions from a class, I guess that would be a "is a" type of relationship.

Uses can also be implemented as a pointer in the using class to the used class but in this case, the pointer might be changed after construction by a setter method...
Sorry I don't understand any of that as we haven't been taught about pointers and we've been told not to use them. So just to clarrify I don't actually have to add anything else as I've already got association working. I think aggregation is pointers anyways.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Employee{
	int age;
	int name[30];
public:
	Employee()
	{
	   cout <<" I am Employee" << endl;
	}


};

class Manager{
	int x;
	Employee *ptr;
public:

};



A manager is also an employee; the Employee data is stored in the ptr member of a Manager object.Manager is also an Employee . A Manager* is not an Employee*.


1
2
3
4
class Manager :public Employee{
int age;
int name[30];
};



A manager is (also) an Employee , so a manager* can be used as a Employee*.
Last edited on
closed account (z05DSL3A)
Association is a simple structural connection or channel between classes and is a relationship where all objects have their own lifecycle and there is no owner.

Multiple students can associate with a single Department and single student can associate with multiple Departments, but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

To implement this assosiation you may come up with a third class to represent how they are linked, such as Course. Course would implement an associative container or two such as a std::map.
Sorry but I've got no idea what a struct is, haven't been taught about them it's not a degree course I'm on.

Thanks for the help Wolf
you can know what the class but you do not know what the struct.rather interesting..

good luck.
closed account (z05DSL3A)
Here is some code I found that may help...
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>

class Student;
class Department
{    
    std::string name;    
public:    
    Department(std::string Name)    
    {      
        std::cout << "Department::ctor: " << Name << std::endl;      
        name = Name;    
    }    
    std::string Name() const    
    {      
        return(name);    
    }
    ~Department()    
    {      
         std::cout << "Department::dtor: " << name << std::endl;
    }
};

class Student
{  
    std::string name;
public:    
    Student(std::string Name)    
    {      
        std::cout << "Student::ctor: " << name << std::endl;;      
        name = Name;    
    }    
    std::string Name()const    
    {      
        return(name);    
    }        
    ~Student()    
    {      
        std::cout << "Student::dtor "<< name << std::endl;      
    };
};

class Course
{    
    Student * std_p;    
    Department * dept_p;    
    std::string coursename;    
    static unsigned int index;    
    static Course *courseList[4]; 

public:       
    Course(std::string crseName, Student* student, Department* dept)
    : coursename(crseName), std_p(student), dept_p(dept)    
    {      
        std::cout << "Course:ctor: " << coursename << std::endl;      
        if (index < 4)      
        {               
            courseList[index] = this;        
            ++index;      
        }      
        else      
        {        
            std::cout <<"Course list full" << std::endl;      
        }    
    };   
    
    ~Course()    
    {       
        std::cout << "Course:dtor: " << coursename << std::endl;     
            
    };

    static std::string findStudent(char *crseName, char* deptName)    
    {      
        for(int i=0; i<index; i++)      
        {        
            if ( (courseList[i]->getCourseName() == crseName) &&              
                (courseList[i]->getDeptName() == deptName) )        
            {          
                return(courseList[i]->getStdName());        
            }      
        }    
    }    
    std::string getStdName()const 
    {
        return(std_p->Name());
    };    
    std::string getDeptName() const 
    {
        return(dept_p->Name());
    };
    std::string getCourseName()const 
    {
        return(coursename);
    };
}; 
unsigned int Course::index =0;Course* Course::courseList[4] = {0,0,0,0};

int main()
{  
    int i;  
    std::cout<<"\nExample of Association class...\n";  
    std::cout<<"-----------------------------------\n\n";  
    std::cout<<"We have got 4 students ...\n";  
    Student *studentNames[4] = {new Student("Meera"), new Student("Moina"), new Student("Teena"), new Student("Mridula")} ;  
    std::cout<<"\n";  
    std::cout<<"We have got 2 Departments...\n";  
    Department *departNames[2] = {new Department("Mathemetics"), new Department("ComputerSceince")} ;    
    std::cout<<"\n";  
    std::cout<<"Here class Course Associates Student and Department, with a Course name ...\n";  
    Course course1("DataStructure",studentNames[0], departNames[1]);  
    Course course2("Maths",studentNames[3], departNames[0]);  
    Course course3("Geometry",studentNames[2], departNames[0]);  
    Course course4("CA",studentNames[1], departNames[1]);  
    std::cout<<"\n";  
    std::cout<<"Finding a Student using Course and Department...\n";  
    std::cout<<"Student who has taken Maths Course in Mathemetics Department is:"<<Course::findStudent("Maths", "Mathemetics")<<std::endl;    
    std::cout<<"\n";  
    std::cout<<"Deletion of objects...\n\n";  
    for(i=0; i<4; ++i)  
    {    
        delete studentNames[i];  
    }  
    std::cout<<"\n";  
    for(i=0; i<2; ++i)  
    {    
        delete departNames[i];  
    }  
    std::cout<<"\n";  
    return(0);
}
@firix: that was rude and I don't think it was a translation issue.

rej3kt wrote:
Sorry but I've got no idea what a struct is

In C++, a struct is just a class that defaults to public instead of private. For instance, this:

1
2
3
struct Foo {
    int Bar;
}

is the same as this:

1
2
3
4
class Foo {
public:
    int Bar;
}
@filipe

Where is wrong here ??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Employee{
	int age;
	int name[30];
public:
	Employee()
	{
	   cout <<" I am Employee" << endl;
	}


};

class Manager{
	int x;
	Employee *ptr;
public:

};

 


I understand what the man say.But you can not understand me.
From the beginning I tried to help him.
we do not get the money while answering questions.
This is not a job. this is a help.
As far as I understand from the C++
I tried to be helpful.but he was not in good faith.
Last edited on
Pages: 12