I hot the following program from the book 'C++ For Dummies' but have two questions about it. First I compiled the program but I'm afraid to run because the memery created from the heap does not appear to me to get de-allocated. Am I correct in not wanting to run this and is my reason valid? Secondly, on the line Student* pS = new Student;
is it alway a rule that memory created from the heap must be access via a pointer? I know when I removed the *
I get the following error from g++. constructor.cpp:30:22: error: conversion from ‘Student*’ to non-scalar type ‘Student’ requested -> Student pS = new Student;
// constructor - example that invokes a constructor
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
cout << "constructing student" << endl;
semesterHours = 0;
gpa = 0.0;
}
// ...other public memebers...
protected:
int semesterHours;
double gpa;
};
int main()
{
cout << "Creating a new Student object" << endl;
Student s;
cout << "Creating a new object off the heap" << endl;
Student* pS = new Student;
}