need help for array in derived class

i want to know can we declare a private array base in derived class? if can, can you explain to me.
p/s: do note this is not a complete code

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  #include <iostream>
#include <string>
using namespace std;

class Owner
{
      protected:
      string  department, school, contact_no, status, address;
      string reg_no, type, colour, brand; 
      string name;
      double ID_no; 
      
      public:
     
            void setDetail();
            void getDetail();
     
            
      };
      
class VIS:public Owner
{
      
      Owner list[20]; //This is the part that I ask
      
      public:
             
             void addDetail();
             void searchDetail();
             
      };

      
////global variables declared
int x=0,i=0;
double ID[20];
string n[20];
string sta[20];
string dep[20];
string reg[20];
string scn[20];
string typ[20];
string brd[20];


void Owner::setDetail()
{
      cout<<"********************************************************************"<<endl<<endl;
      cout<<"Please enter name :";
      cin>>name;
      cout<<"Please enter ID number :";
      cin>>ID_no;
      cout<<"Please enter status :";
      cin>>status;
      cout<<"Please enter department :";
      cin>>department;
      cout<<"Please enter school :";
      cin>>school;
      cout<<"Please enter contact number :";
      cin>>contact_no;
      cout<<"Please enter address :";
      cin>>address;
      cout<<endl;
      cout<<"Please insert vehicle registration number:";
      cin>>reg_no;
      cout<<"Please insert type of vehicle :";
      cin>>type;
      cout<<"Please insert colour of vehicle :";
      cin>>colour;
      cout<<"Please insert brand of vehicle :";
      cin>>brand;
      cout<<"********************************************************************"<<endl<<endl;
      ID[i]=ID_no;
      n[i]=name;
      sta[i]=status;
      dep[i]=department;
      reg[i]=reg_no;
      scn[i]=school;
      typ[i]=type;
      brd[i]=brand;
     } 
      
void Owner::getDetail()
{      
     cout<<"********************************************************************"<<endl<<endl;          
     cout<<"Name                        : "<<name<<endl;
     cout<<"ID Number                   : "<<ID_no<<endl;
     cout<<"Status                      : "<<status<<endl;
     cout<<"Department                  : "<<department<<endl;
     cout<<"School                      : "<<school<<endl;
     cout<<"Contact Number              : "<<contact_no<<endl;
     cout<<"Address                     : "<<address<<endl<<endl;         
     cout<<"Vehicle registration number : "<<reg_no<<endl;
     cout<<"Type of vehicle             : "<<type<<endl;
     cout<<"Colour of vehicle           : "<<colour<<endl;
     cout<<"Brand of vehicle            : "<<brand<<endl<<endl;
     cout<<"********************************************************************"<<endl<<endl;
     
}  


                                  
void VIS::addDetail()
{
       
        char redo;
         
         do 
         {  system("cls");
            x++;
            list[i].setDetail();
            i++;
        
            system("cls");
            cout<<"\nThe following owner information has been added to the system:"<<endl;
     
           for(int i=0; i<x; i++)
           {      list[i].getDetail(); }  
                                                     
        cout<<"Do you wont to add another owner"<<endl;  
        cin>>redo;
        }while((redo=='y'||redo=='Y'));
        
}


