need help on my inheritance here

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 25
Reputation: MyRedz is an unknown quantity at this point 
Solved Threads: 0
MyRedz MyRedz is offline Offline
Light Poster

need help on my inheritance here

 
0
  #1
Apr 5th, 2009
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: need help on my inheritance here

 
0
  #2
Apr 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 25
Reputation: MyRedz is an unknown quantity at this point 
Solved Threads: 0
MyRedz MyRedz is offline Offline
Light Poster

Re: need help on my inheritance here

 
0
  #3
Apr 5th, 2009
thanks i didn't realize that mistake.now it works like a charm!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC