Last active
July 31, 2016 08:48
-
-
Save ravikiran0606/de091d3e877c7881a0b28cc8f8151329 to your computer and use it in GitHub Desktop.
C++ program to implement the array of objects and pointer pointing to it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string> | |
using namespace std; | |
class student{ | |
string name; | |
int rollno; | |
public: | |
void initialize(string n,int r); | |
void display(); | |
}; | |
void student::initialize(string n,int r){ | |
name=n; | |
rollno=r; | |
} | |
void student::display(){ | |
cout<<"Student Details : \n"; | |
cout<<"Roll No : "<<rollno<<endl; | |
cout<<"Name : "<<name<<endl;; | |
} | |
int main() | |
{ | |
int n,i; | |
string nam; | |
int roll; | |
cout<<"Enter the no of students..."; | |
cin>>n; | |
student s[n]; | |
student *ptr,*p[n]; | |
cout<<"\nEnter the student details one by one.."; | |
for(i=0;i<n;i++){ | |
cout<<"\nEnter the roll no followed by name "; | |
cin>>roll>>nam; | |
s[i].initialize(nam,roll); | |
p[i]=&s[i]; | |
} | |
cout<<"\nDisplaying the Student database by using a single pointer to an array of objects"; | |
ptr=s; | |
for(i=0;i<n;i++){ | |
ptr->display(); | |
ptr++; | |
} | |
cout<<"\nDisplaying the Student database by using array of pointers\n"; | |
for(i=0;i<n;i++){ | |
p[i]->display(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment