Array of Object & Pointer to Object Made by: Patel Arpita V.
 Array is a collection of variable of same data types.  Similarly, array of class objects means a collection of objects of same types.  Hence, an array having class type element is known as array of object.  syntax: Class_name object_name [size];
#include<iostream.h> #include<conio.h> class employee { int age; char name[20], post[20]; double salary; public: void getdata(); void putdata(); }; void employee::getdata() { cout<<"Name : "; cin>>name; cout<<"Age : "; cin>>age; cout<<"Post : "; cin>>post; cout<<"Salary : "; cin>>salary; } void employee::putdata() { cout<<"Name : "<<name; cout<<"nAge : "<<age; cout<<"nPost : "<<post; cout<<"nSalary : "<<salary; } int main() { int i; clrscr(); employee emp[2]; for(i=1; i<2; i++) { cout<<"Get details :-n"; emp[i].getdata(); } for(i=1; i<2; i++) { cout<<“nEmployee"<<i<<" :-n"; emp[i].putdata(); } getch(); return 0; }
 What is Pointer? -Pointer means the variable that stores the address of another variable.
 A pointer can point to an object created by class.  Example: abc ob; Here abc=class and ob=object of class abc.  we can define pointer of type abc as below: abc *ptr;  Object pointers are useful in creating objects at run time.
 We can also use an object pointer to access the public members of class.  We can access to member function of abc in two ways: 1)by using (.) dot operator ex:- (*ptr).show(); 2)by using () arrow operator ex:- ptrshow();
 Example of Pointer to object: #include<iostream.h> #include<conio.h> class abc { int a,b,ADD; public: void getdata() { cout<<"Enter the values:" ; cin>>a>>b; } void display() { ADD=a+b; cout<<"Sum="<<ADD; } }; int main() { clrscr(); abc *ob; (*ob).getdata(); ob->display(); getch(); return 0; }
 Example: abc *ptr =new abc[10]; here, abc=class ptr=pointer new=memory management operator
 Example of array of object pointer: #include<iostream.h> #include<conio.h> class abc { int code; float price; public: void getdata(int a,int b) { code=a; price=b; } void show() { cout<<"nCode:"<<code; cout<<"nPrice:"<<price; } }; const int size=2; int main() { clrscr(); abc *ptr=new abc[size]; abc *d=ptr; int x,i; float y; for(i=1;i<size;i++) { cout<<"Input the value code & price:"<<i<<"n"; cin>>x>>y; ptr->getdata(x,y); ptr++; } for(i=1;i<size;i++) { cout<<"nitem:"<<i; d->show(); d++; } getch(); return 0; }
array of object pointer in c++
array of object pointer in c++
array of object pointer in c++

array of object pointer in c++

  • 1.
    Array of Object& Pointer to Object Made by: Patel Arpita V.
  • 2.
     Array isa collection of variable of same data types.  Similarly, array of class objects means a collection of objects of same types.  Hence, an array having class type element is known as array of object.  syntax: Class_name object_name [size];
  • 3.
    #include<iostream.h> #include<conio.h> class employee { int age; charname[20], post[20]; double salary; public: void getdata(); void putdata(); }; void employee::getdata() { cout<<"Name : "; cin>>name; cout<<"Age : "; cin>>age; cout<<"Post : "; cin>>post; cout<<"Salary : "; cin>>salary; } void employee::putdata() { cout<<"Name : "<<name; cout<<"nAge : "<<age; cout<<"nPost : "<<post; cout<<"nSalary : "<<salary; } int main() { int i; clrscr(); employee emp[2]; for(i=1; i<2; i++) { cout<<"Get details :-n"; emp[i].getdata(); } for(i=1; i<2; i++) { cout<<“nEmployee"<<i<<" :-n"; emp[i].putdata(); } getch(); return 0; }
  • 5.
     What isPointer? -Pointer means the variable that stores the address of another variable.
  • 6.
     A pointercan point to an object created by class.  Example: abc ob; Here abc=class and ob=object of class abc.  we can define pointer of type abc as below: abc *ptr;  Object pointers are useful in creating objects at run time.
  • 7.
     We canalso use an object pointer to access the public members of class.  We can access to member function of abc in two ways: 1)by using (.) dot operator ex:- (*ptr).show(); 2)by using () arrow operator ex:- ptrshow();
  • 8.
     Example ofPointer to object: #include<iostream.h> #include<conio.h> class abc { int a,b,ADD; public: void getdata() { cout<<"Enter the values:" ; cin>>a>>b; } void display() { ADD=a+b; cout<<"Sum="<<ADD; } }; int main() { clrscr(); abc *ob; (*ob).getdata(); ob->display(); getch(); return 0; }
  • 10.
     Example: abc *ptr=new abc[10]; here, abc=class ptr=pointer new=memory management operator
  • 11.
     Example ofarray of object pointer: #include<iostream.h> #include<conio.h> class abc { int code; float price; public: void getdata(int a,int b) { code=a; price=b; } void show() { cout<<"nCode:"<<code; cout<<"nPrice:"<<price; } }; const int size=2; int main() { clrscr(); abc *ptr=new abc[size]; abc *d=ptr; int x,i; float y; for(i=1;i<size;i++) { cout<<"Input the value code & price:"<<i<<"n"; cin>>x>>y; ptr->getdata(x,y); ptr++; } for(i=1;i<size;i++) { cout<<"nitem:"<<i; d->show(); d++; } getch(); return 0; }