main.cpp:
Kod: Zaznacz cały
#include "dma.h"
int main(void)
{
Dma * ptr[2];
char templabel[40];
int temprating;
char tempcolor[40];
int num;
for (int i = 0; i < 2; i++)
{
cout << "podaj label ";
cin.getline(templabel, 40);
cout << "podaj 1 lub 2: ";
cin >> num;
if (num == 1)
{
cout << "podaj rating: ";
cin >> temprating;
ptr[i] = new Base(templabel, temprating);
}
if (num == 2)
{
cout << "podaj color ";
cin.getline(tempcolor, 40);
ptr[i] = new Lacks(templabel, tempcolor);
}
while (cin.get() != '
')
continue;
}
/*for (int i = 0; i < 2; i++)
{
cout << ptr[i] << endl;
}*/
cout << ptr[0];
for (int i = 0; i < 2; i++)
{
delete ptr[i];
}
cin.get();
return 0;
}
dma.cpp:
Kod: Zaznacz cały
#include "dma.h"
////////////////////////////////////////////////////
//abstrakcyjna klasa bazowa
Dma::Dma(const char * lab)
{
label = new char[strlen(lab) + 1];
strcpy_s(label, strlen(lab) + 1, lab);
}
Dma::Dma(const Dma & r)
{
delete[] label;
label = new char[strlen(r.label) + 1];
strcpy_s(label, strlen(r.label) + 1, r.label);
}
Dma::~Dma()
{
delete[] label;
}
Dma & Dma::operator=(const Dma & r)
{
if (this == &r)
return *this;
delete[] label;
label = new char[strlen(r.label) + 1];
strcpy_s(label, strlen(r.label) + 1, r.label);
return *this;
}
ostream & operator<<(ostream & os, const Dma & r)
{
os << r.label;
return os;
}
////////////////////////////////////////////////////
Base::Base(const char * lab, int r) : Dma(lab)
{
rating = r;
}
Base::Base(const Base & r) : Dma(r)
{
rating = r.rating;
}
Base & Base::operator=(const Base & r)
{
if (this == &r)
return *this;
Dma::operator=(r);
rating = r.rating;
return *this;
}
ostream & operator<<(ostream & os, const Base & r)
{
os << (const Dma &)r;
os << r.rating;
return os;
}
////////////////////////////////////////////////////
Lacks::Lacks(const char * lab, const char * c) : Dma(lab)
{
strcpy_s(color, COL_LEN, c);
}
Lacks::Lacks(const Lacks & r) : Dma(r)
{
strcpy_s(color, COL_LEN, r.color);
}
Lacks & Lacks::operator=(const Lacks & r)
{
if (this == &r)
return *this;
Dma::operator=(r);
strcpy_s(color, COL_LEN, r.color);
return *this;
}
ostream & operator<<(ostream & os, const Lacks & r)
{
os << (const Dma &)r << endl;
os << r.color;
return os;
}
dma.h:
Kod: Zaznacz cały
#ifndef DMA_
#define DMA_
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;
////////////////////////////////////////////////////
//abstrakcyjna klasa bazowa
class Dma
{
private:
char * label;
public:
Dma(const char * lab);
Dma(const Dma & r);
virtual ~Dma() = 0;
Dma & operator=(const Dma & r);
friend ostream & operator<<(ostream & os, const Dma & r);
};
////////////////////////////////////////////////////
class Base : public Dma
{
private:
int rating;
public:
Base(const char * lab, int r);
Base(const Base & r);
virtual ~Base() {}
Base & operator=(const Base & r);
friend ostream & operator<<(ostream & os, const Base & r);
};
////////////////////////////////////////////////////
class Lacks : public Dma
{
private:
enum { COL_LEN = 40 };
char color[COL_LEN];
public:
Lacks(const char * lab, const char * c);
Lacks(const Lacks & r);
virtual ~Lacks() {}
Lacks & operator=(const Lacks & r);
friend ostream & operator<<(ostream & os, const Lacks & r);
};
#endif
Edit: w pliku
main.cpp
w \(\displaystyle{ 35}\) linijce jest błąd, powinna ona wyglądać tak: cout << *(ptr[i]) << endl;
Ale pojawia się następny problem. Wyświetla mi tylko składowe z bazowej klasy.