How to Create & Add new object in existing array using Dynamic Memory allocation












0















I am trying to do some stuff with C++ and i am new in it :)



I have tried 1 program of class that gets the student details and print the output of it.



#include <iostream>
using namespace std;

#define MAX 10

class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};

//member function definition, outside of the class
void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
perc=(float)total/500*100;
}

//member function definition, outside of the class
void student::putDetails(void) {
cout << "Student details:n";
cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc;
}

int main()
{
student std[MAX]; //array of objects creation
int n,loop;

cout << "Enter total number of students: ";
cin >> n;

for(loop=0;loop< n; loop++){
cout << "Enter details of student " << loop+1 << ":n";
std[loop].getDetails();
}

cout << endl;

for(loop=0;loop< n; loop++) {
cout << "Details of student " << (loop+1) << ":n";
std[loop].putDetails();
}

return 0;
}


Its very basic code and works fine and I am able to give inputs and print the output.



Now I want to add new Student object at runtime using Dynamic memory allocation and want to add that object in the existing array of object (So that I can get the highest, lowest marks of any student)




I know I need to use new operator for this.




But I am not sure what could be the best way to write this solution.



Any help will be highly appreciated.



Thanks!










share|improve this question


















  • 1





    You usually don't use new or delete operations manually in your code with c++.. Also such declarations as raw student std[MAX]; arrays aren't used in production code. You have standard container classes and smart pointers at hand to deal with dynamic memory allocation, Just use these,

    – πάντα ῥεῖ
    Jan 20 at 12:00











  • I am not looking for production at the moment. The code that i have attached is kind of 1 program, and my another task is to modify the above program and add dynamic memory allocation in it.

    – Jayesh Dhandha
    Jan 20 at 12:02






  • 1





    "another task is to modify the above program and add dynamic memory allocation in it" The essence of my comment is that you shouldn't do that yourself, but simply use the standard c++ facilities which do that in 1st place. Using new or delete in your own code is the really really last resort to solve very special and advanced problems.

    – πάντα ῥεῖ
    Jan 20 at 12:20






  • 1





    student std[MAX]; Don't do that! Use a std::vector<student> std; instead. There's no need to use raw arrays in c++.

    – πάντα ῥεῖ
    Jan 20 at 12:37






  • 1





    I know I need to use new operator for this -- Wrong. There is no need for new to accomplish this. Then this: char name[30]; -- Buffer overflow if I input more than 30 characters, use std::string instead. Then of course, this one that so many new programmers get wrong: student std[MAX]; -- Invalid C++. Overall, it seems as if you're not learning C++, but actually C with a few C++ syntax thrown in.

    – PaulMcKenzie
    Jan 20 at 15:42


















0















I am trying to do some stuff with C++ and i am new in it :)



I have tried 1 program of class that gets the student details and print the output of it.



#include <iostream>
using namespace std;

#define MAX 10

class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};

//member function definition, outside of the class
void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
perc=(float)total/500*100;
}

//member function definition, outside of the class
void student::putDetails(void) {
cout << "Student details:n";
cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc;
}

int main()
{
student std[MAX]; //array of objects creation
int n,loop;

cout << "Enter total number of students: ";
cin >> n;

for(loop=0;loop< n; loop++){
cout << "Enter details of student " << loop+1 << ":n";
std[loop].getDetails();
}

cout << endl;

for(loop=0;loop< n; loop++) {
cout << "Details of student " << (loop+1) << ":n";
std[loop].putDetails();
}

return 0;
}


Its very basic code and works fine and I am able to give inputs and print the output.



Now I want to add new Student object at runtime using Dynamic memory allocation and want to add that object in the existing array of object (So that I can get the highest, lowest marks of any student)




I know I need to use new operator for this.




But I am not sure what could be the best way to write this solution.



Any help will be highly appreciated.



Thanks!










