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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
class my_class
{
public:
void store_info(string f, string l, int a);
void retrieve_info(string& f, string& l, int& a);
int get_age();
my_class();
my_class(string f, string l, int i);
private:
string f_name;
string l_name;
int age;
};
void my_class::store_info(string f, string l, int a)
{
f_name = f;
l_name = l;
age = a;
}
void my_class::retrieve_info(string& f, string& l, int& a)
{
cout << f << " " << l << " " << a << endl;
}
int my_class::get_age()
{
return age;
}
my_class::my_class()
{
}
my_class::my_class(string f, string l, int i)
{
store_info(f, l, i);
}
void sort(int size, my_class array[]);
int main()
{
ifstream infile;
string fname, lname;
int i =0, age, x;
infile.open("class_data_dos.txt");
my_class peeps[10];
infile >> fname >> lname >> age;
while (!infile.eof())
{
i++;
for (x = 0; x < i; x++)
{
peeps[x].store_info(fname, lname, age);
}
peeps[i].retrieve_info(fname, lname, age);
infile >> fname >> lname >> age;
}
cout << endl;
sort(i, peeps);
peeps[i].retrieve_info(fname, lname, age);
return 0;
}
void sort(int size, my_class array[])
{
int x;
my_class temp;
for (int i = 0; i < (size - 1); i++)
{
x = i;
for (int j = i; j < size; j++)
{
if (array[j].get_age() < array[x].get_age())
{
x = j;
}
}
if (x != i)
{
temp = array[x];
array[x] = array[i];
array[i] = temp;
}
}
}
|