943,805 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 337
  • C++ RSS
Apr 5th, 2009
0

need help on my inheritance here

Expand Post »
help.why can't my coding loop properly.,..i notice it can't show the loop of ptrpers[j] ?
what did i do wrong.?
it didn't show back the input i put on earlier.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class person
  5. {
  6. protected:
  7. char name[40];
  8. public:
  9. void setname()
  10. {
  11. cout<<"Enter Name:";
  12. cin>>name;
  13. }
  14.  
  15. void putname()
  16. {cout <<"Name:"<<name<<endl;}
  17.  
  18. virtual void getdata()=0;//pure virtual or abstrak
  19. virtual bool outstanding()=0;//pure virtual or abstrak
  20. };
  21.  
  22. class prof:public person
  23. {
  24. int numpub;
  25. public:
  26. void getdata()
  27. {
  28. person::setname();
  29. cout<<"Enter number of prof's publication:";
  30. cin>>numpub;
  31. }
  32. bool outstanding()
  33. {return(numpub>60)? true:false;}
  34. };
  35. class stud:public person
  36. {
  37. int mark;
  38. public:
  39. void getdata()
  40. {
  41. person::setname();
  42. cout<<"Enter student mark:";
  43. cin>>mark;
  44. }
  45. bool outstanding()
  46. {return(mark>60)? true:false;}
  47. };
  48. int main()
  49. {
  50. person*ptrpers[100];
  51. int n=0;//i=0;
  52. char choice;
  53.  
  54. do{
  55. cout<<"enter student or prof,(s/p):";
  56. cin>>choice;
  57. if (choice == 's')
  58. ptrpers[n] = new stud;
  59.  
  60. else
  61. ptrpers[n] = new prof;
  62.  
  63. ptrpers[n]-> getdata();
  64.  
  65. cout<<"enter another prson(y/n)?";
  66. cin>>choice;
  67.  
  68. }while(choice=='y');
  69.  
  70. for(int j=0;j<n;j++)
  71. {
  72. ptrpers[j] -> putname();
  73. if(ptrpers[j] -> outstanding() )
  74. {
  75. cout<<"person outstanding\n";
  76. }
  77. }
  78. return 0;
  79. }
Similar Threads
Reputation Points: 7
Solved Threads: 0
Light Poster
MyRedz is offline Offline
30 posts
since Jul 2007
Apr 5th, 2009
0

Re: need help on my inheritance here

Because you never increment "n" anywhere. In your do while, you need to stick an n++ some place.
Last edited by Comatose; Apr 5th, 2009 at 1:34 pm.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Apr 5th, 2009
0

Re: need help on my inheritance here

thanks i didn't realize that mistake.now it works like a charm!
Reputation Points: 7
Solved Threads: 0
Light Poster
MyRedz is offline Offline
30 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Implementing Linked List iterator, gcc is acting wierd
Next Thread in C++ Forum Timeline: reading file into multidimensional array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC