Object Array in C++

Object array can be made in C++ by the following syntax:

 Class name variable name[size] ={object1,object 2,....,objectn};


Example:


#include<iostream>
using namespace std;
class Hello{
int age;
public:
Hello(){
age=0;
}
Hello(int age){
this->age=age;
}
static void check(Hello *h){
int largest=h[0].age;
for(int i=0;i<sizeof(h)/sizeof(h[0]);i++){
if(largest>h[i+1].age){
}
else{
largest=h[i+1].age;
}
}
cout<<"Largest age is "<<largest<<endl;
}
};
int main() {
Hello h1(24),h2(69),h3(23),h4(4);
Hello h[4]={h1,h2,h3,h4};
Hello::check(h);
return 0;
}
view raw ObjectArray.cpp hosted with ❤ by GitHub

Comments

Popular posts from this blog

C++ programming toll plaza simulation program