I am trying to get a program to call a class, but am having trouble doing so. Here is what I have so far and am wanting it to call the questions for the class program. Any help would be great.
EDIT:
Just saw it. You never created an object of that class. Your compiler thinks you're calling just a normal function called student(), as opposed to what you're trying to do.
And you can't call a constructor explicitly anyways. When you create an object, the constructor is called automatically
Yea after actually looking at this, your functions are poorly put together. Why does getName() actually SET a name? And then return the name anyway? Functions are meant to do a SINGLE task. Typically a good rule of thumb is, a function should either provide some I/O, OR some kind of calculation of some sort. Not both.
Getter functions should have one line return variable;
So as far as having a class named student, and then using that in 4 other sub-classes can I just copy and paste the code in four other classes? Say if I need it for student information for up to four schools? I am just having issues understanding classes and how to use them.
You should always think about what the requirements are before you start programming. For example let's say you have a program that keeps track of school and students.
You could do this with two classes: A student class and a school class.
School class could hold the name of the school, location, etc. And then it would have a container that holds Student objects.
Student class would have things such as name, year, gpa, current schedule, etc.
Start the program by creating a school object and assigning it some name and basic info.
Then have a way of creating a bunch of student objects, and add them to the container in your school object.
#include <cstdlib>
#include <string>
#include <iostream>
usingnamespace std;
class student
{
private:
//attributes
string fName;
string lName;
string id;
string schoolName;
public:
//declaring variables
student()
{
fName = "";
lName = "";
id = -1;
cout << "Student constructor running\n";
}
//calling function to get students first name
string getfName()
{
return fName;
}
//calling function to get students last name
string getlName()
{
return lName;
}
//call function to get id number
string getID()
{
return id;
}
//calling function to get school name
string getschoolName()
{
for(int i = 0; i < 5; i++)
{
if(schoolName == 'd')
campus = 'Devry';
elseif(schoolName = 'n')
campus = 'Nursery';
elseif(schoolName = 't')
campus = 'Tonys';
elseif(schoolName = 'm')
campus = 'Miss Anns';
}
return 0;
}
void setfName(string s)
{
fName = s;
}
void setlName(string s)
{
lName = s;
}
void setID(string s)
{
id = s;
}
void setschoolName(string s)
{
schoolName = s;
}
};
void stuff()
{
//declaring variables for the program
student teststudent;
string fName = "-";
string lName = "-";
string id = "-";
string schoolName = "-";
//asking for the id number and storing it in id
cout << "Please enter the ID number : ";
cin >> id;
teststudent.setID(id);
//asking for first name and stroing it in fName
cout << "Please enter the first name of the student: ";
cin >> fName;
teststudent.setfName(fName);
//asking for last name and storingit in lName
cout << "Please enter the last name of the student: ";
cin >> lName;
teststudent.setlName(lName);
//asking what school the student attends
cout << "Enter what school the student attends, by entering (d)evry\n"
<< "(n)ursery, (t)ony's Judo school, (m)iss Ann's Dance School: ";
cin >> schoolName;
teststudent.setschoolName(schoolName);
cout << "Student id: " << teststudent.getID() << endl
<< "Student name: " << teststudent.getfName() << " "
<< teststudent.getlName() << endl;
cout << "Student goes to " << teststudent.getschoolName << endl;
}
int main()
{
stuff();
system("PAUSE");
return EXIT_SUCCESS;
}
...You never created an object of that class. Your compiler thinks you're calling just a normal function called student(), as opposed to what you're trying to do.
And you can't call a constructor explicitly anyways...
Not true. If we have the call to student(); like in the OP, then a temporary instance of student is constructed and then immediately discarded.
All these conditions are assigning to schoolName as opposed to checking for equality. In the first condition you have, you use a double equals sign which is the correct way to check for equality. Also, schoolName is a string yet you're comparing with chars (single quotes mean it's a char). You should be putting double quotes around d, n, t, and m. Same thing with Devry, Nursery, etc. In that function, campus is never defined and I think you should be returning campus instead of zero. And wny are those checks in a loop anyway? Nothing is changing between the iterations.
#include <cstdlib>
#include <string>
#include <iostream>
usingnamespace std;
class student
{
private:
//attributes
string fName;
string lName;
string id;
string schoolName;
public:
//declaring variables
student()
{
fName = "";
lName = "";
id = -1;
cout << "Student constructor running\n";
}
//calling function to get students first name
string getfName()
{
return fName;
}
//calling function to get students last name
string getlName()
{
return lName;
}
//call function to get id number
string getID()
{
return id;
}
//calling function to get school name
string getschoolName()
{
return schoolName;
}
void setfName(string s)
{
fName = s;
}
void setlName(string s)
{
lName = s;
}
void setID(string s)
{
id = s;
}
void setschoolName(string s)
{
if(s == "d")
schoolName = "Devry";
elseif(s =="n")
schoolName = "Nursery";
elseif(s == "t")
schoolName = "Tonys";
elseif(s == "m")
schoolName = "Miss Anns";
}
};
void stuff()
{
//declaring variables for the program
student teststudent;
string fName = "-";
string lName = "-";
string id = "-";
string schoolName = "-";
//asking for the id number and storing it in id
cout << "Please enter the ID number : ";
cin >> id;
teststudent.setID(id);
//asking for first name and stroing it in fName
cout << "Please enter the first name of the student: ";
cin >> fName;
teststudent.setfName(fName);
//asking for last name and storingit in lName
cout << "Please enter the last name of the student: ";
cin >> lName;
teststudent.setlName(lName);
//asking what school the student attends
cout << "Enter what school the student attends, by entering (d)evry\n"
<< "(n)ursery, (t)ony's Judo school, (m)iss Ann's Dance School: ";
cin >> schoolName;
teststudent.setschoolName(schoolName);
cout << "Student id: " << teststudent.getID() << endl
<< "Student name: " << teststudent.getfName() << " "
<< teststudent.getlName() << endl;
cout << "Student goes to " << teststudent.getschoolName()<< endl;
}
int main()
{
stuff();
system("PAUSE");
return EXIT_SUCCESS;
}
So if I was wanting to have a subclass for each school, how would incorperate each one? I have a thought of doing it like this, will it work? and will I have to write a code for each subclass inside of the stuff class?
You might want to consider breaking them up into different files if you are going to do it that way. Other than that it looks like you are on the right track.
As far as making different classes for each of the schools, how would I then call each different one to be used? I guess I am wanting to know that after they input what school they are attending, do I need different main functions for each class? This is what I have now, and am wondering if they choose devry, do I need a different main function or just put the other questions in the stuff function.
#include <cstdlib>
#include <string>
#include <iostream>
usingnamespace std;
class student
{
private:
//attributes for the student class
string fName;
string lName;
string id;
string schoolName;
public:
//declaring variables
student()
{
fName = "";
lName = "";
id = -1;
cout << "Student constructor running\n" << endl;
}
//calling function to get students first name
string getfName()
{
return fName;
}
//calling function to get students last name
string getlName()
{
return lName;
}
//call function to get id number
string getID()
{
return id;
}
//calling function to get school name
string getschoolName()
{
return schoolName;
}
void setfName(string s)
{
fName = s;
}
void setlName(string s)
{
lName = s;
}
void setID(string s)
{
id = s;
}
//setting the name for the school for the student and returing it
void setschoolName(string s)
{
if(s == "d")
schoolName = "Devry";
elseif(s =="n")
schoolName = "Nursery";
elseif(s == "t")
schoolName = "Tonys";
elseif(s == "m")
schoolName = "Miss Anns";
}
};
class DeVryStu : public student
{
private:
string campus;
string program;
public:
DeVryStu()
{
cout << "Devry student constructor is running.\n";
campus = "-";
program = "-";
}
//getting what campus the student goes to
string getCampus()
{
return campus;
}
//getting what school program the student is in
string getProgram()
{
return program;
}
void setCampus(string s)
{
campus = s;
}
void setProgram(string s)
{
program = s;
}
};
void stuff()
{
//declaring variables for the program
student teststudent;
string fName = "-";
string lName = "-";
string id = "-";
string schoolName = "-";
//asking for the id number and storing it in id
cout << "Please enter the ID number : ";
cin >> id;
teststudent.setID(id);
//asking for first name and stroing it in fName
cout << "Please enter the first name of the student: ";
cin >> fName;
teststudent.setfName(fName);
//asking for last name and storingit in lName
cout << "Please enter the last name of the student: ";
cin >> lName;
teststudent.setlName(lName);
//asking what school the student attends
cout << "Enter what school the student attends: (d)evry\n"
<< "(n)ursery, (t)ony's Judo school, (m)iss Ann's Dance School: ";
cin >> schoolName;
teststudent.setschoolName(schoolName);
//displaying outputs for the inputed information about the student.
cout << "Student id: " << teststudent.getID() << endl
<< "Student name: " << teststudent.getfName() << " "
<< teststudent.getlName() << endl;
cout << "Student goes to " << teststudent.getschoolName()<< endl;
}
int main()
{
stuff();
system("PAUSE");
return EXIT_SUCCESS;
}