void VIS::searchDetail()
{

      int c;
     char redo1;
     string name;
     
     do{ 
      system("cls"); 
      cout<<"\t\t*****SEARCH MENU*****"<<endl<<endl;
      cout<<"\t1.Name "<<endl;
      cout<<"\t2.ID Number"<<endl;
      cout<<"\t3.Status"<<endl;
      cout<<"\t4.Department "<<endl;
      cout<<"\t5.School "<<endl;
      cout<<"\t6.Vehicle Registration Number"<<endl;
      cout<<"\t7.Type of Vehicle "<<endl;
      cout<<"\t8.Brand of Vehicle "<<endl<<endl;
      cout<<"\t\t*********************"<<endl<<endl;
      
      cout<<"Search  based on :"<<endl;
      cin>>c;
      
      
      switch(c)
      { case 1: cout<<"Please enter the name of owner : ";
                cin>>name;
                    
                for(int i=0; i<x; i++) 
                { if (n[i]==name) 
                 { cout<<"Matching="<< name<<endl;
                     list[i].getDetail();             
                 }
                }
        break;
        case 2: cout<<"Please enter the ID of owner : ";
                cin>>ID_no;
                    
                for(int i=0; i<x; i++) 
                { if (ID[i]==ID_no) 
                 {   cout<<"Matching="<< ID_no<<endl;; 
                     list[i].getDetail();             
      
                 }
                }
        break;
        case 3: cout<<"Please enter the status of owner : ";
                cin>>status;
                    
                for(int i=0; i<x; i++) 
                { if (sta[i]==status) 
                 {   cout<<"Matching="<< status<<endl;;
                     list[i].getDetail();              
                 }
                }
        break;
        case 4: cout<<"Please enter the department of owner : ";
                cin>>department;
                    
                for(int i=0; i<x; i++) 
                { if (dep[i]==department) 
                 {   cout<<"Matching="<<department<<endl; 
                     list[i].getDetail();             
                 }
                }
        break;
        case 5: cout<<"Please enter the school of owner : ";
                cin>>school;
                    
                for(int i=0; i<x; i++) 
                { if (scn[i]==school) 
                 {   cout<<"Matching="<<school<<endl; 
                     list[i].getDetail();             
                 }
                }
        break;
        case 6: cout<<"Please enter the vehicle registration number of owner : ";
                cin>>reg_no;
                    
                for(int i=0; i<x; i++) 
                { if (reg[i]==reg_no) 
                 {    cout<<"Matching="<<reg_no<<endl;
                     list[i].getDetail();             
                 }
                }
        break;
        case 7: cout<<"Please enter the type of owner vehicle : ";
                cin>>type;
                    
                for(int i=0; i<x; i++) 
                { if (typ[i]==type) 
                 {   cout<<"Matching="<< type<<endl; 
                     list[i].getDetail();             
                 }
                }
        break;
        case 8: cout<<"Please enter the brand of owner vehicle : ";
                cin>>brand;
                    
                for(int i=0; i<x; i++) 
                { if (brd[i]==brand) 
                 {    cout<<"Matching="<< brand<<endl;
                     list[i].getDetail();             
                 }
                }
        break;
        
        default: cout << "Wrong Option....Please Reenter Your Choice: "<<endl;
      }
     
     cout<<endl<<endl;
      cout<<"Do you want to Search any other field (y/n)?"<<endl;
      cin>>redo1;
     }while(redo1=='y'||redo1=='Y');
     
     
     
}

int main()
{
    VIS info ;
    int c;
    char ch;
    
    do
 {
    system("cls");               
    cout<<"\t\tWelcome to Vehicle Information Software ."<<endl;
    cout<<"\t      Please choose one of the following commands :"<<endl<<endl<<endl;
    cout<<"\t\t    1. Add data"<<endl;

    
    cout<<"\tChoice :";
    cin>>c;
    
   system("cls");
   switch(c)
  {
  case 1:  
           cout<<"\nEnter the information of the new owner"<<endl;
           info.addDetail();
            
  break;
  
  case 2: 
           info.searchDetail();
           
  break;
  
  
}
  cout<<"Do you want to continue";
      cin>>ch;
}
while(ch=='y'||ch=='Y');


    system("pause");
    return 0;
}
Last edited on
Yes, you can. It'll be nested classes then. This is called a 'has-a' relationship.

EDIT:

In your case nesting an object array of class Owner in VIS, the VIS 'has-a' relationship with Owner.

Hope that helps
Last edited on
thank you very much
No problem ;)
But I'd still advise you to look up the topic of nested classes in detail.
Topic archived. No new replies allowed.