Hello, i had to write a program for class that met the following criteria:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Create a class named Student that has three member variables:
name – A string that stores the name of the student
numClasses – An integer that tracks how many courses the
student is currently enrolled in
classList – A dynamic array of strings used to store the names
of the classes that the student is enrolled in
Write appropriate
constructor(s),
mutator, and
accessor methods for the class along
with the following:
- A method that inputs all values from the user, including the list of class names.
This method will have to support input for an arbitrary number of classes.
- A method that outputs the name and list of all courses.
- A method that resets the number of classes to 0 and the classList to an empty list.
- An overloaded assignment operator that correctly makes a new copy of the list of
courses.
- A destructor that releases all memory that has been allocated.
Write a main method that tests all of your functions.
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace std;
class Student
{
public: Student();
~Student();
void InputData(); // Input all data from user
void OutputData(); // Output class list to console
void ResetClasses(); // Reset class list
Student& operator =(const Student& rightSide);// Assignment operator
private:
string name;
int numClasses;
string *classList;
};
#include <iostream>
#include <iomanip>
usingnamespace std;
Student::Student() : name(""), numClasses(0), classList(NULL)
{
}
// Student::~Student
Student::~Student()
{
numClasses = 0;
ResetClasses();
name = "";
}
// Student::ResetClasses
void Student::ResetClasses()
{
if (classList) {
delete [] classList;
classList = NULL;
numClasses = 0;
}
}
// Student::InputData
void Student::InputData()
{
// Reset the class list just in case this method
// was called again and the dynamic array wasn't cleared
ResetClasses();
cout << "Enter student name > ";
getline(cin, name);
cout << "Enter number of classes > ";
cin >> numClasses;
cin.ignore(2,'\n'); // Discard extra newline
if (numClasses > 0) {
// Dynamically construct array large enough to hold the
// number of classes
classList = new string[numClasses];
// Loop through the number of classes, inputting
// the name of each one into our dynamic array
for (int i=0; i<numClasses; i++) {
cout << "Enter name of class " << (i+1) << " > ";
getline(cin, classList[i]);
}
}
cout << endl;
}
// Student::OutputData
void Student::OutputData()
{
cout << "Name of Student: " << name << endl
<< "Class List: " << endl;
for (int i = 0; i < numClasses; i++) {
cout << setw(2) << right << i + 1 << ") " << classList[i] << endl;
}
}
// Student::=
Student& Student::operator =(const Student& rightSide)
{
ResetClasses();
numClasses = rightSide.numClasses;
if (numClasses > 0) {
classList = new string[numClasses];
for (int i = 0; i < numClasses; i++) {
classList[i] = rightSide.classList[i];
}
}
return *this;
}
// test function
void student()
{
// Test our code with two student classes
Student s1, s2;
s1.InputData(); // Input data for student 1
cout << "Student 1's data:" << endl;
s1.OutputData(); // Output data for student 1
cout << endl;
s2 = s1;
cout << "Student 2's data after assignment from student 1:" << endl;
s2.OutputData(); // Should output same data as for student 1
s1.ResetClasses();
cout << "Student 1's data after reset:" << endl;
s1.OutputData(); // Should have no classes
cout << "Student 2's data, should still have original classes:" << endl;
s2.OutputData(); // Should still have original classes
cout << endl;
}
The errors i keep getting are either THIS:
unresolved external symbol _main referenced in function ___tmainCRTStartup
fatal error LNK1120: 1 unresolved externals
OR THIS:
error LNK2019:unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
fatal error LNK1120: 1 unresolved externals
I googled both these issues and every answer was to switch the Linker SubSystem to Console or Windows depending on the error (THIS IS SUPPOSE TO BE A CONSOLE APPLICATION). Whether the SubSystem is Console or Windows i get those errors. Please can anybody help me with this!
I don't know what you google'd but you need to have an entry point for your program to work. This is is defined as the "int main()" function by default.