0
  1. 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
  1. 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.

frickie
  • 1
  • 1
  • 1
    This has nothing to do with destructors and you don't need `delete` anywhere in your code. `sizeof` does not produce the number of elements of an array. Use `std::size` to get the number of elements: `int stud_size = std::size(stud);` or avoid haing to calculate the size at all by using a range-for loop: `for (auto s : stud) { s->identify(); /*[...]*/ }` – user17732522 May 08 '22 at 21:15
  • `sizeof(stud)` return size of array in bytes. – Swift - Friday Pie May 08 '22 at 21:15
  • 1
    Does this answer your question? [What does sizeof do?](https://stackoverflow.com/questions/3203162/what-does-sizeof-do) and [How do I find the length of an array?](https://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array) – user17732522 May 08 '22 at 21:16
  • 1
    `int stud_size = (int) sizeof(stud);` is not the number of elements in the array, it is the size of the array, or the size of 4 pointers. You could use `int stud_size = sizeof(stud) / sizeof(*stud);`, but a more C++ way would be `std::size(stud);`. – Retired Ninja May 08 '22 at 21:16
  • @user17732522 your answer worked for using a range-based for loop. `for (auto s : stud) { s->identify(); }` And I think `std::size(stud)` does not work. Thank you. And also thank you to @Retired_Ninja. – frickie May 08 '22 at 21:26
  • `std::size` works as well but requires at least C++17. – user17732522 May 08 '22 at 21:57

0 Answers0