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:
Class name variable name[size] ={object1,object 2,....,objectn};
Example:
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> | |
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; | |
} |
Comments
Post a Comment
Feel free to leave a comment...