Classes & Arrays

I understand that classes are blueprints, and instances of this class/blueprint can be created at will with different names, each instance uses the properties and methods of the class, but they can be set different values. My question is, 'Can I create instances of a class, but store each instance in an array?.' OR... 'Can I create an array that basically is the class, so each cell of the table, if you will (part of the array, [1], [2]) has a different piece of information in it that relates to the class.'

So here is my class;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CClient
{
      public:
             string fN, lN;                              // fN = First Name  // lN = Last Name
             int cN, cr, ci;                             // cN = Card Number // cr = Credit //
             void set_values(string, string, int, int, int);
             string firstName(){return (fN);}
             string lastName(){return (lN);}
             int cardNumber(){return (cN);}
             int clientID() {return (ci);}
             int credit(){return (cr);}
};

void CClient::set_values(string fName, string lName, int cNum, int cred, int cID)
{
     fN = fName;
     lN = lName;
     cN = cNum;
     cr = cred;
     ci = cID;
};


I was wandering if i could create an instance of the class, CClient customer;. If having a class like this in or part of an array as explained above is not possible. How would I go about storing this into a .txt and being able to read it again afterwards.

So far for inputting the information into a file i have this;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
               ofstream customerRecords ("customerRecords.txt", ios::app);

               if (customerRecords.is_open())
               {
                        customerRecords << "---------------------------------------------"<<endl;
                        customerRecords << "CUSTOMER RECORDS: "<<recordAmount<<endl;
                        customerRecords << "---------------------------------------------"<<endl;
                        customerRecords << "Customer ID:   "<<ID<<endl;
                        customerRecords << "Customer Name: "<<customer.fN<<" "<<customer.lN<<endl;
                        customerRecords << "Customer Card Number: "<<customer.cN<<endl;
                        customerRecords << "---------------------------------------------"<<endl;
                        customerRecords.close();
                        cout<<"         ...Record Successfully Entered..."<<endl<<endl<<endl;
               } else cout<< "         ***Unable To Open File***"<<endl<<endl;


With this, and with the little knowledge i have i very much doubt that this will be able to be read afterwards, as in if i re-open the program or run the .exe again and use a /search feature that i implement.

HERE IS THE COMPLETE CODE I HAVE SO FAR, I HAVE SURPASSED THE ASSIGNEMT REQUIREMENT AND I SEEK THIS KNOWLEDGE JUST TO EXPAND MY OWN SKILLS... So no rush and post whatever may help if you want to...
P.S. Sorry if the code is a mess, its not complete and still in parts, thanks in advance for any help i may recieve :)

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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
//----------------------------------------------------------------------------------------------------------------
//     Variables
//----------------------------------------------------------------------------------------------------------------
      string choice;
      bool run = true;
      int ID=0;
//----------------------------------------------------------------------------------------------------------------
//     Classes
//----------------------------------------------------------------------------------------------------------------
class CClient
{
      public:
             string fN, lN;                              // fN = First Name  // lN = Last Name
             int cN, cr, ci;                             // cN = Card Number // cr = Credit //
             void set_values(string, string, int, int, int);
             string firstName(){return (fN);}
             string lastName(){return (lN);}
             int cardNumber(){return (cN);}
             int clientID() {return (ci);}
             int credit(){return (cr);}
};

void CClient::set_values(string fName, string lName, int cNum, int cred, int cID)
{
     fN = fName;
     lN = lName;
     cN = cNum;
     cr = cred;
     ci = cID;
};
//----------------------------------------------------------------------------------------------------------------
//     Functions
//----------------------------------------------------------------------------------------------------------------
void newCustomer ()                          // Function To Add A New Customer
{
          if (choice == "/new" || choice == "/NEW")
          {
               int recordAmount=0;

               CClient customer;
               customer.ci = ID;
               ID++;
               
               cout<<"         ...Client ID...  : "<<ID<<endl<<endl;
               cout<<"   Please Enter The Customers First Name:    ";
               cin >>customer.fN; cout<<endl;
               cout<<"   Please Enter The Customers Last Name:     ";
               cin >>customer.lN; cout<<endl;
               cout<<"   Please Enter The Customers Card Number:   ";
               cin >>customer.cN; cout<<endl<<endl;
               
               ofstream customerRecords ("customerRecords.txt", ios::app);

               if (customerRecords.is_open())
               {
                        customerRecords << "---------------------------------------------"<<endl;
                        customerRecords << "CUSTOMER RECORDS: "<<recordAmount<<endl;
                        customerRecords << "---------------------------------------------"<<endl;
                        customerRecords << "Customer ID:   "<<ID<<endl;
                        customerRecords << "Customer Name: "<<customer.fN<<" "<<customer.lN<<endl;
                        customerRecords << "Customer Card Number: "<<customer.cN<<endl;
                        customerRecords << "---------------------------------------------"<<endl;
                        customerRecords.close();
                        cout<<"         ...Record Successfully Entered..."<<endl<<endl<<endl;
               } else cout<< "         ***Unable To Open File***"<<endl<<endl;
          }
}
void searchCustomer ()                       // Funtion To Search For Customers
{
          if (choice == "/search" || choice == "/SEARCH")
          {
               cout<<"         ...FEATURE NOT YET IMPLIMENTED..."<<endl<<endl;
          }
}

