By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

I have another C++ mess on my hands

P: 9
I can not get this program to work.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. const int SIZE = 80;
  9.  
  10.  
  11.  
  12. class Student
  13. {
  14. protected:
  15.  
  16.     int social;
  17.     string name;
  18.     char *NamePtr;
  19. public:
  20.     Student (int = 999999999 , char [] = "unassigned");
  21.     void setSSN(long);
  22.     long getSSN();
  23.     void setName(char[]);
  24.     char* getName();
  25.     virtual double calcGPA();
  26. };
  27.  
  28. Student::Student(int ssn, string studentname)
  29. {
  30.     social = ssn;
  31.     strcpy (name, studentname);
  32. }
  33.  
  34. void Student::setSSN(int SSN)
  35. {
  36.             social = SSN;
  37.         return;
  38. }
  39.  
  40. long Student::getSSN()
  41. {
  42.     return (social);
  43. }
  44.  
  45. void Student::setName(char name2)
  46. {
  47.         strcpy (name, name2);
  48.         return;
  49. }
  50.  
  51. char* Student::getName()
  52. {    
  53.     NamePtr = name;
  54.     return (NamePtr);
  55. }
  56.  
  57. double Student::calcGPA()
  58. {
  59.     cout << "Student calcGPA" << endl;
  60.     return (0);
  61. }
  62.  
  63. class GradStudent : public Student 
  64. {
  65. private:
  66.     char status;
  67. public:
  68.     GradStudent (char = 'A');
  69.     virtual double calcGPA();
  70.     void SetStatus(char);
  71. };
  72.  
  73. GradStudent::GradStudent(char grade)
  74. {
  75.     status = grade;
  76. }
  77.  
  78. double GradStudent::calcGPA()
  79. {
  80.     double GPA = 0.0;
  81.     if (status == 'A' || status == 'a')
  82.         GPA = 4.0;
  83.     cout << "GradStudent calcGPA";
  84.     return (GPA);
  85. }
  86.  
  87. void GradStudent::SetStatus (char stat)
  88. {
  89.     status = stat;
  90.     return;
  91. }
  92.  
  93. class UnderGradStudent : public Student
  94. {
  95. private:
  96.     double CreditHrs;
  97.     double QualityPts;
  98. public:
  99.     UnderGradStudent ( double = 0, double = 0);
  100.     virtual double calcGPA ();
  101.     void setCredits(int);
  102.     void setQuality(int);
  103. };
  104.  
  105. UnderGradStudent::UnderGradStudent(double Hours, double Points)
  106. {
  107.     CreditHrs = Hours;
  108.     QualityPts = Points;
  109.     if (Hours < 0)
  110.         CreditHrs = 0;
  111.     if (Points < 0)
  112.         QualityPts = 0;
  113. }
  114.  
  115. double UnderGradStudent::calcGPA ()
  116. {
  117.     double GPA = 0.0;
  118.     if (CreditHrs > 0)
  119.         GPA = QualityPts / CreditHrs;
  120.     cout << "UnderGraduateStudent calcGPA";
  121.     return (GPA);
  122. }
  123.  
  124. void UnderGradStudent::setCredits (int C)
  125. {
  126.     CreditHrs = C;
  127.     if (C < 0)
  128.         CreditHrs = 0;
  129.     return;
  130. }
  131.  
  132. void UnderGradStudent::setQuality (int Q)
  133. {
  134.     QualityPts = Q;
  135.     if (Q < 0)
  136.         QualityPts = 0;
  137.     return;
  138. }
  139.  
  140.  
  141. int main()
  142. {
  143.     char ans = 'n';
  144.     char grade;
  145.     string StudentName;
  146.     double StudentSSN;
  147.     int choice, points, credits, continue;
  148.  
  149.  
  150.     Student* a = NULL;
  151.  
  152.     do
  153.     {
  154.         cout << "Enter 1 for undergrad.\nEnter 2 for grad" << endl << "-->";
  155.         cin >> choice;
  156.  
  157.         if (choice == 1)
  158.         {
  159.             a = new UnderGradStudent;
  160.  
  161.             cin.ignore();
  162.             cout << "Please enter student name: ";
  163.             cin.getline(StudentName, SIZE);
  164.             a->setName(StudentName);        
  165.         }
  166.  
  167.         else if (choice == 2)
  168.         {
  169.             a = new GradStudent;
  170.  
  171.             cin.ignore();
  172.             cout << "Please enter student name: ";
  173.             cin.getline(StudentName, SIZE);
  174.             a->setName(StudentName);
  175.  
  176.  
  177.         }
  178.  
  179.  
  180.         else continue;
  181.  
  182.  
  183.         cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
  184.             << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;
  185.  
  186.  
  187.         delete a;
  188.         a = NULL;
  189.  
  190.         cout << "Continue?: ";
  191.         cin >> continue;
  192.         cin.ignore();
  193.         cout << endl;
  194.  
  195.     }while (toupper(ans) == 'Y');
  196.  
  197.  
  198.     cout << "Press ENTER to quit.";
  199.     cin.ignore( 1000, '\n' );
  200.  
  201.  
  202.     return 0;
  203. }
