object creation

This is a discussion on object creation within the C++ Programming forums, part of the General Programming Boards category; this is the code for program in which using public static data member the information of 4 students is entered, ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    object creation

    this is the code for program in which using public static data member the information of 4 students is entered, means i have created 4 objects of class student. my program works but i want to modify my program in such a way i dont want to declare 4 objects separately kindly guide me, is there any way that i declare my object as array of objects so i reduce my code and not declaring objects in a line.
    Code:
    # include <iostream>
    # include <conio.h>
    using namespace std;
    
    class student {
    
    private:
    	
    	char name[50];
    	char blood_group[15];
    public:
    	
    
    	
    	
    	void get_info ()
    	{
    		
    		cout<<"enter name of student:   ";
    		cin.getline (name,50);
    		cout<<"enter blood group of student:  ";
    		cin.getline (blood_group,15);
    
    	
    	}//end function
    	void show_info ()
    	{
    		cout<<"name is:  "<<name<<endl;
    		cout<<"blood group is: "<<blood_group<<endl;
    		cout<<"roll number is: "<<++roll_num<<endl;
    	
    	}//end function
    };//end class
    int student::roll_num=0;
    int main ()
    {
    	
    	student s1,s2,s3,s4;
    	s1.get_info ();
            s2.get_info();
            s3.get_info();
            s4.get_info();
    	s1.show_info ();
            s2.show_info();
            s3.show_info();
            s4.show_info();
    	
    	getch ();
    
    
    }//end main

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Yes, you could use an array. Why don't you give it a try as it apparently only requires a small change to your program?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    i have tried but its not working
    i think i dont know how to declare that because i m trying like
    Code:
    static int size=4;
    	student s[size];
    	for (int i=1;i<=4;i++){
    	s[i].get_info ();
    	}
    	for (int i=1;i<=4;i++){
    	s[i].show_info ();
    	}
    	getch ();

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by rafay_07
    i have tried but its not working
    i think i dont know how to declare that because i m trying like
    Yeah. The first problem is that you did not declare size to be const. The next problem is that you started looping from 1 instead of 0, thus you missed the first element of the array and accessed one past the end of the array, which is wrong. Here's what you might do:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class student {
    public:
        void get_info()
        {
            cout << "enter name of student: ";
            cin.getline(name, 50);
            cout << "enter blood group of student: ";
            cin.getline(blood_group, 15);
        }
    
        void show_info()
        {
            cout << "name is: " << name << endl;
            cout << "blood group is: " << blood_group << endl;
            cout << "roll number is: " << ++roll_num << endl;
        }
    private:
    	char name[50];
    	char blood_group[15];
    
        static int roll_num;
    };
    
    int student::roll_num = 0;
    
    int main()
    {
        const int size = 4;
        student students[size];
    
        for (int i = 0; i < size; ++i)
        {
            students[i].get_info();
        }
    
        for (int i = 0; i < size; ++i)
        {
            students[i].show_info();
        }
    }
    Notice that in addition to the changes I mentioned, I also declared roll_num as a static member variable, in addition to defining it.

    That said, I would not design the class in this way: roll_number is not really something that has to do with students, and then you should be providing ways to access the elements of the student object in useful and flexible ways, not just provide two member functions that do things that are so specific.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    actually i was trying to implement static data member, in my same previous program i declared roll_num in private now i was just seeking that what if i declare roll_num in public.
    kindly guide me in three things
    1. if i declare some variable in public access specifier then this variable is a data member or not?
    2. i have only used roll number as a static data member what other features of class student can be declared as static data member?
    3. u said that "then you should be providing ways to access the elements of the student object in useful and flexible ways, not just provide two member functions that do things that are so specific."
    respectfully i did not get u actually

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by rafay_07
    1. if i declare some variable in public access specifier then this variable is a data member or not?
    Yes, it is still a data member.

    Quote Originally Posted by rafay_07
    2. i have only used roll number as a static data member what other features of class student can be declared as static data member?
    As I stated earlier, I don't think roll number should be a static member variable. It should be a counter used in the code that uses the student objects, not part of the student class.

    Quote Originally Posted by rafay_07
    3. u said that "then you should be providing ways to access the elements of the student object in useful and flexible ways, not just provide two member functions that do things that are so specific."
    respectfully i did not get u actually
    For example, I might write the class like this:
    Code:
    class Student {
    public:
        Student() {}
    
        Student(const std::string& name_, const std::string& blood_group_)
            : name(name_), blood_group(blood_group_) {}
    
        std::string getName() const
        {
            return name;
        }
    
        std::string getBloodGroup() const
        {
            return blood_group;
        }
    
        void setName(const std::string& name_)
        {
            name = name_;
        }
    
        void setBloodGroup(const std::string& blood_group_)
        {
            blood_group = blood_group_;
        }
    private:
        std::string name;
        std::string blood_group;
    };
    In the above code, the Student class with the getters and setters is not much better than:
    Code:
    struct Student
    {
        std::string name;
        std::string blood_group;
    };
    But the difference between your code is that there is a more general interface, i.e., to access the class members through the getters and setters. You could be a little smarter, for example, and make setBloodGroup perform some input checking.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Dynamic Object Creation (and Management)...
    By Comrade_Yeti in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2005, 01:44 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21