- This is student.hpp (Base class)
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
class student
{
public:
student(const std::string& name, const std::string& ssn);
virtual ~student();
virtual void identify();
protected:
// declaring it protected so that i can use it for derived class
std::string studName;
std::string studSSN;
};
student::student(const std::string& name, const std::string& ssn)
: studName(name), studSSN(ssn)
{
std::cout << "Parent Constructor" << std::endl;
}
// destructor for the class
student::~student()
{
std::cout << "Parent Destructor" << std::endl;
}
// displays the information about the student
void student::identify()
{
std::cout << "Name: " << studName << std::endl;
std::cout << "SSN: " << studSSN << std::endl;
}
#endif
- This is studentAthlete.hpp (derived class)
#ifndef STUDENTATHLETE_H
#define STUDENTATHLETE_H
#include <iostream>
#include <string>
#include "student.hpp"
class studentAthlete : public student
{
public:
studentAthlete(const std::string& name, const std::string& ssn, const std::string& game);
~studentAthlete();
void identify();
private:
std::string sports;
};
studentAthlete::studentAthlete(const std::string& name, const std::string& ssn, const std::string& game)
: student(name,ssn),sports(game)
{
std::cout << "Child Constructor" << std::endl;
}
studentAthlete::~studentAthlete()
{
std::cout << "Child Destructor" << std::endl;
}
void studentAthlete::identify()
{
student::identify();
std::cout << "Sports: " << sports << std::endl;
std::cout << "Student is an athlete." << std::endl;
}
#endif
This is the main class
#include <iostream>
#include <string>
#include "student.hpp"
#include "studentAthlete.hpp"
int main()
{
student ja("John Anderson", "345-12-3547");
student bw("Bill Williams", "286-72-6194");
studentAthlete bj("Bob Johnson", "294-87-6295", "football");
studentAthlete dr("Dick Robinson", "669-28-9296", "baseball");
// list of student pointers
student* stud[] = {&ja, &bw, &bj, &dr};
int stud_size = (int) sizeof(stud);
for (int i = 0; i < stud_size; i++)
{
stud[i]->identify();
std::cout << std::endl << std::endl;
}
return EXIT_SUCCESS;
}
I tried using the delete function for dislocating pointer or memory leaks. I don't know how to get rid of the error or to use a destructor.
I am having segmentation fault zsh: segmentation fault ./"main" Any help destructor or removing segmentation fault would be appreciated. Thank you.