nedeľa, 27 november 2011 17:27 Written by 2794 times
Rate this item
(1 Vote)

C++ - Zoznam studentov

Tento program napísaný v CPP vytvorí zoznam študentov. Po spustení načíta v cykle for 10 študentov.  Na začiatku si vytvoríme triedu Student. V nej si vytvoríme metódu print()ktorá vypíše zoznam študentov v poradí v akom boli zadaní. Na zoznam študentov použijeme list<Student> a nazveme ho  myStudentList;


Zdrojový kód :

#include <iostream>
#include <string>
#include <iomanip>
#include <list>

using namespace std;

//www.projectik.eu

class Student
{
  private:
    int id;         
    string meno;   
    string priezvisko;
    int vek;   

  public:             
    Student()
    {
      id = 0; meno = ""; priezvisko = ""; vek = 0;
    }   
    Student(int Id, string Meno, string Priezvisko, int Vek)
    {
      id = Id; meno = Meno; priezvisko = Priezvisko; vek = Vek;
    }
       
    int getID() {return id;}
    string getMeno() {return meno;}
    string getPriezvisko() {return priezvisko;}    
    int getVek() {return vek;}
   
    void print()
    {
       cout << setw(10) << id << setw(15) << meno
            << setw(15) << priezvisko << setw(15) << vek << endl;
    }
};
//===================================================================

int main()
{
   list<Student> myStudentList;
   list<Student>::iterator iter;
   
   int id = 0;
   string meno = "";
   string priezvisko = "";
   int vek = 0;

   for(int i=0; i<1; i++)
   {    
      cout << "Meno studenta:";
      getline(cin, meno);
      cout << "Priezvisko studenta:";
      getline(cin, priezvisko);    
      cout << "Vek: ";
      cin >> vek;
      cin.ignore(80, '\n');
      Student stu(id++, meno,  priezvisko,vek);
      myStudentList.push_back(stu);
   }
   cout << "Vypis zoznamu studentov :" << endl;
   cout << setw(10) << "id" << setw(15) << "meno"
            << setw(15) << "priezvisko" << setw(15) << "vek" << endl;
   iter = myStudentList.begin();
   while(iter != myStudentList.end())
   {
      (*iter).print();
      cout << endl;
      iter++;
   }
   cout << "www.projectik.eu" << endl;
   system("PAUSE");
   return 0;
}
Last modified on pondelok, 26 október 2015 12:32
Ing.Jaroslav Vadel

Som zakladateľom www.projectik.eu.

Hrám sa na programátora, ktorý ovláda:

c#,cpp,java,unity3d,php,html,NI testand,NI Vision Builder,Cognex In-Sight,NI LabView

"Naprogramovať program, ktorý funguje vie každy. Ale to, že program funguje ešte neznamena, že je napísany správne "

Website: www.projectik.eu