Classes and get/set methods

Ok so im having some problems with this program that uses classes and methods. instead of comments that say set the first/last/subject for paper1 should there be some kind of module there or what? The errors I am getting are Termpaper.h no such file in directory, expected expression before "paper" (I thought that by placing #include termpaper.h in the header delcared that) any help would be nice. Thank you very much.
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
// Start
//     Declarations
//       TermPaper  paper1
//       string firstName
//       string lastName
//       string subject
//       character grade
//     output "Please enter first name.  "
//     input firstName
//     output "Please enter last name.  "
//     input lastName
//     output "Please enter subject.  "
//     input subject
//     output "Please enter letter grade.  "
//     input grade
//     Set the first name for paper1
//     Set the last name for paper1
//     Set the subject for paper1
//     Set the letter grade for paper1
//     output "First Name: ", paper1.getFirstName()
//     output "Last Name: ", paper1.getLastName()
//     output "Subject: ", paper1.getSubject()
//     output "Grade: ", paper1.getGrade()
// Stop

#include <cstdlib>
#include <iostream>
#include <string>
#include "TermPaper.h"

using namespace std;

int main()
{
    TermPaper paper 1;
    string firstName;
    string lastName;
    string subject;
    char grade;
    
    cout << "Please enter first name. " << endl;
    cin >> firstName;
    
    cout << "Please enter last name. " << endl;
    cin >> lastName;
    
    cout << "Please enter subject. " << endl;
    cin >> subject;
    
    cout << "Please enter letter grade. " << endl;
    cin >> grade;
    
   // Set the first name for paper1
   // Set the last name for paper1
   // Set the subject for paper1
   // Set the letter grade for paper1 
    
    
    cout << "First Name: " << paper1.getFirstName();
    cout << "Last Name: " << paper1.getLastName();
    cout << "Subject: " << paper1.getSubject();
    cout << "Grade: " << paper1.getGrade();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}




this is the class header

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
#include <string>

using namespace std;

#ifndef _TermPaper
#define _TermPaper
class TermPaper
{
   private:
	   string fName; // first name
	   string lName; // last name
	   string subject; // subject of the paper
	   char letterGrade; // assigned letter grade



   public:
	   TermPaper( );
	   void setFname(string fN);
	   void setLName(string lN);
	   void setSubject(string sub);
	   void setLetterGrade(char grade);
	   string  getFname( );
	   string getLName( );
	   string getSubject( );
	   char getLetterGrade( );

};
#endif 



This is the class definition.
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
#include <cstdlib>
#include <iostream>
#include "Termpaper.h"

using namespace std;



TermPaper::TermPaper( )
{
  fName = "";
  lName = "";
  subject = "";
  letterGrade = 'F';
}
void TermPaper::setFname(string fN)
{
   fName = fN;
}
void TermPaper::setLName(string lN)
{
  lName = lN;
}
void TermPaper::setSubject(string sub)
{
  subject = sub;
}
void TermPaper::setLetterGrade(char grade)
{
  letterGrade = grade;
}
string  TermPaper::getFname( )
{
  return fName;
}
string TermPaper::getLName( )
{
  return lName;
}
string TermPaper::getSubject( )
{
  return subject;
}
char TermPaper::getLetterGrade( )
{
  return letterGrade;
}



Im not sure if i have to combine the header and definition into one file in the Dev c++ environment or if they stay seperate or if they just need to be .txt files in the same folder as the other program files. Any help would be much appreciated.
The error message is clear enough. The compiler can not find header "TermPaper.h".
Last edited on
closed account (Dy7SLyTq)
a) you cant have spaces in a variable name
b) are they all in the same directory?
c) if you are using an ide are all files a part of the project?
@DTS yes they are all in the same folder. I believe that at the moment the files are not all in the same program but they do call each other.

@vlad I tried renaming the .txt file that has the termpaper header to TermPaper.h but its still giving me an error. Though I dont think that what I did would be the fix.
Ok ive gotten past the other previous problems and have most of the program compiled but at the end I still get errors. But these errors are ones that I have not seen before:

