Klasa ze wskaźnikami

0

Witam,
Mam takie coś o:

 
int main()
{
	CFigura fig1;
	CTrojkat tr1;
	CProstokat pr1;
	CFigura *wsk = &fig1;
	wsk->przypisz();
	cout << "Pole figury: " << wsk->pole() << endl;

	wsk = &tr1;
	wsk->przypisz();
	cout << "Pole trojkata: " << wsk->pole() << endl;

	wsk = &pr1;
	wsk->przypisz();
	cout << "Pole prostokata: " << wsk->pole() << endl;

	system("pause");
	return 0;
}

No i za każdym razem osobno podaje dane dla figur, a jak zrobić, żeby prostokąt czy trójkąt wziął już dane, które są podane dla figury?
Zapewne w tym wsk->przypisz() dla trójkąta i prostokąta trzeba by coś dodać, ale coś mi nie działa jak dodaje argumenty tam.

1

to może pokaż jak te klasy i metoda przypisz wyglądają?

0
class CFigura
{
protected:
	double podstawa;
	double wysokosc;

public:
	void przypisz();
	void wypisz();
	CFigura()
	{
		podstawa = 0;
		wysokosc = 0;
	}
	virtual

	double pole()
	{
		return -1;
	}
};

class CProstokat :public CFigura, public CKolor
{
public:
	double pole()
	{
		return podstawa*wysokosc;
	}
};

class CTrojkat :public CFigura, public CKolor
{
public:
	double pole()
	{
		return (1.0 / 2)*podstawa*wysokosc;
	}
};

void CFigura::przypisz()
{
	cout << "Wysokosc: "; cin >> wysokosc;
	cout << "Podstawa: "; cin >> podstawa;
}
 
0

A może o taką strukturę Ci chodziło:

#include<iostream>
#include<string>
using namespace std;

class color{
protected:
    string rgb;

public:
    color(const string& rgb) : rgb(rgb) {}
    virtual ~color() {}
    string getColor() { return this->rgb; }
};

class shape : public color{
protected:
    string name;
    double height;
    double base;

public:
    shape(const string& name, double height, double base, const string& rgb)
        : color(rgb), name(name), height(height), base(base) {}
    virtual ~shape() {}
    virtual double calculateField() = 0;
    string getName(){ return this->name; }
};

class rectangle : public shape{
public:
    rectangle(double height, double base, const string& rgb) :
        shape("Rectangle", height, base, rgb) {}

    double calculateField(){
        return this->base * this->height;
    }
};

int main() {
    rectangle rec(12.4, 2 ,"#ff32cc");
    shape *sh = &rec;
    cout << "Name: " << sh->getName() << "\nField: " << sh->calculateField() << "\nColor: " << sh->getColor() << endl;
    return 0;
}

Twój kod nie działa, bo niepotrzebnie psujesz sobie strukturę wielodziedziczeniem. Spokojnie kształt może dziedziczyć własności od koloru, a tak naprawdę nie tyle dziedziczyć co posiadać kolor więc dziedziczenie po klasie color można spokojnie zastąpić obiektem wewnątrz klasy shape. No ale zrobiłem tak jak Ty chciałeś żeby było.

Generalnie tak niżej jest bardziej prawidłowo, gdyż kształt posiada kolor, a nie jest kolorem sam w sobie:

#include<iostream>
#include<string>
using namespace std;

struct color{
    string rgb;
    color(const string& rgb) : rgb(rgb) {}
};

class shape{
protected:
    string name;
    double height;
    double base;
    color clr;

public:
    shape(const string& name, double height, double base, const string& rgb)
        : name(name), height(height), base(base), clr(color(rgb)) {}
    virtual ~shape() {}
    virtual double calculateField() = 0;
    string getName(){ return this->name; }
    string getColor(){ return this->clr.rgb; }
};

class rectangle : public shape{
public:
    rectangle(double height, double base, const string& rgb) :
        shape("Rectangle", height, base, rgb) {}

    double calculateField(){
        return this->base * this->height;
    }
};

int main() {
    rectangle rec(12.4, 2 ,"#ff32cc");
    shape *sh = &rec;
    cout << "Name: " << sh->getName() << "\nField: " << sh->calculateField() << "\nColor: " << sh->getColor() << endl;
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1