dynamic memory allocation of class objects

This is a discussion on dynamic memory allocation of class objects within the C++ Programming forums, part of the General Programming Boards category; I am not able to add data on run time (dynamic allocation of memory) and then read that data and ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    dynamic memory allocation of class objects

    I am not able to add data on run time (dynamic allocation of memory) and then read that data and print all of the objects of class on screen!

    can some one help please?

    these are the functions i am using to store and display data!

    Code:
    void studentAdd() {
    	stds = new Students;
            stds->Students::addStudent();
            stds++;
    }
    
    void studentShow() {
            int num=0;
    	int total = stds->Students::totalStds();
            for (int i=0;i<total;i++) {
    	stds->Students::showStudent();
    	}
    }

    class member functions are!

    Code:
    void Students::addStudent() {
    
    cout << "\n\n\tStudent ID:\t"<< stdId;
    cout << "\n\tStudent Name:\t";
    cin >> stdName;
    cout << "\tStudent Age:\t";
    cin >> stdAge;
    cout << "\tStudent Class:\t";
    cin >> stdClass;
    cout << "\tTrnspt [1,0]:\t";
    cin >> stdTransport;
    cout << "\tHostel [1,0]:\t";
    cin >> stdHostel;
    stdExists=1;
    
    }
    
    void Students::showStudent() {
    
    cout << "\n\n\tStudent ID:\t"<< stdId;
    cout << "\n\tStudent Name:\t"<< stdName;
    cout << "\n\tStudent Age:\t"<< stdAge;
    cout << "\n\tStudent Class:\t"<< stdClass;
    cout << "\n\tTrnspt [1,0]:\t"<< stdTransport;
    cout << "\n\tHostel [1,0]:\t"<< stdHostel;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    here is the complete code!

    Code:
    class Students {
    private:
    int static countStdId;
    int stdId;
    string stdName;
    int stdAge;
    string stdClass;
    int stdTransport;
    int stdHostel;
    int stdExists;
    
    public:
    	Students();		// constructor declaration
    	~Students();		// destructor declaration
    	void addStudent();
    	void showStudent();
    	int checkExt() { return stdExists; }
    	int totalStds() { return countStdId; }
    };
    
    int Students::countStdId=0;	// static variable
    
    Students * stds;
    //////////////////////////////////////////
    // Constructor function
    //////////////////////////////////////////
    
    Students::Students() {
    	stdId=++countStdId;
    	stdAge=0;
    	stdTransport=0;
    	stdHostel=0;
    	stdExists=0;
    }
    
    //////////////////////////////////////////
    // Destructor
    //////////////////////////////////////////
    Students::~Students() {
    }
    
    //////////////////////////////////////////
    // Students :: Add New Student
    //////////////////////////////////////////
    
    void Students::addStudent() {
    
    cout << "\n\n\tStudent ID:\t"<< stdId;
    cout << "\n\tStudent Name:\t";
    cin >> stdName;
    cout << "\tStudent Age:\t";
    cin >> stdAge;
    cout << "\tStudent Class:\t";
    cin >> stdClass;
    cout << "\tTrnspt [1,0]:\t";
    cin >> stdTransport;
    cout << "\tHostel [1,0]:\t";
    cin >> stdHostel;
    stdExists=1;
    
    }
    
    void Students::showStudent() {
    
    cout << "\n\n\tStudent ID:\t"<< stdId;
    cout << "\n\tStudent Name:\t"<< stdName;
    cout << "\n\tStudent Age:\t"<< stdAge;
    cout << "\n\tStudent Class:\t"<< stdClass;
    cout << "\n\tTrnspt [1,0]:\t"<< stdTransport;
    cout << "\n\tHostel [1,0]:\t"<< stdHostel;
    
    }
    
    
    void studentAdd() {
    //	int num=0;
    //	cout << "\n\n\tEnter number of students you wish to add: ";
    //	cin >> num;
    
    	//int total = stds->Students::totalStds();
    
    	stds = new Students;
            stds->Students::addStudent();
            stds++;
    
    //	for (int i=total-1;i<num;i++) {
    //	stds[i].Students::addStudent();
    //	}
    
    studentMenu(2);		// call students menu with successfully added message
    }
    
    void studentShow() {
    
            int num=0;
    
    	int total = stds->Students::totalStds();
    
            for (int i=0;i<total;i++) {
    	stds->Students::showStudent();
    	}
    
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your conceptual problem appears to be that you want Students both to contain data of one student and work as a container of students.

    Normally you'd make a Student class with all the Student data and methods, and then store them for example in a std::vector<Student>. You can then further define functions that work with a collection of students and accept the vector as input.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    What i am trying to do it generate auto ID (using constructor) and then adding data (on run time) which is saved in memory..

    and then what i want to do it print all of the data to the screen which is there in the memory!
    i am just all confused..

    any further help would be greatly appreciated!

    Regards.

    Here is the code with class..

    Code:
    class Students {
    private:
    int static countStdId;
    int stdId;
    string stdName;
    int stdAge;
    string stdClass;
    int stdTransport;
    int stdHostel;
    int stdExists;
    
    public:
    	Students();		// constructor declaration
    	~Students();		// destructor declaration
    	void addStudent();
    	void showStudent();
    	int checkExt() { return stdExists; }
    	int totalStds() { return countStdId; }
    };
    
    int Students::countStdId=0;	// static variable
    
    Students * stds;
    //////////////////////////////////////////
    // Constructor function
    //////////////////////////////////////////
    
    Students::Students() {
    	stdId=++countStdId;
    	stdAge=0;
    	stdTransport=0;
    	stdHostel=0;
    	stdExists=0;
    }
    
    //////////////////////////////////////////
    // Destructor
    //////////////////////////////////////////
    Students::~Students() {
    }
    
    //////////////////////////////////////////
    // Students :: Add New Student
    //////////////////////////////////////////
    
    void Students::addStudent() {
    
    cout << "\n\n\tStudent ID:\t"<< stdId;
    cout << "\n\tStudent Name:\t";
    cin >> stdName;
    cout << "\tStudent Age:\t";
    cin >> stdAge;
    cout << "\tStudent Class:\t";
    cin >> stdClass;
    cout << "\tTrnspt [1,0]:\t";
    cin >> stdTransport;
    cout << "\tHostel [1,0]:\t";
    cin >> stdHostel;
    stdExists=1;
    
    }
    
    void Students::showStudent() {
    
    cout << "\n\n\tStudent ID:\t"<< stdId;
    cout << "\n\tStudent Name:\t"<< stdName;
    cout << "\n\tStudent Age:\t"<< stdAge;
    cout << "\n\tStudent Class:\t"<< stdClass;
    cout << "\n\tTrnspt [1,0]:\t"<< stdTransport;
    cout << "\n\tHostel [1,0]:\t"<< stdHostel;
    
    }
    
    
    void studentAdd() {
    //	int num=0;
    //	cout << "\n\n\tEnter number of students you wish to add: ";
    //	cin >> num;
    
    	//int total = stds->Students::totalStds();
    
    	stds = new Students;
            stds->addStudent();
            stds++;
    
    //	for (int i=total-1;i<num;i++) {
    //	stds[i].Students::addStudent();
    //	}
    
    studentMenu(2);		// call students menu with successfully added message
    }
    
    void studentShow() {
    
            int num=0;
    	int total = stds->Students::totalStds();
            for (int i=0;i<total;i++) {
    	stds->showStudent();
    	stds++;
    	}
    
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Reread anon's first sentence. That's your problem. A single student and a group of students are two different things. You are trying to use a Students class to model both.

    For example, your Students class has a single member variable called stdName. That means that you can only hold one name in a Students object. If you can only hold one name at a time, then a Students object probably should represent a single student, right? But then, you have functions like AddStudent that are a member of the Students class. If you're adding a student to something, that thing is probably a collection of students. So that's your problem, you're using Students to act like a single student and to act like a collection of students. It can't do both.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    yea its a group.. and i am not willing to store multiple names within a single object;;
    what i am trying to do is 1 student is 1 object so one object will contact data of a single student, second object would contain data of second student and so on...

    i hope it clears!

    any help would be greatly appreciated!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nightrithem View Post
    yea its a group.. and i am not willing to store multiple names within a single object;;
    what i am trying to do is 1 student is 1 object so one object will contact data of a single student, second object would contain data of second student and so on...

    i hope it clears!

    any help would be greatly appreciated!
    Right. This means that, for example, there's no possible valid meaning of a member function "StudentAdd" -- you already have the student object, so there's no way to add something to this student.

    You could conceivably have a function called "StudentAdd" as a member function of a class called "BigPileOfStudents" or something -- but you need to have a separate class that acts as a container.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find Size Of Dynamic Memory Allocation?
    By appleGuy in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 09:34 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  5. memory allocation class
    By Orca in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:43 AM

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