Oct 6 '15 #1

✓ answered by weaknessforcats

There are lots of errors here. Foremost is that some of your member functions have different arguments from what is in the class declaration.

etc..

I suggest you create a new source file ana have only your class definition and a main:

Expand|Select|Wrap|Line Numbers
  1. class Student
  2. {
  3.  
  4. };
  5.  
  6. int main()
  7. {
  8.    Student s;
  9. }
Verify this compiles.

Now add member data and a constructor.

Verify this compiles.
Verify the program works.

Now add another member function.

Add code in main() to test it.
Verify this compiles.
Verify the program still works.


By working gradually you guarantee the program compiles and runs correctly so any errors would be in the last bit of code you added. Just work through a step at a time.

I see you are using things like strcpy in a C++ program. Bad news. YOu use the assignment operator on string objects.

Post again and let me know how you are coming along.

Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
There are lots of errors here. Foremost is that some of your member functions have different arguments from what is in the class declaration.

etc..

I suggest you create a new source file ana have only your class definition and a main:

Expand|Select|Wrap|Line Numbers
  1. class Student
  2. {
  3.  
  4. };
  5.  
  6. int main()
  7. {
  8.    Student s;
  9. }
Verify this compiles.

Now add member data and a constructor.

Verify this compiles.
Verify the program works.

Now add another member function.

Add code in main() to test it.
Verify this compiles.
Verify the program still works.


By working gradually you guarantee the program compiles and runs correctly so any errors would be in the last bit of code you added. Just work through a step at a time.

I see you are using things like strcpy in a C++ program. Bad news. YOu use the assignment operator on string objects.

Post again and let me know how you are coming along.
Oct 6 '15 #2