share|improve this question


















  • 1





    You usually don't use new or delete operations manually in your code with c++.. Also such declarations as raw student std[MAX]; arrays aren't used in production code. You have standard container classes and smart pointers at hand to deal with dynamic memory allocation, Just use these,

    – πάντα ῥεῖ
    Jan 20 at 12:00











  • I am not looking for production at the moment. The code that i have attached is kind of 1 program, and my another task is to modify the above program and add dynamic memory allocation in it.

    – Jayesh Dhandha
    Jan 20 at 12:02






  • 1





    "another task is to modify the above program and add dynamic memory allocation in it" The essence of my comment is that you shouldn't do that yourself, but simply use the standard c++ facilities which do that in 1st place. Using new or delete in your own code is the really really last resort to solve very special and advanced problems.

    – πάντα ῥεῖ
    Jan 20 at 12:20






  • 1





    student std[MAX]; Don't do that! Use a std::vector<student> std; instead. There's no need to use raw arrays in c++.

    – πάντα ῥεῖ
    Jan 20 at 12:37






  • 1





    I know I need to use new operator for this -- Wrong. There is no need for new to accomplish this. Then this: char name[30]; -- Buffer overflow if I input more than 30 characters, use std::string instead. Then of course, this one that so many new programmers get wrong: student std[MAX]; -- Invalid C++. Overall, it seems as if you're not learning C++, but actually C with a few C++ syntax thrown in.

    – PaulMcKenzie
    Jan 20 at 15:42
















0












0








0








I am trying to do some stuff with C++ and i am new in it :)



I have tried 1 program of class that gets the student details and print the output of it.



#include <iostream>
using namespace std;

#define MAX 10

class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};

//member function definition, outside of the class
void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
perc=(float)total/500*100;
}

//member function definition, outside of the class
void student::putDetails(void) {
cout << "Student details:n";
cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc;
}

int main()
{
student std[MAX]; //array of objects creation
int n,loop;

cout << "Enter total number of students: ";
cin >> n;

for(loop=0;loop< n; loop++){
cout << "Enter details of student " << loop+1 << ":n";
std[loop].getDetails();
}

cout << endl;

for(loop=0;loop< n; loop++) {
cout << "Details of student " << (loop+1) << ":n";
std[loop].putDetails();
}

return 0;
}


Its very basic code and works fine and I am able to give inputs and print the output.



Now I want to add new Student object at runtime using Dynamic memory allocation and want to add that object in the existing array of object (So that I can get the highest, lowest marks of any student)




I know I need to use new operator for this.




But I am not sure what could be the best way to write this solution.



Any help will be highly appreciated.



Thanks!










share|improve this question














I am trying to do some stuff with C++ and i am new in it :)



I have tried 1 program of class that gets the student details and print the output of it.



#include <iostream>
using namespace std;

#define MAX 10

class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};

//member function definition, outside of the class
void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
perc=(float)total/500*100;
}

//member function definition, outside of the class
void student::putDetails(void) {
cout << "Student details:n";
cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc;
}

int main()
{
student std[MAX]; //array of objects creation
int n,loop;

cout << "Enter total number of students: ";
cin >> n;

for(loop=0;loop< n; loop++){
cout << "Enter details of student " << loop+1 << ":n";
std[loop].getDetails();
}

cout << endl;

for(loop=0;loop< n; loop++) {
cout << "Details of student " << (loop+1) << ":n";
std[loop].putDetails();
}

return 0;
}


Its very basic code and works fine and I am able to give inputs and print the output.



Now I want to add new Student object at runtime using Dynamic memory allocation and want to add that object in the existing array of object (So that I can get the highest, lowest marks of any student)




I know I need to use new operator for this.




But I am not sure what could be the best way to write this solution.



Any help will be highly appreciated.



Thanks!







c++ new-operator dynamic-memory-allocation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 11:52









Jayesh DhandhaJayesh Dhandha

590517