void takeCredit ()                           // Funtion To Take Credit From The Customers Card
{
          
}

void addCredit ()                            // Funtion To Add Credit To The Customers Card
{
          
}

void exit ()                                 // Function To Exit The Program
{
          if (choice == "/exit" || choice == "/EXIT")
          {
               run = false;
          }  
}
//----------------------------------------------------------------------------------------------------------------
//     MAIN
//----------------------------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
    while (run == true)
    {    
          run = true;
          cout<<endl;
          cout<<"To Enter A New Record Type: /new"<<endl;
          cout<<"To Search For A Record Type /search"<<endl;
          cout<<"To Exit The Program Type /exit"<<endl<<endl;
          cin>>choice; cout<<endl;
          
          newCustomer();
          searchCustomer();
          exit ();
         

    }
    
    return EXIT_SUCCESS;
}
Last edited on
tl;dr
Basically i would like each instance of the class above to be a record, i would then like to store/output these 'record' in some form of file, i would then like to be able to search for them, and delete them if i need to. I think i have the adding sorted. I would also like each record to have its own ID, this will be deleted along with a record if deleted, and replaced by a new one to fill the gap.

Also i would appreciate an answer to the first and second paragraphs in the OP. Thanks again!

EDIT: Also this is the first program like this i have wrote, only program to come close on size and features was pong in the allegro graphics library... I am very much a newbie...
Last edited on
If I understand what you are trying to say here, another try to avoid 1 or 2 character variable names. Another thing is this for a class or Something else?

@Azagaros
The program is for an assignment for class, but i have already done that and have that as a separate project, this is just a more bloated one where i am trying to learn new things such as file inputs, reading and so on.

As for the two letter variables names, thanks for the advice and i will keep that in mind for the future.

Also did you mean to finish your first sentence?
'If I understand what you are trying to say here...'
- It feels like you were meant to carry this on and got sidetracked?

Either way, to clarify:
- Can i create instances of a class, and put each instance into its on slot of an array ( CClient [oneInstance] [twoInstance] and so on)?
- Can each instance of a class be grouped, outputted into a file, and called back into the program for a view or search feature?
Last edited on
I think i might have got it ... would i have to do this?

CClient customer[5]

And then access each property by doing this?

customer[1].ci = 10 -- for example
customer[2].ci = 20


But now is there a way for them to be grouped so they can be easily called back in to be viewed/searched for or deleted or stored into a file, trying to do customerRecords << customer[0] doesn't work, would i need to use a different file type/way of outputting? Binary perhaps? I don't know :/
Last edited on
You are doing it like I would expect as you asked your question.

There are two ways to handle customerRecords << customer[0]. One way is if you know Operator Overloading of << and the other is doing it by brute force like:
1
2
3
4
// nIndex is a counter on a loop.
customerRecord << customer[nIndex].fn;
customerRecord << customer[nIndex].ln;
// and so on until I complete the record. 


I don't know your scope of knowledge to take you beyond this.
Last edited on
I have a large understanding of all of the basics, i have read all of the C++ tutorials on this website apart from the 'Advanced Concepts', i do understand namespaces though. And i can also refer back to all of the documentation on this. I still havn't completely looked at all of the input/output section, but i have been experimenting with that.

I have had lessons inside college aswell as following up tutorials myself as i am keen to learn. If you say what you were going to say i will see if i can understand it, if not, i will try my best or google for more info. I do have a desire to learn programming. So throw it at me! p.s - I do understand your last post...

This is the most complicated program in terms of arrays that i have created, althought it isnt that big. I had to make this for a class assignment... Thanks for your 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
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    double sum=0, avg=0;
    int n=0, entries;
    
    cout<<("How Many Numbers Would You Like To Input?")<<endl;
    cin>>entries;
    
    double numbers [entries];
    
    for (n=0;n<entries;n++)
    {
    cout<<("Please Enter The Numbers You Would Like To Find The Average Of:")<<endl;
    cin>>numbers[n];
    
    sum=sum+numbers[n];
    avg=sum/entries;
    }

    cout<<("Average of the entered numbers:")<<avg<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.