Struktury przykład

ksiazka.h

#pragma once
#include <cstring>
#include <ostream>
using namespace std;

class ksiazka
{
private:
	char autor[20];
public:
	char tytul[40];
	float cena;

public:
	ksiazka();
	ksiazka(ksiazka& k);

	char* getAutor() { return autor; }
	void  setAutor(char* autor) { strcpy_s(this->autor,20,autor); }

	ksiazka& operator=(char* autor);
	ksiazka& operator=(ksiazka const& k);

};


ostream& operator<<(ostream& s, ksiazka& k);

ksiazka.cpp

#include "ksiazka.h"

ksiazka::ksiazka()
{
	autor[0] = 0;
	tytul[0] = 0;
	cena = 0;
}

ksiazka::ksiazka(ksiazka& k)
{
	strcpy_s(this->autor, 20, k.autor);
	strcpy_s(this->tytul, 20, k.tytul);
	cena = k.cena + 1;

}

ksiazka& ksiazka::operator=(char* autor)
{
	setAutor(autor);
	return *this;
}

ksiazka& ksiazka::operator=(ksiazka const& k)
{
	strcpy_s(this->autor, 20, k.autor);
	strcpy_s(this->tytul, 20, k.tytul);
	cena = k.cena - 1;
	return *this;
}

ostream& operator<<(ostream& s, ksiazka& k)
{
	s << "Autor: " << k.getAutor() << endl;
	s << "Tytul: " << k.tytul << endl;
	s << "Cena: " << k.cena;
	s << endl;
	return s;
}

glowny.cpp

#include<iostream>
#include<iomanip>
#include<locale>
#include<climits>
#include "ksiazka.h"
using namespace std;

const size_t N = 3;
typedef unsigned char byte; //definicja nowego typu



void wczytaj(ksiazka &k)
{
	char tmp[20];
	cout << "autor: ";
	cin >> tmp;
	//k.setAutor(tmp);
	k = tmp;
}


int main(void)
{
	ksiazka b;

	wczytaj(b);

	ksiazka c = b;

	cout << b << c << endl;

	ofstream plik("textfile.txt");
	plik << b << c << endl;
	plik.close();
	
	ofstream plik_b("textfileB.txt",ios::binary);
	plik_b.write((const char*)&b, sizeof(b));
	plik_b.write((const char*)&c, sizeof(c));
	plik_b.close();

	return 0;
}