590517








  • 1





    You usually don't use new or delete operations manually in your code with c++.. Also such declarations as raw student std[MAX]; arrays aren't used in production code. You have standard container classes and smart pointers at hand to deal with dynamic memory allocation, Just use these,

    – πάντα ῥεῖ
    Jan 20 at 12:00











  • I am not looking for production at the moment. The code that i have attached is kind of 1 program, and my another task is to modify the above program and add dynamic memory allocation in it.

    – Jayesh Dhandha
    Jan 20 at 12:02






  • 1





    "another task is to modify the above program and add dynamic memory allocation in it" The essence of my comment is that you shouldn't do that yourself, but simply use the standard c++ facilities which do that in 1st place. Using new or delete in your own code is the really really last resort to solve very special and advanced problems.

    – πάντα ῥεῖ
    Jan 20 at 12:20






  • 1





    student std[MAX]; Don't do that! Use a std::vector<student> std; instead. There's no need to use raw arrays in c++.

    – πάντα ῥεῖ
    Jan 20 at 12:37






  • 1





    I know I need to use new operator for this -- Wrong. There is no need for new to accomplish this. Then this: char name[30]; -- Buffer overflow if I input more than 30 characters, use std::string instead. Then of course, this one that so many new programmers get wrong: student std[MAX]; -- Invalid C++. Overall, it seems as if you're not learning C++, but actually C with a few C++ syntax thrown in.

    – PaulMcKenzie
    Jan 20 at 15:42
















  • 1





    You usually don't use new or delete operations manually in your code with c++.. Also such declarations as raw student std[MAX]; arrays aren't used in production code. You have standard container classes and smart pointers at hand to deal with dynamic memory allocation, Just use these,

    – πάντα ῥεῖ
    Jan 20 at 12:00











  • I am not looking for production at the moment. The code that i have attached is kind of 1 program, and my another task is to modify the above program and add dynamic memory allocation in it.

    – Jayesh Dhandha
    Jan 20 at 12:02






  • 1





    "another task is to modify the above program and add dynamic memory allocation in it" The essence of my comment is that you shouldn't do that yourself, but simply use the standard c++ facilities which do that in 1st place. Using new or delete in your own code is the really really last resort to solve very special and advanced problems.

    – πάντα ῥεῖ
    Jan 20 at 12:20






  • 1





    student std[MAX]; Don't do that! Use a std::vector<student> std; instead. There's no need to use raw arrays in c++.

    – πάντα ῥεῖ
    Jan 20 at 12:37






  • 1





    I know I need to use new operator for this -- Wrong. There is no need for new to accomplish this. Then this: char name[30]; -- Buffer overflow if I input more than 30 characters, use std::string instead. Then of course, this one that so many new programmers get wrong: student std[MAX]; -- Invalid C++. Overall, it seems as if you're not learning C++, but actually C with a few C++ syntax thrown in.

    – PaulMcKenzie
    Jan 20 at 15:42










1




1





You usually don't use new or delete operations manually in your code with c++.. Also such declarations as raw student std[MAX]; arrays aren't used in production code. You have standard container classes and smart pointers at hand to deal with dynamic memory allocation, Just use these,

– πάντα ῥεῖ
Jan 20 at 12:00





You usually don't use new or delete operations manually in your code with c++.. Also such declarations as raw student std[MAX]; arrays aren't used in production code. You have standard container classes and smart pointers at hand to deal with dynamic memory allocation, Just use these,

– πάντα ῥεῖ
Jan 20 at 12:00













I am not looking for production at the moment. The code that i have attached is kind of 1 program, and my another task is to modify the above program and add dynamic memory allocation in it.

– Jayesh Dhandha
Jan 20 at 12:02





I am not looking for production at the moment. The code that i have attached is kind of 1 program, and my another task is to modify the above program and add dynamic memory allocation in it.

– Jayesh Dhandha
Jan 20 at 12:02




1




1





"another task is to modify the above program and add dynamic memory allocation in it" The essence of my comment is that you shouldn't do that yourself, but simply use the standard c++ facilities which do that in 1st place. Using new or delete in your own code is the really really last resort to solve very special and advanced problems.

– πάντα ῥεῖ
Jan 20 at 12:20





"another task is to modify the above program and add dynamic memory allocation in it" The essence of my comment is that you shouldn't do that yourself, but simply use the standard c++ facilities which do that in 1st place. Using new or delete in your own code is the really really last resort to solve very special and advanced problems.

– πάντα ῥεῖ
Jan 20 at 12:20




1




1





student std[MAX]; Don't do that! Use a std::vector<student> std; instead. There's no need to use raw arrays in c++.

– πάντα ῥεῖ
Jan 20 at 12:37





student std[MAX]; Don't do that! Use a std::vector<student> std; instead. There's no need to use raw arrays in c++.

– πάντα ῥεῖ
Jan 20 at 12:37




1




1





I know I need to use new operator for this -- Wrong. There is no need for new to accomplish this. Then this: char name[30]; -- Buffer overflow if I input more than 30 characters, use std::string instead. Then of course, this one that so many new programmers get wrong: student std[MAX]; -- Invalid C++. Overall, it seems as if you're not learning C++, but actually C with a few C++ syntax thrown in.

– PaulMcKenzie
Jan 20 at 15:42







I know I need to use new operator for this -- Wrong. There is no need for new to accomplish this. Then this: char name[30]; -- Buffer overflow if I input more than 30 characters, use std::string instead. Then of course, this one that so many new programmers get wrong: student std[MAX]; -- Invalid C++. Overall, it seems as if you're not learning C++, but actually C with a few C++ syntax thrown in.

– PaulMcKenzie
Jan 20 at 15:42














1 Answer
1






active

oldest

votes


















1














