#include<iostream.h>
#include<conio.h>
class add
{
private:
int x,y; // Data Member
public:
add(int p,int q) // Default Parameter Constructor
{
x=p;
y=q;
}
void display()
{
int r;
r=x+y;
cout<<"\n NAswer="<<r;
}
};
main()
{
clrscr();
int i;
add a[3]={add(2,4),add(5,7),add(9,10)};
for(i=0;i<3;i++)
{
a[i].display();
}
getch();
}
---------------------------------------------------------------
and declare an array of three phonerecs, phonerec subscribers[3];
then this will call the default constructor three times, once on each object and set the name field of each to "Me" etc.
The example below uses an initializer list that constructs each object in an array of 10 objects, and calls a function to set each object to have a different id value. After the array has been constructed, A[0]has an id of 0, A[1] an id of 1 etc. #include <string> using namespace std; int x=0; int getX() { return x++; } class a { public: int b; int id; string s; a() : id(getX()), b(5), s("value2") { } }; void main() { a A[10]; } a A[10];
This can also be done by using a static data member of a class. We'll see those in a future lesson.
No comments:
Post a Comment