Question
c++ Array of Pointers: declare ST[5] - an array of pointers to student objects, use a for-loop to make the array point to 5 new
c++
Array of Pointers: declare ST[5] - an array of pointers to student objects, use a for-loop to make the array point to 5 new objects in the heap, use a for-loop to set the last name of each object to what the user enters, use a for-loop to get and display the name of each student. (in the client.cpp)
___________________________
student.h
#include
using namespace std;
class student
{
private:
// Data Members making up each student object (hidden from the client)
string student_name;
string student_major;
int student_grade_level;
public:
student(); // constructor
string getname();
void setname(string);
string getmajor();
void setmajor(string);
~student(); // destructor
};
____________________________________________
srudent.cpp
#include "student.h"
#include
using namespace std;
// note that student.h includes
student::student(){};
student::~student(){};
void student::setname(string s)
{
student_name = s;
}
string student::getname()
{
return student_name;
}
void student::setmajor(string s)
{
student_major = s;
}
string student::getmajor()
{
return student_major;
}
__________________________________
client.cpp
#include
#include"student.h"
using namespace std;
int main()
{
student *ST[5];
// ** declare ST[5] - an array of pointers to student objects
for (int i=0;i<5;i++)
{
ST[i]=new student;
}
// ** use a for-loop to make the array point to 5 new objects in the heap
for(int i=0;i<5;i++)
string LN;
int num =i;
cout<<"Enter the lastname of the student: "< cin>>LN; ST[i]->setname(LN); // ** use a for-loop to set the last name of each object to what the user enters // ** use a for-loop to get and display the name of each student }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started