I have a snippet of code and am trying to make sure I am doing this right. Here is what I have so far and what needs to be done. Any help would be great

#include <iostream>
using namespace std;
const int NUM_TEACHERS = 3;
class Teacher
{
public:
	Teacher();
private:
 char name[40];
 int teacherID;
 char class[20];
};

int main()
{
 //1.) write a constructor that will create three teachers
   teacher teacher[NUM_TEACHERS] = {
                  Student("Andy", 98765, "Science"),
                  Student("Jane", 12345, "Science"),
                  Student("Sally", 13579, "Science")};
 teacher*teacherPtr = &teacher;

Recommended Answers

All 3 Replies

What's class "student"? You have to add constructor in the class Teacher to construct this class with a Student.

In main you can't call class Teacher if you write "teacher"(with a lower case letter). It must be like this:

/*upper case only for the first one wich is the class*/Teacher teacher[NUM_TEACHERS] = {
...

unless you change the name of your class to "teacher"(with lower case letter too). but it wouldn't compile because you can't initiate a variable with the same name as its class.

#include <iostream>
 using namespace std;
 const int NUM_TEACHERS = 3;

Other than having a generic namespace resolution, this is all good.

class Teacher
 {
 public:
 Teacher();
 private:
 char name[40];
 int teacherID;
 char class[20];
 };

This class declaration is a decent declaration. You remembered the semi-colon at the end, that's a big newbie "gotcha", good job.

I do, however, take issue with your member variable names. Generally, you want to differentiate a member variable from a non-member variable by some manner. A common one is adding "m_" to the beginning of the name. The identifier (variable/function name) "class" is not a legal identifier because it's a "reserved word". Based on my previous suggestion, you'll want to call it something like "m_subject" instead (add "m_" to the improved (and legal) identifier "subject").

You may also want to consider changing the name of "teacherID" to "m_ID", it's fewer characters so it's quicker to type, but it still relays an appropriate level of information without being redundant. The identifier "name" is okay, but you should consider updating it in a similar fashion to something like "m_name".

You're also missing an overloaded constructor. I'll touch on the constructor issue below.

int main()
 {
 //1.) write a constructor that will create three teachers
 teacher teacher[NUM_TEACHERS] = {
 Student("Andy", 98765, "Science"),
 Student("Jane", 12345, "Science"),
 Student("Sally", 13579, "Science")};
 teacher*teacherPtr = &teacher;

Here is where the vast majority of your issues are coming from. C and C++ are case sensitive. This means that the identifiers "teacher" and "Teacher" are not the same, "TeAcHeR" is different still. Make sure that wherever you want to use the "Teacher" dataType you use the proper capitalization (I can see 2 spots in this snippet, there are 5 in the corrected code).

Where did the "Student" class come from? How do you expect an array of "Teacher" objects to hold "Student" objects? Unless "Student" inherits from "Teacher" (which I doubt because you're new), this isn't legal. Those three objects should be "Teacher" objects.

And now to the constructor issue. Are you familiar with function overloading? Function overloading allows you to write different versions of the same function that accomplish a similar task, but accomplish it in a different way. The different versions of the function(s) are differentiated by the number and dataTypes of their respective arguments. Example:

#include <iostream>

//namespace resolutions
using std::cout;
using std::endl;

//function prototypes
void displayMessage();        //the "default" message function
void displayMessage(int);     //an int-overloaded version of the message function
void displayMessate(double);  //another overloaded version of the function (based on double)

//begin main function
int main() {
  //local variable declarations
  int sampleInt = 1;
  double sampleDouble = 1.5;

  //call the default function
  displayMessage();

  //call the int version of the same function
  displayMessage(sampleInt);

  //call the double version of the same function
  displayMessage(sampleDouble);

  return(EXIT_SUCCESS);
}

//define the default message function
void displayMessage() {
  cout << "Hello, I am the default version of the message.\nI display from displayMessage().\n" << endl;
}

//define the int version of the overloaded message function
void displayMessage(int incomming) {
  cout << "Hello, I am the int-overloaded version of the message.\nI display from displayMessage(int).\n" << endl;
}

//define the double version of the overloaded message function
void displayMessage(double incomming) {
  cout << "Hello, I am the double-overloaded version of the message.\nI display from displayMessage(double).\n" << endl;
}

Now, you need to extend this concept to your "Teacher" constructor and add the appropriate one for the code you are using to create the array. How many arguments do you need? What are the dataTypes of those arguments? Here's a link that may help.

commented: Nice. +17

Thank you all so much for the help. You were right, I had student where it should have been teacher. With the help this forum gave me I was able to get the program working! Thanks again for everything. I will definitely visit this site more often to see what other tips and tricks I can pick up!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.