P: 9
Some of the error messages from the compiler does not make sense, well for me they do not, being new to C++.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Student
  7. {
  8. protected:
  9.  
  10.     int social;
  11.     string name;
  12.     string *NamePtr;
  13. public:
  14.     Student (); // Default constructor
  15.     Student(int ssn, string StudentName);
  16.     void setSSN(int);
  17.     int getSSN();
  18.     void setName(string);
  19.     string* getName();
  20.     virtual double calcGPA();
  21. };
  22.  
  23. Student ::Student()
  24. {
  25. }
  26. Student::Student(int ssn, string  StudentName)
  27. {
  28.  
  29.     social = ssn;
  30.     strcpy (name, Studentname);
  31.  
  32.     return;
  33. }
  34.  
  35. void Student::setSSN(int SSN)
  36. {
  37.         social = SSN;
  38.         return;
  39. }
  40.  
  41. int Student::getSSN()
  42. {
  43.     return (social);
  44. }
  45.  
  46. void Student::setName(string name2)
  47. {
  48.         strcpy (name, name2);
  49.         return;
  50. }
  51.  
  52. string* Student::getName()
  53. {    
  54.     NamePtr = name;
  55.     return (NamePtr);
  56. }
  57.  
  58. double Student::calcGPA()
  59. {
  60.     cout << "Student calcGPA" << endl;
  61.     return (0);
  62. }
  63.  
  64. class GradStudent : public Student 
  65. {
  66. private:
  67.     char Status;
  68. public:
  69.     GradStudent (char = 'A');
  70.     virtual double calcGPA();
  71.     void SetStatus(char);
  72. };
  73.  
  74. GradStudent::GradStudent(char grade)
  75. {
  76.     Status = grade;
  77. }
  78.  
  79. double GradStudent::calcGPA()
  80. {
  81.     double GPA = 0.0;
  82.     if (Status == 'A' || Status == 'a')
  83.         GPA = 4.0;
  84.     cout << "GradStudent calcGPA";
  85.     return (GPA);
  86. }
  87.  
  88. void GradStudent::SetStatus (char stat)
  89. {
  90.     Status = stat;
  91.     return;
  92. }
  93.  
  94. class UnderGradStudent : public Student
  95. {
  96. private:
  97.     double CreditHrs;
  98.     double QualityPts;
  99. public:
  100.     UnderGradStudent ( double = 0, double = 0);
  101.     virtual double calcGPA ();
  102.     void setCreditHrs(int);
  103.     void setQuality(int);
  104. };
  105.  
  106. UnderGradStudent::UnderGradStudent(double Hours, double Points)
  107. {
  108.     CreditHrs = Hours;
  109.     QualityPts = Points;
  110.     if (Hours < 0)
  111.         CreditHrs = 0;
  112.     if (Points < 0)
  113.         QualityPts = 0;
  114. }
  115.  
  116. double UnderGradStudent::calcGPA ()
  117. {
  118.     double GPA = 0.0;
  119.     if (CreditHrs > 0)
  120.         GPA = QualityPts / CreditHrs;
  121.     cout << "UnderGraduateStudent calcGPA";
  122.     return (GPA);
  123. }
  124.  
  125. void UnderGradStudent::setCreditHrs (int C)
  126. {
  127.     CreditHrs = C;
  128.     if (C < 0)
  129.         CreditHrs = 0;
  130.     return;
  131. }
  132.  
  133. void UnderGradStudent::setQuality (int Q)
  134. {
  135.     QualityPts = Q;
  136.     if (Q < 0)
  137.         QualityPts = 0;
  138.     return;
  139. }
  140.  
  141.  
  142. int main()
  143. {
  144.     char ans = 'n', grade, Status;
  145.     string StudentName;
  146.     int StudentSSN;
  147.     int code, CreditHrs, points;
  148.  
  149.  
  150.     Student* a = NULL;
  151.  
  152.     do
  153.     {
  154.         cout << "Enter 1 for undergrad.\nEnter 2 for grad." << endl << "-->";
  155.         cin >> code;
  156.  
  157.         if (code == 1)
  158.         {
  159.             a = new UnderGradStudent;
  160.  
  161.             cin.ignore();
  162.             cout << "Enter Student name: ";
  163.             cin.getline(StudentName);
  164.             a->setName (StudentName);
  165.             cout << "Enter Student SSN: ";
  166.             cin >> StudentSSN;
  167.             a->getSSN();
  168.  
  169.             cout << "Enter credit hours: ";
  170.             cin >> CreditHrs;
  171.             a->setCreditHrs(CreditHrs);
  172.  
  173.             cout << "Enter quality points: ";
  174.             cin >> points;
  175.             a->setQuality(points);
  176.             cout << endl;
  177.  
  178.  
  179.         }
  180.  
  181.         else if (code == 2)
  182.         {
  183.             a = new GradStudent;
  184.  
  185.             cin.ignore();
  186.             cout << "Enter Student name: ";
  187.             a->getName(StudentName);
  188.  
  189.  
  190.             cout << "Enter Student SSN: ";
  191.             cin >> StudentSSN;
  192.             a->setSSN(StudentSSN);
  193.  
  194.             cout << "Enter Status: ";
  195.             cin >> Status;
  196.             a->SetStatus(Status);
  197.             cout << endl;
  198.         }
  199.  
  200.  
  201.  
  202.  
  203.  
  204.         cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
  205.             << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;
  206.  
  207.  
  208.         delete a;
  209.         a = NULL;
  210.  
  211.         cout << "Continue?: ";
  212.         cin >> ans;
  213.         cin.ignore();
  214.         cout << endl;
  215.     }
  216.     while (toupper(ans) == 'Y');
  217.  
  218.  
  219.  
  220.     return 0;
  221. }
Oct 9 '15 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Expand|Select|Wrap|Line Numbers
  1. Student::Student(int ssn, string  StudentName)
  2. {
  3.  
  4.     social = ssn;
  5.     //strcpy(name, Studentname);
  6.     name = StudentName;
  7.  
  8.     return;
  9. }
Please stop using strcpy. This is C++ and not C. strcpy only works with char* strings with a \0 terminator.

A C++ string object is not a C-string.

Also:
Expand|Select|Wrap|Line Numbers
  1. string* Student::getName()
  2. {
  3.     NamePtr = name;
  4.     return (NamePtr);
  5. }
  6.  
NamePtr is a string* and name is a string. You can't assign an obect to a pointer.

NamePtr is a hacker's delight. It exposes the private data member member and allows you to change it without using a member function. Remove this from your design. You don't need a string*.

The code should be:
Expand|Select|Wrap|Line Numbers
  1. string Student::getName()
  2. {
  3.     ///NamePtr = name;
  4.     //return (NamePtr);
  5.     return name;
  6. }
But here is your real problem:
Expand|Select|Wrap|Line Numbers
  1. a = new UnderGradStudent;
  2. ......
  3. ....
  4. cin >> points;
  5. a->setQuality(points);
  6.  
setQuality is a method of UnderGradStudent whereas your variable "a" is a Student. You can only call base class methods from a base class pointer.

Only you know that a points to an UnderGradStudent object. The compiler has no idea.

For this to work you need a setQuality method in Student:

Expand|Select|Wrap|Line Numbers
  1. class Student
  2. {
  3.   public:
  4.    virtual void setQuality(int) =0;
  5. };
This code will allow a->setQuality to work and the virtual will cause a call to UnderGradStudent::setQuality.

You might read this: http://bytes.com/topic/c/insights/79...polymorphism-c

Fix your code and post again. And please stop using strcpy or any other C functions from 1968.
Oct 9 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.