//witam mam o to taki problem że przy niszczeniu obiektow program sie wykrzacza a //dokładnie przy copy.report() pojawia mi się taki oto błąd.
#pragma once
#include<cstring>
using namespace std;
class CD
{
char performers[50];
char label[20];
int selections;
double playtime;
public:
CD(const char* s1 = "nic", const char* s2 = "nic", int n = 0, double x = 0);
virtual ~CD();
virtual void Report() const;
virtual CD& operator=(const CD& D);
};
class Classic : public CD
{
private:
char* nazwa;
public:
Classic()
{
nazwa = new char[sizeof("disik")];
strcpy(nazwa, "disik");
};
Classic(const char* s1, const char* s2, int n, double x, const char* dis="disik");
virtual void Report() const;
virtual Classic& operator=(const Classic& D);
~Classic();
};
// główny main
#include"iostream"
#include"zad1.h"
using namespace std;
void Bravo(const CD& disk)
{
disk.Report();
}
int main()
{
CD c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Sonata fortepianowa B-dur,Fantazja C-moll", "Alfred Brendel", 2, 57.17, "Philips");
CD* pcd = &c1;
cout << "Bezposrednie uzycie obiektow:\n";
c1.Report();
c2.Report();
cout << "uzycie wskaznika na typ cD:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "wywolywanie funkcji z argumentem w postaci referencji do typu CD:\n";
Bravo(c1);
Bravo(c2);
cout << "test przypisania: ";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
//definicje metod
#include"zad1.h"
#include<iostream>
#include<cstring>
using namespace std;
CD::CD(const char* s1, const char* s2, int n, double x)
{
strncpy(performers, s1, 48);
performers[49] = '\0';
strncpy(label, s2, 18);
label[19] = '\0';
selections = n;
playtime = x;
}
CD::~CD()
{
}
void CD::Report() const
{
cout << "autor: " << performers << endl;
cout << "nazwa plyty: " << label << endl;
cout << "liczba utworow: " << selections << endl;
cout << "dlugosc plyty: " << playtime << endl;
}
Classic::Classic(const char* s1, const char* s2, int n, double x, const char* dis) : CD(s1, s2, n, x)
{
nazwa = new char[sizeof(dis)];
strcpy(nazwa, dis);
}
void Classic::Report() const
{
CD::Report();
cout << nazwa << endl;
}
CD& CD::operator=(const CD& a)
{
if (this == &a)
return *this;
strcpy(performers, a.performers);
strcpy(label, a.label);
selections = a.selections;
playtime = a.playtime;
return *this;
}
Classic& Classic::operator=(const Classic& a)
{
if (this == &a)
return *this;
CD::operator=(a);
delete [] nazwa;
nazwa = new char[sizeof(a.nazwa)];
strcpy(nazwa, a.nazwa);
return *this;
}
Classic::~Classic()
{
delete [] nazwa;
}