Kol W1 (2)


// Kol_1_W_1.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

using namespace std;

void crash(char *str)

{

cout << str;

system("pause");

exit(1);

}

class student

{

char *name;

char *surname;

int birth_year;

public:

student(char *nm, char *snm, int b_y);

student(const student &ob);

~student();

void disp();

private:

void alloc(size_t sz1, size_t sz2);

};

student::student(char *nm, char *snm, int b_y) : birth_year(b_y)

{

size_t len_name = strlen(nm)+1;

size_t len_surn = strlen(snm)+1;

alloc(len_name, len_surn);

strcpy_s(name, len_name*sizeof(char), nm);

strcpy_s(surname, len_surn*sizeof(char), snm);

}

student::student(const student &ob)

{

birth_year = ob.birth_year;

size_t len_name = strlen(ob.name)+1;

size_t len_surn = strlen(ob.surname)+1;

alloc(len_name, len_surn);

strcpy_s(name, len_name*sizeof(char), ob.name);

strcpy_s(surname, len_surn*sizeof(char), ob.surname);

}

student::~student()

{

delete [] name;

delete [] surname;

}

void student::alloc(size_t sz1, size_t sz2)

{

try

{

name = new char [sz1];

surname = new char [sz2];

}

catch(bad_alloc)

{

crash("mem alloc err");

}

}

void student::disp()

{

cout << name << "\t" << surname << "\t" << birth_year << endl;

}

void fun_disp(student st)

{

st.disp();

}

int _tmain(int argc, _TCHAR* argv[])

{

student *pst[3];

try

{

pst[0] = new student ("Andrzej", "Golota", 1991);

pst[1] = new student ("Agata", "Cristi", 1990);

pst[2] = new student ("Michal", "Wasilewski", 1992);

}

catch(bad_alloc)

{

crash("mem alloc err");

}

for(int i=0; i<3; ++i)

fun_disp(*pst[i]);

for(int i=0; i<3; ++i)

{

delete pst[i];

pst[i] = NULL;

}

system("pause");

return 0;

}



Wyszukiwarka

Podobne podstrony:
Kol W1, studia Polibuda Informatyka, III semestr, języki paradygmaty programowania (jipp), kolos 2
Farmakologia pokazy, Podstawy Farmakologii Ogólnej (W1)
W1 wprow
Przygotowanie PRODUKCJI 2009 w1
w1 czym jest psychologia
PD W1 Wprowadzenie do PD(2010 10 02) 1 1
wde w1
Finanse W1
Solid Edge Generator kół zębatych
W1 ZLO
AM1 2005 W1
w1

więcej podobnych podstron