Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE HELP ASAP. For this lab, we are going to use a pointer in a class definit

ID: 3862615 • Letter: P

Question

PLEASE HELP ASAP.

For this lab, we are going to use a pointer in a class definition.

Note: The lab files (Student.h, and lab6.cpp) are available on GL in the directory

After you have copied the files into your lab 6 directory, you should spend some time really looking at the Student.h file.

Use the files that you copied from the class directory as the starting skeleton for your program. It includes comments about what you are going to need to do. You should not need to modify the main function at all.

There are three parts to this lab:

Code Student.cpp. The declaration for the student class is in Student.h. You can see the sample expected output below. You need to implement the constructor, destructor, and Display(). You cannot use a vector in this lab but you can use a dynamically created array.

Write the makefile (instructions are below).

Check for memory leaks using valgrind ./lab6.cpp

Student.h FILE

#ifndef STUDENT_H

#define STUDENT_H

#include <iostream>

#include <string>

using namespace std;

class Student

{

public:

Student();

~Student();

void Display();

private:

int m_num;

float *m_ptr;

};

#endif

lab6.cpp

#include "Student.h"

#include <iostream>

#include <string>

using namespace std;

int main() {

Student s;

s.Display();

return 0;

}

Explanation / Answer

//Student.h

#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
   Student();
   ~Student();
   void Display();
   //add method to set GPA value,added by ChegEA
   void addGPA(float gpa);
private:
   int m_num;
   float *m_ptr;
};

#endif

-----------------------------------------

//Student.cpp

#include "Student.h"

Student::Student()
{
   m_ptr = 0;
   m_num = 0;
}

Student::~Student()
{
   delete m_ptr;
}

void Student::Display()
{
   static int i = 0;
   cout << "Student" << i+1 << ":" << *m_ptr << endl;
   i++;
}

//method to add GPA,added by chegg EA

void Student::addGPA(float gpa)
{
   m_ptr = new float;
   *m_ptr = gpa;
}

-------------------------------------

//lab6.cpp

#include "Student.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
   int n;
   float GPA;
   cout << "Enter total number of students: ";
   cin >> n;
   cout << "Enter GPA of students." << endl;

   //create object of Student type with number of students.Added checgEA
   Student *s;
   s = new Student[n];
   for (int i = 0; i < n; i++)
   {
       cout << "Student" << i + 1 << " : ";
       cin >> GPA;
       s[i].addGPA(GPA);
   }
   cout << "Displaying GPA of students."<<endl;
   for (int i = 0; i < n; i++)
   {
       s[i].Display();
   }
   //delete pointer allocated
   delete[] s;
  
   return 0;
}

-----------------------------------------------------------------------------------------------------------------

//Makefile

Student.o: Student.cpp Student.h
   g++ -c Student.cpp

lab6.o: lab6.cpp Student.h
   g++ -c lab6.cpp

run: Student.o lab6.o
   g++ -o run Student.o lab6.o
  
clean:
   rm *.o run

----------------------------------------------------------------------------------------------------------------

type at command prompt

$make run

$run

gives below output

//output

sh-4.2$ run                                                                                                                                                                     

Enter total number of students: 3                                                                                                                                               

Enter GPA of students.                                                                                                                                                          

Student1 : 4.0                                                                                                                                                                  

Student2 : 3.3                                                                                                                                                                  

Student3 : 3.4                                                                                                                                                                  

Displaying GPA of students.                                                                                                                                                     

Student1:4                                                                                                                                                                      

Student2:3.3                                                                                                                                                                    

Student3:3.4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote