Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Howtocreateanarraryofclassobjects

I am following an online C++ programming course by a company called edX but I am having trouble with one of the exercises. I have tried to explain the problem on here before but without much luck so I thought it may be best if I just post the exercise on here in edX's own words to see if one of you good people can work out what they want.

"In this assignment you need to create a class file for:

  1. A Student
  2. A Teacher
  3. A Course

The Course object will contain an array of Student objects so ensure that you create an array inside the Course object to hold students. For this assignment create an array of size 3.

The Student class needs to have private member variables for first and last name, age, address, city and phone along with public accessors for these.

Each class will have a default constructor and one that sets the values of the member variables when the object is created. Each class should also have a destructor.

Ensure you are using a header (.h) and an implementation (.cpp) file for each class.

In the main () method:

  1. Instantiate three Student objects called Student1, Student2 and student3, provide values for the member variables.

  2. Instantiate a Course object called "Intermediate C++". (This is from me. Is that even a legal name ?)

  3. Add your three students to this course object.

  4. Using cout statements where appropriate output some of the member variables from Student1."

Below is the code I have written for a Student heder, Student implementation and partial main() function.

Student.h

#pragma once
#include <string>

using namespace std;

class Student
{
private:
    string FirstName;
    string LastName;
    int Age;
    string Address;
    string City;
    string Phone;

public:
    // Declare default constructor
    Student();
    // Declare the real constructor
    Student(string FirstName, string LastName, int Age, string Address, string City, string Phone);
    // Declare a destructor
    ~Student();
    // Declare public accessor functions
    string GetFirstName();
    string GetLastName();
    int GetAge();
    string GetAddress();
    string GetCity();
    string GetPhone();
};

Student.cpp

#include "Student.h"

Student::Student() {}

Student::Student(string _FirstName, string _LastName, int _Age, string _Address, string _City, string _Phone)
{
    this->FirstName = _FirstName;
    this->LastName = _LastName;
    this->Age = _Age;
    this->Address = _Address;
    this->City = _City;
    this->Phone = _Phone;
}

Student::~Student() {}

string Student::GetFirstName()
{
    return FirstName;
}

string Student::GetLastName()
{
    return LastName;
}

int Student::GetAge()
{
    return Age;
}

string Student::GetAddress()
{
    return Address;
}

string Student::GetCity()
{
    return City;
}

string Student::GetPhone()
{
    return Phone;
}

Main program

//#include "pch.h" // Visual Studio precompiled headers
#include <iostream>
#include <string>
#include "Course.h"
#include "Teacher.h"
#include "Student.h"

using namespace std;

int main()
{

    Teacher* Teacher1 = new Teacher("Graham", "Dearsley", 51, "8 Sudbury Ave", "Wembley", "07763245710");

    cout << Teacher1->GetFirstName() << endl;
    cout << Teacher1->GetPhone() << endl;

    Student *Student1 = new Student("Steve", "Hurwood", 63, "25 The Woods", "Harrow", "07Something");
    Student *Student2 = new Student("John", "Ryman", 50, "2 Preston Rd", "Wembley", "076Something");
    Student *Student3 = new Student("Angela", "Barton", 46, "1 Somewhere", "Acton", "07Something else");

    cout << Student1->GetFirstName() << endl;
    cout << Student1->GetPhone() << endl;

    return 0;
}

The problem I am having is writing the Course class and putting Student objects in its array. Is it even possible to do what they are asking ? I have figured out that the data type for the array would be Student but that's as far as I get. All the help on the Web says to use a vector instead of an array but the course hasn't covered them yet and its not what they are asking for.

Comments