//
// main.cpp
// studentcpp
//
// Created by duanqibo on 2019/7/12.
// Copyright © 2019年 duanqibo. All rights reserved.
// 面向对象程序设计 c++
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class student
{
private:
string name;
int age;
public:
/* student(string name1,int age1)
{
name=name1;
age=age1;
}*/
string getName()
{
return name;
}
void setName(string name1)
{
name=name1;
}
int getAge()
{
return age;
}
void setAge(int age1)
{
age=age1;
}
};
int main(int argc, const char * argv[])
{
int i;
//student stu[2]={student("zhang",18),student("wang",19)};
student stu[2];
string name1;
int age1;
cout<<"请输入两个学生的信息:"<<endl;
for(i=0;i<2;i++)
{
cin>>name1>>age1;
stu[i].setName(name1);
stu[i].setAge(age1);
}
for(i=0;i<2;i++)
{
cout<<"姓名:"<<"\t"<<stu[i].getName();
cout<<"年龄:"<<"\t"<<stu[i].getAge()<<endl;
}
//以下将学生信息存入文本文件
ofstream f;
f.open("d:\\student.txt",ios::out);
f<<"姓名"<<"\t"<<"年龄"<<endl;
f<<"-----------"<<endl;
for(i=0;i<2;i++)
{
f<<stu[i].getName()<<"\t"<<stu[i].getAge()<<endl;
}
return 1;
}
运行结果: