What if I have many member data in the class, is this possible?
Example I got with one member data:
#include <iostream>
#include <vector>
using namespace std;
class Demo {
double d;
public:
Demo() {
d = 0.0;
}
Demo(double x) {
d = x;
}
Demo &operator=(double x) {
d = x; return *this;
}
double getd() {
return d;
}
};
bool operator<(Demo a, Demo B)/>
{
return a.getd() < b.getd();
}
bool operator==(Demo a, Demo B)/>
{
return a.getd() == b.getd();
}
int main()
{
vector<Demo> v;
int i;
for(i = 0; i <10; i++)
v.push_back(Demo(i/3.0));
for(i = 0; i <v.size(); i++)
cout << v[ i ].getd() << " ";
cout << endl;
for(i = 0; i <v.size(); i++)
v[ i ] = v[ i ].getd() * 2.1;
for(i = 0; i <v.size(); i++)
cout << v[ i ].getd() << " ";
return 0;
}
I have tried adding more member variables and changing the parameters in the constructors but I got errors.
A class something like this, then its objects are stored in vector.
class Friend
{
public:
//constructors and destructor.
//functions here
private:
string name;
int age;
float height;
};

New Topic/Question
Reply



MultiQuote




|