[Linker error] undefined reference to `TermPaper::getFirstName()'
[Linker error] undefined reference to `TermPaper::getLastName()'
[Linker error] undefined reference to `TermPaper::getGrade()'

Any help would be much appreciated. I feel like I'am really stuck at this point as to what the errors can be. Thank you to anyone who can help.



Class Definition .cpp
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
// Class automatically generated by Dev-C++ New Class wizard
#include <cstdlib>
#include <iostream>
#include <string>
#include "termpaper.h" 

using namespace std;


TermPaper::TermPaper()
{
fName = "";
lName = "";
subject = "";
letterGrade = 'F';            
	
}
void TermPaper::setFname(string fN)
{
   fName = fN;
}
void TermPaper::setLName(string lN)
{
  lName = lN;
}
void TermPaper::setSubject(string sub)
{
  subject = sub;
}
void TermPaper::setLetterGrade(char grade)
{
  letterGrade = grade;
}
string  TermPaper::getFname( )
{
  return fName;
}
string TermPaper::getLname( )
{
  return lName;
}
string TermPaper::getSubject( )
{
  return subject;
}
char TermPaper::getLetterGrade( )
{
  return letterGrade;
}


Class Header .h
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
// Class automatically generated by Dev-C++ New Class wizard
#include <string>

using namespace std;
#ifndef _TermPaper
#define _TermPaper
class TermPaper
{
 	  private:
	   string fName; 
	   string lName; 
	   string subject; 
	   char letterGrade; 



   public:
	   TermPaper( );
	   void setFname(string fN);
	   void setLName(string lN);
	   void setSubject(string sub);
	   void setLetterGrade(char grade);
	   string  getFname( );
	   string getLname( );
	   string getSubject( );
	   char getLetterGrade( );
};

#endif 



Main Program
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
// Pseudocode PLD Chapter 10 #3 pg. 445
// Start
//     Declarations
//       TermPaper  paper1
//       string firstName
//       string lastName
//       string subject
//       character grade
//     output "Please enter first name.  "
//     input firstName
//     output "Please enter last name.  "
//     input lastName
//     output "Please enter subject.  "
//     input subject
//     output "Please enter letter grade.  "
//     input grade
//     Set the first name for paper1
//     Set the last name for paper1
//     Set the subject for paper1
//     Set the letter grade for paper1
//     output "First Name: ", paper1.getFirstName()
//     output "Last Name: ", paper1.getLastName()
//     output "Subject: ", paper1.getSubject()
//     output "Grade: ", paper1.getGrade()
// Stop

#include <cstdlib>
#include <iostream>
#include <string>
#include "TermPaper.h"

using namespace std;

int main()
{
    TermPaper paperone;
    string firstName;
    string lastName;
    string subject;
    char grade;
    
    cout << "Please enter first name. " << endl;
    cin >> firstName;
    
    cout << "Please enter last name. " << endl;
    cin >> lastName;
    
    cout << "Please enter subject. " << endl;
    cin >> subject;
    
    cout << "Please enter letter grade. " << endl;
    cin >> grade;
    
   // Set the first name for paper1
   // Set the last name for paper1
   // Set the subject for paper1
   // Set the letter grade for paper1 
    
    
    cout << "First Name: " << paperone.getFirstName();
    cout << "Last Name: " << paperone.getLastName();
    cout << "Subject: " << paperone.getSubject();
    cout << "Grade: " << paperone.getGrade();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



Im also still wondering if there is some kind of coding that needs to be done to the middle of the main program instead of just the comments that say "set the first name for paper1". If anyone has any ideas about that too let me know. thank you! :)
Re: the errors - you are trying to call functions that don't exist. Compare the function (method) names in the class with what you are calling in main().


Im also still wondering if there is some kind of coding that needs to be done to the middle of the main program instead of just the comments that say "set the first name for paper1".

Question: Once you get the errors corrected what do you think the getters will return?
well in this class if there was a set or get function it would have been in the pseudocode and in our book for the semester hasnt had any examples of a set statement in the main function
Hi hagfish88,

This maybe a surprise for you, but I don't think you need get & set functions at all !

We had a huge discussion about this here:

http://www.cplusplus.com/forum/lounge/101305/


I'll try to do an executive summary for you:

-. Naive (straight assignment) set functions are really bad. For example hagfish.setAcctBalance(-1000000);

Use constructors, with initialiser lists instead.

-. If each class member has a public get / set function, then all the variables might as well be public !!

-. Functions that Update a value after it has been constructed, should have validation code. For example a Withdrawal function might check whether the user is authorised, and if there is sufficient balance say.

-. Get functions are no where near as bad, but are only really needed when access to a variable is needed from outside the class.

-. Don't use get functions when a member function would be better. For example you have this in main():

60
61
62
63
    cout << "First Name: " << paperone.getFirstName();
    cout << "Last Name: " << paperone.getLastName();
    cout << "Subject: " << paperone.getSubject();
    cout << "Grade: " << paperone.getGrade();


Instead you could have a member function like this :

1
2
3
4
5
6
7

void TermPaper::PrintDetails() {
    std::cout << "First Name: " << m_FirstName << "\n";
    std::cout << "Last Name: " << m_LastName << "\n";
    std::cout << "Subject: " << m_Subject << "\n";
    std::cout << "Grade: " << m_Grade << "\n";
}


This idea is part of the concept of encapsulation : Generally, all the functionality and data for an object should be in the object's class.

The problem is that book authors teach the idea of having private variables (which is right), so then one needs accessor & mutator functions (get / set), but then they don't explain how to do that properly.

If you change you code to implement these suggestions, then you will probably have much less & better code, and might even get better marks, if this is an assignment.

Hope all goes well 8+)
Topic archived. No new replies allowed.