need help

hi all, i have this code . i want to generate spical id for each obj,but here it does not work.so please any help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<string>
using namespace std;
class pat{
string name;
float age;
static int id;
string doctor_name;
public:
	pat(string n,float a,string doc){
name=n;
age=a;
doctor_name=doc;
id++;
}
string getname()const{return name;}
void setname(string n){name=n;}
float getage()const{return age;}
void setage(float a){age=a;}
static int getid(){return ++id;}
string getdoctor_name()const{return doctor_name;}
void setdoctor_name(string doc){doctor_name=doc;}

void print()const{
cout<<"the name of pastion is "<<name<<endl;
cout<<"the age of pastiont is "<<age<<endl;
cout<<"the id of pastiont is "<<id<<endl;
cout<<"the name of doctor is "<<doctor_name<<endl;
}
};
int pat::id=0;
int main(){
pat obj("turky",22,"mohesn");
pat obj2("sowsan",33,"mohesn");
pat obj4("dana",11,"mohesn");
obj.print();
cout<<endl;
obj2.print();
cout<<endl;
obj4.print();

system("pause");
return 0;
}
There is exactly one instance of pat::id in the entire program. It is similar to a global variable.

What you do need in addition to pat::id is a regular member variable where each pat object has their very own unique id.


PS. Could you indent your code. That makes it easier to read.
i dont understand what you said very well, so where i have to change to make it work?
1
2
3
4
5
6
7
8
class foo {
  static unsigned int nextid;
  const unsigned int id;
public:
  foo() : id( nextid++ ) {}
};

unsigned int foo::nextid {0};
Topic archived. No new replies allowed.