By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

Design Student Class

P: 2
Hi,

I would like to design a Student Class containing the private members namely Char *Name,int Age and Char Sex.

The constructor of the class need to be passed with only one String argument..

The String passed is in the format like

Student S("David 18 M") each is separated by a single space.
Now the private members of the class need to be filled with corresponding values....

Please let me know how to implement this scenario?

I have used find method of String class to find the index at which space is located but unable to fill it correctly.
Sep 22 '06 #1
Share this Question
Share on Google+
2 Replies


P: 27
Hi

Here is code for your student class:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. class S {
  7.  
  8.   char _name[32];
  9.   int _age;
  10.   char _sex;
  11.  
  12.   public:
  13.  
  14.   S(char* str) {
  15.     stringstream s(str);
  16.     s >> _name >> _age >> _sex;        
  17.   }        
  18.  
  19.   void print(void) const
  20.   {
  21.     cout << "Student name: " << _name << endl;
  22.     cout << "Student age:  " << _age << endl;
  23.     cout << "Student sex:  " << _sex << endl;
  24.   }
  25. };
  26.  
  27. int main(){
  28.  
  29.   S student("David 18 M");    
  30.   student.print();    
  31.   return 0;
  32. }
Sep 22 '06 #2

P: 2
Hi Dush,

Thankyou for the Quick reply.


Hi

Here is code for your student class:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. class S {
  7.  
  8.   char _name[32];
  9.   int _age;
  10.   char _sex;
  11.  
  12.   public:
  13.  
  14.   S(char* str) {
  15.     stringstream s(str);
  16.     s >> _name >> _age >> _sex;        
  17.   }        
  18.  
  19.   void print(void) const
  20.   {
  21.     cout << "Student name: " << _name << endl;
  22.     cout << "Student age:  " << _age << endl;
  23.     cout << "Student sex:  " << _sex << endl;
  24.   }
  25. };
  26.  
  27. int main(){
  28.  
  29.   S student("David 18 M");    
  30.   student.print();    
  31.   return 0;
  32. }
Sep 25 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.