1. Programming Assignment For this lab, we are going to use a pointer in a class
ID: 3862308 • Letter: 1
Question
1. Programming Assignment
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:
Student.h
#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;
}
After you have copied the files into your lab 6 directory, you should spend some time really looking at the Student.h file.
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
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.
1.1. Compiling and Running
For this lab, we are going to use a makefile to help expedite the compilation process. We will continue to use makefiles to help practice building them and learning additional features in a makefile.
To create a makefile, go into your lab6 directory and create a new makefile.
-bash-4.1$ emacs makefile
Once you are editing your makefile, you will want to enter the following commands.
CXX = g++
CXXFLAGS = -Wall
lab6: Student.o
$(CXX) $(CXXFLAGS) Student.o lab6.cpp -o lab6
Student.o: Student.cpp Student.h
$(CXX) $(CXXFLAGS) -c Student.cpp
clean:
rm *.o
rm *~
run:
./lab6
When you want to run the makefile, just type “make.”
-bash-4.1$ make
g++ -Wall -c Student.cpp
g++ -Wall Student.o lab6.cpp -o lab6
-bash-4.1$
Now if you notice, there are two additional macros included in your makefile. The first is “make clean”. It will go through and remove your .o files and your temp files with a ~. To run it, you just type make clean.
-bash-4.1$ make clean
rm *.o
rm *~
rm: cannot remove `*~': No such file or directory
make: *** [clean] Error 1
-bash-4.1$
You’ll notice that we didn’t have any files that ended with a ~ so it just warned us that was the case. It doesn’t hurt anything.
WARNING: Be very careful using a makefile and any rm commands. If you mess up the rm command, you can remove all files. BACK UP YOUR FILES FREQUENTLY!
The second is so you don’t have to remember what your current output file is named. You can just run the compile executable by typing make run.
At the Linux prompt, enter the command ./lab6 to run your program. It should look like the sample output provided below:
-bash-4.1$ ./lab6
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
-bash-4.1$
Don’t forget to test valgrind before turning in the lab. If there is a leak, you need to work on the destructor to remove the dynamically allocated memory. As a reminder, you do not call the destructor explicitly! To do this, you can just type valgrind ./lab6.
-bash-4.1$ emacs makefile
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;
}
-------------------------------------------------------------------------------------------------
//output
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.