c++day7

1.XMIND

2.

试编程:

封装一个学生的类,定义一个学生这样类的vector容器, 里面存放学生对象(至少3个)

再把该容器中的对象,保存到文件中。

再把这些学生从文件中读取出来,放入另一个容器中并且遍历输出该容器里的学生。

#include 
#include 
#include 


using namespace std;

//封装 学生 类

class Student
{
public:
    string name;
    int age;
    string speak;
public:
    Student(){}
    Student(string name, int age, string speak):name(name),age(age),speak(speak)
    {}
};

int main()
{
    //创建三个类对象
    Student s1("北原春希",18,"你们都是我的翅膀");
    Student s2("小木曾雪菜",17,"为什么会变成这样呢……第一次有了喜欢的人。有了能做一辈子朋友的人。两件快乐事情重合在一起。而这两份快乐,又给我带来更多的快乐。得到的,本该是像梦境一般幸福的时间……但是,为什么,会变成这样呢……");
    Student s3("冬马和纱",17,"是我,是我先,明明都是我先来的……接吻也好,拥抱也好,还是喜欢上那家伙也好");

    //创建一个类的容器,存储类对象
    vector v;
    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);

    //把容器中的对象保存在文件中
    ofstream ofs;
    ofs.open("D:/QTfile/Student.txt",ios::out);
    vector::iterator i;
    for(i=v.begin();i!=v.end();i++)
    {
        ofs << i->name << "  " << i->age << ":"<< i->speak < new_v;
    ifstream ifs;
    ifs.open("D:/QTfile/Student.txt",ios::in);
    string name;
    int age;
    string speak;
    while(ifs >> name >> age >> speak)
    {
        new_v.push_back(Student(name,age,speak));
    }
    ifs.close();

    //遍历新容器中的对象
    vector::iterator new_i;
    for(new_i=new_v.begin();new_i!=new_v.end();new_i++)
    {
        cout<< new_i->name << "  " << new_i->age  << "  " << new_i->speak << endl << endl;
    }

    return 0;
}

你可能感兴趣的:(c++,开发语言)