PDA

View Full Version : ذخیره داده ها در آرایه و یا وکتور



imdeveloper
جمعه 04 دی 1394, 11:38 صبح
سلام
من یه همچین کلاسی نوشتم :


class Student
{
private:
string FullName;
bool Present;
int score;
string Date;
public:
void set_FullName(string FullName);
void Set_Present(bool Present);
void Set_Score(int Score);
void Set_Date(string Date);
string Get_FullName();
bool Get_Present();
int Get_Score();
string Get_Date();
};
void Student::set_FullName(string Name)
{
FullName = Name;
}
void Student::Set_Present(bool Pre)
{
Present = Pre;
}
void Student::Set_Score(int sco)
{
score = sco;
}
void Student::Set_Date(string Dat)
{
Date = Dat;
}
string Student::Get_FullName()
{
return FullName;
}
bool Student::Get_Present()
{
return Present;
}
int Student::Get_Score()
{
return score;
}
string Student::Get_Date()
{
return Date;
}

بعد حالا میخوام مثلا اطلاعات 10 تا دانش آموز رو توی آرایه یا وکتور نگه دارم بعد توی فایل ذخیره کنم
اما نمیدونم باید چیکار کنم الان
ممنون میشم راهنمایی کنین

zero_ox
جمعه 04 دی 1394, 16:52 عصر
سلام : به این صورت می تونید داخل وکتور ذخیره کنید

// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student
{
private:
string FullName;
bool Present;
int score;
string Date;
public:
void set_FullName(string FullName);
void Set_Present(bool Present);
void Set_Score(int Score);
void Set_Date(string Date);
string Get_FullName();
bool Get_Present();
int Get_Score();
string Get_Date();
};
void Student::set_FullName(string Name)
{
FullName = Name;
}
void Student::Set_Present(bool Pre)
{
Present = Pre;
}
void Student::Set_Score(int sco)
{
score = sco;
}
void Student::Set_Date(string Dat)
{
Date = Dat;
}
string Student::Get_FullName()
{
return FullName;
}
bool Student::Get_Present()
{
return Present;
}
int Student::Get_Score()
{
return score;
}
string Student::Get_Date()
{
return Date;
}
int main()
{
Student t,*s=new Student();
vector<Student> v;
s->set_FullName("zero_ox");
s->Get_FullName();
s->Set_Present(1);
s->Set_Score(20);
s->Set_Date("12/25/2015");
v.push_back(*s);
vector<Student>::iterator it;


for ( it = v.begin(); it != v.end(); ++it ) {
cout<<it->Get_FullName()<<endl<<it->Get_Present()<<endl<<it->Get_Score()<<endl<<it->Get_Date();
}
}

imdeveloper
جمعه 04 دی 1394, 17:56 عصر
سلام : به این صورت می تونید داخل وکتور ذخیره کنید

// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student
{
private:
string FullName;
bool Present;
int score;
string Date;
public:
void set_FullName(string FullName);
void Set_Present(bool Present);
void Set_Score(int Score);
void Set_Date(string Date);
string Get_FullName();
bool Get_Present();
int Get_Score();
string Get_Date();
};
void Student::set_FullName(string Name)
{
FullName = Name;
}
void Student::Set_Present(bool Pre)
{
Present = Pre;
}
void Student::Set_Score(int sco)
{
score = sco;
}
void Student::Set_Date(string Dat)
{
Date = Dat;
}
string Student::Get_FullName()
{
return FullName;
}
bool Student::Get_Present()
{
return Present;
}
int Student::Get_Score()
{
return score;
}
string Student::Get_Date()
{
return Date;
}
int main()
{
Student t,*s=new Student();
vector<Student> v;
s->set_FullName("zero_ox");
s->Get_FullName();
s->Set_Present(1);
s->Set_Score(20);
s->Set_Date("12/25/2015");
v.push_back(*s);
vector<Student>::iterator it;


for ( it = v.begin(); it != v.end(); ++it ) {
cout<<it->Get_FullName()<<endl<<it->Get_Present()<<endl<<it->Get_Score()<<endl<<it->Get_Date();
}
}




سلام
ممنون از پاسختون
فقط یه چیزی من الان چجوری میتونم مثلا 20تا دانش آموزش رو اطلاعاتشون رو بگیرم و ذخیره کنم؟؟

Poores
جمعه 04 دی 1394, 23:00 عصر
شما باید یک آرایه ، یا وکتور از جنس کلاستون تعریف کنین و اطلاعات هر کدوم رو مجزا در یافت کنین :


student x[20];
// ya
vector<student> x;


این از تعریف آرایه یا وکتور از جنس کلاستون


string n;
cin >> n;
x[0].set_Fullname(n);



برای مثال این یک مثال از دریافت اسم دانش آموز اول بود (از حلقه برای دریافت 20 دانش آموز استفاده کنید)

zero_ox
شنبه 05 دی 1394, 08:54 صبح
همانطور که این دوستمون گفتن می تونید از آرایه ویه حلقه استفاده کنید فقط برای گرفتن استرینگ بهتره از تابع getline به جای cin استفاده کنید