Создал структуру "Студент". Определил конструктор для инициализации полей структуры со
значениями по умолчанию. Определил конструктор копирования и деструктор.
#include <iostream>
using namespace std;
struct Student{
char Sirname[255];
char Name[255];
char Patronymic[255];
int Year;
char Team[255];
Student(const char *sn, const char *n, const char *p, int y,const char *t)
{ strcpy(Sirname,sn );
strcpy(Name,n);
strcpy(Patronymic,p);
Year=y;
strcpy(Team,t);
cout<<"Constructor On"<<endl;
};
Student(const Student &st)
{ strcpy(Sirname,st.Sirname );
strcpy(Name,st.Name);
strcpy(Patronymic,st.Patronymic);
Year=st.Year;
strcpy(Team,st.Team);
cout<<"Copy-constructor On"<<endl;
};
~Student()
{ cout<<"Destructor On"<<endl;
};
int Test1(const char *p)
{
};
void Test2()
{
};
};
int main()
{ cout<<"Enter function main()"<<endl;
Student st("Ivanov", "Ivan", "Ivanovich",1989,"AKB21"), st1(st);
cout << st.Sirname << " " << st.Name << " " << st.Patronymic << " " << st.Year << " "<<st.Team<<endl;
cout << st1.Sirname << " " << st1.Name << " " << st1.Patronymic << " " << st1.Year << " "<<st1.Team<<endl;
cout<<"Exit function main()"<<endl;
return 0;
}
ostream& operator << (ostream& os, Student& st) {для своего класса, и не мучайся...
cout <<
st.Surname << " " << st.Name << " " << st.Patronymic << " " <<
st.Year << " " <<st.Team;
return os;
}
ostream& operator << (ostream& os, Student& st) {для своего класса, и не мучайся...
cout <<
st.Surname << " " << st.Name << " " << st.Patronymic << " " <<
st.Year << " " <<st.Team;
return os;
}
Это перегрузка операции вставки вывода в поток...
Вот так?
#include <iostream>
using namespace std;
struct Student {
// на всякий случай делаем другом, если перенесешь поля в private секцию...
friend ostream& operator << (ostream&, Student&);
Student(const char *sn, const char *n, const char *p, int y, const char *t) {
strcpy(Surname, sn); strcpy(Name, n); strcpy(Patronymic, p);
Year = y;
strcpy(Team,t);
cout<<"Constructor On"<<endl;
}
Student(Student &st) {
init(st);
cout << "X(X&) constructor" << endl;
}
~Student()
{
cout<<"Destructor On"<<endl;
}
// вот оператор присваивания
Student& operator = (const Student &st) {
init(st);
return *this;
}
// вот тестовая функция
void Test() {
Patronymic[0] = 'Y';
}
char Surname[255];
char Name[255];
char Patronymic[255];
int Year;
char Team[255];
private:
void init(const Student& st) {
strcpy(Surname, st.Surname );
strcpy(Name, st.Name);
strcpy(Patronymic, st.Patronymic);
Year = st.Year;
strcpy(Team, st.Team);
}
};
ostream& operator << (ostream& os, Student& st) {
cout <<
st.Surname << " " << st.Name << " " << st.Patronymic << " " <<
st.Year << " " <<st.Team;
return os;
}
int main(int argc, char *argv[])
{
Student st("Ivanov", "Petr", "Alekseevich", 1989, "AKB21"), st1(st);
cout << st << endl;
st.Test();
cout << st << endl;
cout << st1 << endl;
return 0;
}
#include <iostream>
using namespace std;
struct Student {
// на всякий случай делаем другом, если перенесешь поля в private секцию...
friend ostream& operator << (ostream&, Student&);
Student(const char *sn, const char *n, const char *p, int y, const char *t) {
strcpy(Surname, sn); strcpy(Name, n); strcpy(Patronymic, p);
Year = y;
strcpy(Team,t);
cout<<"Constructor On"<<endl;
}
Student(Student &st) {
init(st);
cout << "X(X&) constructor" << endl;
}
~Student()
{
cout<<"Destructor On"<<endl;
}
// вот оператор присваивания
Student& operator = (const Student &st) {
init(st);
return *this;
}
// вот тестовая функция
void Test() {
Patronymic[0] = 'Y';
}
char Surname[255];
char Name[255];
char Patronymic[255];
int Year;
char Team[255];
private:
void init(const Student& st) {
strcpy(Surname, st.Surname );
strcpy(Name, st.Name);
strcpy(Patronymic, st.Patronymic);
Year = st.Year;
strcpy(Team, st.Team);
}
};
ostream& operator << (ostream& os, Student& st) {
cout <<
st.Surname << " " << st.Name << " " << st.Patronymic << " " <<
st.Year << " " <<st.Team;
return os;
}
int main(int argc, char *argv[])
{
Student st("Ivanov", "Petr", "Alekseevich", 1989, "AKB21"), st1(st);
cout << st << endl;
st.Test();
cout << st << endl;
cout << st1 << endl;
return 0;
}