Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. class student{
  7. string name;
  8. int rollno;
  9.  
  10. public:
  11. void initialize(string n,int r);
  12. void display();
  13. };
  14.  
  15. void student::initialize(string n,int r){
  16. name=n;
  17. rollno=r;
  18. }
  19.  
  20. void student::display(){
  21. cout<<"Student Details : \n";
  22. cout<<"Roll No : "<<rollno<<endl;
  23. cout<<"Name : "<<name<<endl;;
  24. }
  25.  
  26. int main()
  27. {
  28. int n,i;
  29. string nam;
  30. int roll;
  31. cout<<"Enter the no of students...";
  32. cin>>n;
  33.  
  34. student s[n];
  35. student *ptr,*p[n];
  36. cout<<"\nEnter the student details one by one..";
  37. for(i=0;i<n;i++){
  38. cout<<"\nEnter the roll no followed by name ";
  39. cin>>roll>>nam;
  40. s[i].initialize(nam,roll);
  41. p[i]=&s[i];
  42. }
  43.  
  44. cout<<"\nDisplaying the Student database by using a single pointer to an array of objects";
  45. ptr=s;
  46. for(i=0;i<n;i++){
  47. ptr->display();
  48. ptr++;
  49. }
  50. cout<<"\nDisplaying the Student database by using array of pointers\n";
  51. for(i=0;i<n;i++){
  52. p[i]->display();
  53. }
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement