You instantiate classes like this one:
class Data{
private:
int id;
string name;
public:
Data(int,string);
void setId(int);
void setName(string);
int getId();
string getName();
};
Data::Data(int d=0,string n="default"):id(d),name(n){
}
void Data::setId(int d=0){
id = d;
}
void Data::setName(string nm="default"){
name = nm;
}
int Data::getId(){
return id;
}
string Data::getName(){
return name;
}
Then after the above declaration, you can use:
Data data(8,"lady gaga");
cout << data.getId() << endl;
cout << data.getName() << endl;
Or you can use:
Data data; // without bracketdata.setId(45);
data.setName("Luna Maya");
No comments:
Post a Comment