Using classes

Pages: 12
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdlib>
#include <iostream>

using namespace std;

class student
{
private: 
         //attributes
         string name;
         string id;
         char gender;
         
public:
       
       student()
       {
                name = "";
                id = -1;
                gender = 'z';
                
       }
       
       string getName()
       {
              
              cout << "Please enter the name of the student:  ";
              cin >> name;
              
              return name;      
       }        
       
       string getID(string s)
       {
              s = name;
              
              cout << "Please enter the ID number for" << name << ": ";
              cin >> id;
              
              return id;
              
       }
       
       string getGender(string s)
       {
              s = name;
              
              cout << "Please enter the gender for" << name << ": " << endl
                   << "Enter (m) or (f).";
                   
              cin >> gender;
              
              if(gender == 'm')
              return "male";
          
              if(gender == 'f')
              return "female";
       }
};

void stuff()
{
     cout << "Stuff running\n" << endl;
     
     student();
     
}
int main(int argc, char *argv[])
{
    stuff();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
What are you having problems with exactly?

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
Last edited on
class is a type of object. And you create object like thus:

student object_name;

and call object methods like thus:

object_name.getName();

object_name.getID();

and you don't need string s in your method getID(string s) and other methods.

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;
Something like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <cstdlib>
#include <string>
#include <iostream>
#include <limits>
using namespace std;

class student
{
private: 
         //attributes
         string name;
         string id;
         char gender;
         
public:
       
       student()
       {
                name = "";
                id = -1;
                gender = 'z';
                
       }
       
       string getName()
       {
              

              return name;      
       }        
       
       string getID()
       {
           
              return id;
              
       }
       
      char getGender()
       {
             
           return gender;
       }
	     void setName(string s)
       {
              
              
            name =s;
              
          
       }        
       
       void setID(string s)
       {
              id =s;
              
              
       }
       
       void setGender(char s)
       {
		 gender = s;
              
   
       }
};

void stuff()
{
     cout << "Stuff running\n" << endl;
     
student teststudent;
string name ="-";
char cGender='-';


cout << "Please enter the ID number : ";
cin.clear();
cin.sync();
getline(cin,name);
teststudent.setID(name);


cout << "Please enter the name of the student:  ";
cin.clear();
cin.sync();
getline(cin,name);

teststudent.setName(name);
cout << "Please enter the gender for" << teststudent.getName()<< ": " << endl
                   << "Enter (m) or (f).";
cin.clear();
cin.sync();  
cin>>cGender;
teststudent.setGender(cGender);

cout<<"teststudent id: " <<teststudent.getID()<<endl
	<<"teststudent name: "<<teststudent.getName()<<endl
	<<"teststudent gender: ";


if (teststudent.getGender()=='M'||teststudent.getGender()=='m')
	cout<<"Male"<<endl;
else if (teststudent.getGender()=='F'||teststudent.getGender()=='f')
	cout<<"Female"<<endl;
else
	{cout<<"Guessing..."<<endl
              <<endl
              <<"Best Answer : MONSTER AHHHHHHHHH!!!!!!!!!!!";}
}
int main()
{
stuff();
    
cout<<"Press Enter Key To Exit"<<endl;
cin.clear();
cin.sync();
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
return 0;
}

Last edited on
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.
I have come this far and am trying to get the class to decide what school the student goes to but am having issues with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstdlib>
#include <string>
#include <iostream>

using namespace 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';
                  else if(schoolName = 'n')
                      campus = 'Nursery';
                  else if(schoolName = 't')
                      campus = 'Tonys';
                  else if(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;

}
ResidentBiscuit wrote:
...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.
I'm trying to get it to put the school with the student , but am having issues with the for loop and returning it.
1
2
3
4
5
6
else if(schoolName = 'n')
   campus = 'Nursery';
else if(schoolName = 't')
   campus = 'Tonys';
else if(schoolName = 'm')
   campus = 'Miss Anns';

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.
Last edited on
Is this what you wanted?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstdlib>
#include <string>
#include <iostream>

using namespace 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";
                  else if(s =="n")
                      schoolName = "Nursery";
                  else if(s == "t")
                      schoolName = "Tonys";
                  else if(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;

}

Last edited on
I tried this code but still getting errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   string getschoolName()
       {    
          //string campus = schoolName; 
                      
          if(schoolName = "d")
             return = "Devry";
          else if(schoolName = "n")
             return = "Nursery";
          else if(schoolName = "t")
             return = "Tonys";
          else if(schoolName = "m")
             return = "Miss Anns";
                                    
       }  


is there something I am missing?
Yes, you should be using a double equals sign for comparison:

if(schoolName == "d") //example

Also, there shouldn't be equals signs after the return statements.
You already set the school name look at my code above.

1
2
3
4
5
6
7
string getschoolName()
       {    
         // should be
         return schoolName;
                                    
       }
Subzero thank you. I am new to classes and am having so issues with this assignment. Ill see how much farther I go before needing a little more help
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class DeVryStudent : public Student
{
private:
     string iD;
     string campus;
     string program;
     
public:
     DeVryStudent()
     {
          cout << "DeVryStudent constructor running\n";
          
          iD = "";
          campus = "";
          program = "";
     }
     
     DeVryStudent(string s)
     {
          cout << "DeVryStudent overloaded constructor running\n";
          
          name = s;
          iD = "";
          campus = "";
          program = "";
     }
     
     ~DeVryStudent()
     {
          cout << name << " DeVryStudent destructor running\n";
     }
     
     void setID(string s, string pw)
     {
          //password protect
          //validate as 8 digits long
     }
     
     string getID(string pw)
     {
          //password protect
     }
     
     void setCampus(string s)
     {
     }
     
     string getCampus()
     {
     }
     
     void setProgram(string s)
     {
     }
     
     string getProgram()
     {
     }
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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <cstdlib>
#include <string>
#include <iostream>

using namespace 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";
          else if(s =="n")
             schoolName = "Nursery";
          else if(s == "t")
             schoolName = "Tonys";
          else if(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;

}
Code Below.
Last edited on
Pages: 12