string initialisation

Feb 23, 2013 at 6:00am
Hey this is my class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class boys
{
      string 2bac[50][2];
      string 4bac[100][4];
      string 6bac[80][6];
      string 2bnac[50][2];
      string 4bnac[100][4];
      string 6bnac[80][6];
public:
      boys(){};
      boys(&boys); 
      void getdetails();
      void addblocksboys(int x)
      {boys *p;
      p=new int[x];
      cout<<"New blocks have been constructed";
      }
      friend void checkavailboys(boys);
};


Nw I want to create an array of class boys in mainfunction and intialize all the strings to a ""( blank space)
so is the following code correct if not please tell me the correct one...

1
2
int main()
{boys a[6]={""};}


Thank You
Feb 23, 2013 at 7:52am
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
 class boys
{
    // identifier names may not begin with numerals.

      string 2bac[50][2];       // 100 strings
      string 4bac[100][4];      // 400 strings
      string 6bac[80][6];       // 480 strings
      string 2bnac[50][2];      // 100 strings
      string 4bnac[100][4];     // 400 strings
      string 6bnac[80][6];      // 480 strings
public:
      boys(){};
      boys(const boys&)        // was: boys(&boys); 
      void getdetails();       // looks like it should return something other than void.

      void addblocksboys(int x) // leaks memory every time it is called.
      {boys *p;
      p=new int[x];
      cout<<"New blocks have been constructed";
      }
      friend void checkavailboys(boys)
};

 // Did you really intend for one object to hold 1960 strings? 

#include <string>
#include <iostream>

int main()
{
    boys b[6] ;        // Hey, look! 11760 strings!

    // all strings contained in the object at  b[i] where i is a value from 0 to 5 will
    // compare equal to the string literal "".
}


Feb 24, 2013 at 4:36am
Hey cire thank you but yes actually my project is about hostel management so.... what I am doing is that I am considering one object of class as one block
..where one block contains 6 types of rooms....
where each room contains the number of studs I have mentioned in the name of that data member...
Now,
I want to store the name of each student in the memory I have allocated ....
but for that initially the variables(array of variables) should not contain anything... so please help me how can I do it... Do I need to use a for loop or something else....
1
2
3
4
5
 void addblocksboys(int &x) //Is this modification okk for  what you have suggested..
      {boys *p;
      p=new int[x];
      cout<<"New blocks have been constructed";
      }


Feb 24, 2013 at 5:19am
I think your description lacks a little clarity.

First, addblocksboys is absolutely wrong for any purpose, and you shouldn't attempt to define it before your design is more complete.

I think I understand that you're attempting to model rooms in a hostel. You want to keep track of occupants of those rooms, and you want to be able to process the rooms in groups called blocks. There are at least 6 different types of rooms.

How many rooms does a block consist of? You say it consists of 6 different types of rooms, so I'm guessing a block consists of at least 6 rooms and possibly more. How many occupants does a room have?

How do the variables you called 2bac, 4bac, etc. fit into this scheme? What does the class boys represent? The name suggests that it isn't a room, or a block, or.. well, anything you've said you're trying to keep track of, really.
Last edited on Feb 24, 2013 at 5:20am
Feb 24, 2013 at 5:31am
okkk well let me explain more clearly ... I am creating hostel blocks for boys and girls...
so the class boys represent boys' hostel and girls represent girls' hostel

Okk Each block contains 6 types of rooms.... where in I have specified the no of those rooms in the class definition...eg

string 2bac[50][2]; mean 2bed ac room having 50 rows(50 rooms) and 2 columns(space for storing name of 2 occupants).... Here is the place where I want to initialize the variable as ""...
(I have changed the identifier's name)...
Well and about addblockboys function... I understand your concern and I will redefine it thanks...
But could you please help me with initialising
all the rows and columns of all the data members of a class array with ""??
Thanks
Feb 24, 2013 at 5:57am
So the class boys should probably be named block.

But could you please help me with initialising all the rows and columns of all the data members of a class array with ""??


They already are. The default string constructor creates an empty string which is equivalent to "".
Feb 24, 2013 at 6:12am
Okkk thanks a lot dude!! u cleared my doubt!!
Topic archived. No new replies allowed.