One warning and main() returned garbage value

  associations, c++, c++17, oop, uml

Multiple students can associate with a single Department and single student can
associate with multiple Departments, but there is no ownership between the objects
and both have their own lifecycle. Both can create and delete independently.

WAP in C++ to model the relationships.

I have implemented this code as follows

#include<iostream>
#include<cstring>
using namespace std;

class Student
{
    char* name_p;
public:
    Student(char *sName)
    {
        cout<<"Student constructor calledn";
        name_p=new char(sizeof(strlen(sName)));
        name_p=sName;
    }

    ~Student()
    {
        cout<<"Student destructor calledn";
        delete name_p;
    };

    char* sName()
    {
        return name_p;
    }
};

class Department
{
    char* name_p;
public:
    Department(char *dName)
    {
        cout<<"Department destructor calledn";
        name_p=new char(sizeof(strlen(dName)));
        name_p=dName;
    }

    ~Department()
    {
        cout<<"Department destructor calledn";
        delete name_p;
    }

    char* dName()
    {
        return name_p;
    }
};

class Course
{
    Student* std_p;
    Department* dept_p;
    char* courseName_p;
    static unsigned int index;
    static Course *courseList_p[4];
public:
    Course(char* crseName,Student* student,Department* dept)
    {
        cout<<"Course constructor calledn";
        std_p=student;
        dept_p=dept;

      if(index<4)
      {
            courseName_p=new char(sizeof(strlen(crseName)));
            courseName_p=crseName;

            courseList_p[index]=this;
            ++index;
      }
      else
      {
            cout<<"Cannot accomodate any more Coursen";
      }
    };

    ~Course()
    {
      cout<<"Course destructor calledn";
      delete courseName_p;
    };

    static char* findStudent(char *crseName, char* deptName)
    {
      for(int i=0; i<index; i++)
      {
        if ( (courseList_p[i]->getCourseName() == crseName) &&
             (courseList_p[i]->getDeptName() == deptName) )
        {
          return(courseList_p[i]->getStdName());
        }
      }
    }

    char* getStdName()
    {
        return std_p->sName();
    };
    char* getDeptName()
    {
        return dept_p->dName();
    };
    char* getCourseName()
    {
        return courseName_p;
    };
};

unsigned int Course::index =0;
Course* Course::courseList_p[4]={0,0,0,0};

int main()
{
    int i;

    cout<<"nExample of Association classn";
    cout<<"-----------------------------------nn";

    cout<<"We have got 4 studentsn";
    Student *studentNames[4] = {new Student("Meera"), new Student("Moina"), new Student("Teena"), new Student("Mridula")} ;

    cout<<"n";

    cout<<"We have got 2 Departmentsn";
    Department *departNames[2] = {new Department("Mathematics"), new Department("ComputerSceince")} ;

    cout<<"n";

    cout<<"Here class Course Associates Student and Department, with a Course namen";
    Course course1("DataStructure",studentNames[0], departNames[1]);
    Course course2("Maths",studentNames[3], departNames[0]);
    Course course3("Geometry",studentNames[2], departNames[0]);
    Course course4("CA",studentNames[1], departNames[1]);

    cout<<"n";

    cout<<"Finding a Student using Course and Departmentn";
    cout<<"Student who has taken Maths Course in Mathematics Department is:"<<Course::findStudent("Maths", "Mathematics")<<endl;

    cout<<"n";

    cout<<"Deletion of objectsnn";

    for(i=0;i<4;i++)
    {
        delete studentNames[i];
    }

    cout<<"n";

    for(i=0;i<2;i++)
    {
        delete departNames[i];
    }

    cout<<"n";

    return 0;
}

The code is showing warnings in main function i.e. ISO C++ forbids converting a string constant to char* and also the main() function is not returning 0 but a garbage value. Please help me in rectify these errors and warnings.

Also I don’t want to use this pointer in class course, can I implement the code without using this pointer.

Source: Windows Questions C++

LEAVE A COMMENT