IMO, The best way to do this using dynamic memory is by using std::unique_ptr or std::shared_ptr (it actually depends on the requirement).



Here is one example of usage of unique_ptr:



using StudentPtr = std::unique_ptr<student>;

int main() {
std::vector<StudentPtr> studentDetails;
int n;

cout << "Enter the number of students: ";
cin >> n;

studentDetails.resize(n);

for (auto &s: studentDetails) {
s = StudentPtr(new student);
s->getDetails();
}

return 0;
}


For getting minimum and maximum, you may use min_element and max_element provided by STL respectively.






share|improve this answer


























  • @JayeshDhandha If you feel this answered your question, consider voting and accepting stackoverflow.com/help/someone-answers

    – Kunal Puri
    Jan 22 at 0:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276142%2fhow-to-create-add-new-object-in-existing-array-using-dynamic-memory-allocation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














IMO, The best way to do this using dynamic memory is by using std::unique_ptr or std::shared_ptr (it actually depends on the requirement).



Here is one example of usage of unique_ptr:



using StudentPtr = std::unique_ptr<student>;

int main() {
std::vector<StudentPtr> studentDetails;
int n;

cout << "Enter the number of students: ";
cin >> n;

studentDetails.resize(n);

for (auto &s: studentDetails) {
s = StudentPtr(new student);
s->getDetails();
}

return 0;
}


For getting minimum and maximum, you may use min_element and max_element provided by STL respectively.






share|improve this answer


























  • @JayeshDhandha If you feel this answered your question, consider voting and accepting stackoverflow.com/help/someone-answers

    – Kunal Puri
    Jan 22 at 0:43
















1














IMO, The best way to do this using dynamic memory is by using std::unique_ptr or std::shared_ptr (it actually depends on the requirement).



Here is one example of usage of unique_ptr:



using StudentPtr = std::unique_ptr<student>;

int main() {
std::vector<StudentPtr> studentDetails;
int n;

cout << "Enter the number of students: ";
cin >> n;

studentDetails.resize(n);

for (auto &s: studentDetails) {
s = StudentPtr(new student);
s->getDetails();
}

return 0;
}


For getting minimum and maximum, you may use min_element and max_element provided by STL respectively.






share|improve this answer


























  • @JayeshDhandha If you feel this answered your question, consider voting and accepting stackoverflow.com/help/someone-answers

    – Kunal Puri
    Jan 22 at 0:43














1












1








1







IMO, The best way to do this using dynamic memory is by using std::unique_ptr or std::shared_ptr (it actually depends on the requirement).



Here is one example of usage of unique_ptr:



using StudentPtr = std::unique_ptr<student>;

int main() {
std::vector<StudentPtr> studentDetails;
int n;

cout << "Enter the number of students: ";
cin >> n;

studentDetails.resize(n);

for (auto &s: studentDetails) {
s = StudentPtr(new student);
s->getDetails();
}

return 0;
}


For getting minimum and maximum, you may use min_element and max_element provided by STL respectively.






share|improve this answer















IMO, The best way to do this using dynamic memory is by using std::unique_ptr or std::shared_ptr (it actually depends on the requirement).



Here is one example of usage of unique_ptr:



using StudentPtr = std::unique_ptr<student>;

int main() {
std::vector<StudentPtr> studentDetails;
int n;

cout << "Enter the number of students: ";
cin >> n;

studentDetails.resize(n);

for (auto &s: studentDetails) {
s = StudentPtr(new student);
s->getDetails();
}

return 0;
}


For getting minimum and maximum, you may use min_element and max_element provided by STL respectively.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 20 at 12:20

























answered Jan 20 at 12:13









Kunal PuriKunal Puri

2,6151519




2,6151519













  • @JayeshDhandha If you feel this answered your question, consider voting and accepting stackoverflow.com/help/someone-answers

    – Kunal Puri
    Jan 22 at 0:43



















  • @JayeshDhandha If you feel this answered your question, consider voting and accepting stackoverflow.com/help/someone-answers

    – Kunal Puri
    Jan 22 at 0:43

















@JayeshDhandha If you feel this answered your question, consider voting and accepting stackoverflow.com/help/someone-answers

– Kunal Puri
Jan 22 at 0:43





@JayeshDhandha If you feel this answered your question, consider voting and accepting stackoverflow.com/help/someone-answers

– Kunal Puri
Jan 22 at 0:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276142%2fhow-to-create-add-new-object-in-existing-array-using-dynamic-memory-allocation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Callistus III

Ostreoida

Index Sanctorum