To moja superklasa (jest najwyżej w mojej hierarchii)
CShape.h
#ifndef CSHAPE_H
#define CSHAPE_H
#include <windows.h>
class CShape
{
private:
RECT mBoundingRect;
protected:
const RECT& mrBoundingRect;
virtual void beforeBoundingRectChange(RECT& aNewBoundingRect){}
virtual void afterBoundingRectChange(){}
public:
//Funkcje obsługi właściwości
void setBoundingRect(RECT aBoundingRect);
RECT getBoundingRect();
//Metody
virtual void Paint(HDC hdc) = 0;
//Konstruktor i destruktor
CShape();
virtual ~CShape();
};
.cpp
#include "CShape.h"
#include "Utils.h"
//Funkcje obsługi właściwości
void CShape::setBoundingRect(RECT aBoundingRect)
{
CorrectRect(&aBoundingRect);
beforeBoundingRectChange(aBoundingRect);
CorrectRect(&aBoundingRect);
mBoundingRect = aBoundingRect;
afterBoundingRectChange();
}
RECT CShape::getBoundingRect()
{
return mBoundingRect;
}
//Konstruktor i destruktor
CShape::CShape()
:
mrBoundingRect(mBoundingRect)
{
SetRect(&mBoundingRect, 50, 50, 350, 250);
}
CShape::~CShape()
{
}
Jej dzieckiem są CSimpleShape i CInscribed - o tej drugiej pózniej
CSimpleShape.h
#ifndef CSIMPLESHAPE_H
#define CSIMPLESHAPE_H
#include <windows.h>
#include "CShape.h"
class CSimpleShape : public CShape
{
private:
int mLineWidth;
COLORREF mLineColor;
COLORREF mFillColor;
HPEN mPen;
HBRUSH mBrush;
protected:
virtual void doPaint(HDC hdc) = 0;
public:
//Funkcje obsługi właściwości
void setLineWidth(int aLineWidth);
int getLineWidth();
void setLineColor(COLORREF aLineColor);
COLORREF getLineColor();
void setFillColor(COLORREF aFillColor);
COLORREF getFillColor();
//Metody
void Paint(HDC hdc);
//Konstruktor i destruktor
CSimpleShape();
virtual ~CSimpleShape();
};
.cpp
#include "CSimpleShape.h"
#include "Utils.h"
//Funkcje obsługi właściwości
void CSimpleShape::setLineWidth(int aLineWidth)
{
if(aLineWidth >= 0)
{
mLineWidth = aLineWidth;
DeleteObject(mPen);
mPen = CreatePen(PS_SOLID, mLineWidth, mLineColor);
}
}
int CSimpleShape::getLineWidth()
{
return mLineWidth;
}
void CSimpleShape::setLineColor(COLORREF aLineColor)
{
mLineColor = aLineColor;
DeleteObject(mPen);
mPen = CreatePen(PS_SOLID, mLineWidth, mLineColor);
}
COLORREF CSimpleShape::getLineColor()
{
return mLineColor;
}
void CSimpleShape::setFillColor(COLORREF aFillColor)
{
mFillColor = aFillColor;
DeleteObject(mBrush);
mBrush = CreateSolidBrush(mFillColor);
}
COLORREF CSimpleShape::getFillColor()
{
return mFillColor;
}
//Metody
void CSimpleShape::Paint(HDC hdc)
{
SelectObject(hdc, mPen);
SelectObject(hdc, mBrush);
doPaint(hdc);
}
//Konstruktor i destruktor
CSimpleShape::CSimpleShape()
{
mLineWidth = 3;
mLineColor = RGB(0,0,255);
mFillColor = RGB(255,255,0);
mPen = CreatePen(PS_SOLID, mLineWidth, mLineColor);
mBrush = CreateSolidBrush(mFillColor);
}
CSimpleShape::~CSimpleShape()
{
DeleteObject(mPen);
DeleteObject(mBrush);
}
Od CSimple odchodzą trzy klasy dzieci, wystarczy mi jak przekazać tylko jedną dlatego ograniczę się tylko do CRectangles, a pózniej CSquare
CRectangle.h
#ifndef CRECTANGLE_H
#define CRECTANGLE_H
#include "CSimpleShape.h"
class CRectangle : public CSimpleShape
{
protected:
virtual void doPaint(HDC hdc);
};
.cpp
#include "CRectangle.h"
#include "Utils.h"
void CRectangle::doPaint(HDC hdc)
{
Rectangle(hdc,
mrBoundingRect.left,
mrBoundingRect.top,
mrBoundingRect.right,
mrBoundingRect.bottom);
}
CSquare jest dzieckiem CRectangle
CSquare.h
#ifndef CSQUARE_H
#define CSQUARE_H
#include <windows.h>
#include "Utils.h"
#include "CRectangle.h"
class CSquare : public CRectangle
{
protected:
virtual void beforeBoundingRectChange(RECT& aNewBoundingRect);
public:
CSquare();
};
.cpp
#include "CSquare.h"
#include <math.h>
void CSquare::beforeBoundingRectChange(RECT& aNewBoundingRect)
{
int width, height, center;
//zakładam, że prostokąt jest już skorygowany
width = aNewBoundingRect.right - aNewBoundingRect.left;
height = aNewBoundingRect.bottom - aNewBoundingRect.top;
if(width > height)
{
center = (aNewBoundingRect.right + aNewBoundingRect.left) / 2;
aNewBoundingRect.right = center + height / 2;
aNewBoundingRect.left = center - height / 2;
}
else if(width < height)
{
center = (aNewBoundingRect.bottom + aNewBoundingRect.top) / 2;
aNewBoundingRect.top = center - width / 2;
aNewBoundingRect.bottom = center + width / 2;
}
}
CSquare::CSquare()
{
setBoundingRect(mrBoundingRect);
}
I tutaj zaczynają się schody albowiem przyszedł czas na tą nieszczęsną kompozycję :) Mam w klasie CInscribed narysować kwadrat korzystając z klasy CSquare, jednak kiedy kompletnie nie wiem jak przekazać obiekt stworzony klasie CSquare do klasy CInscribed. To moja pierwsza kompozycja, dlatego proszę o wyrozumiałość co do moich pytań. Co do klasy CInscribed proszę się nie sugerować tym co jest tam napisane - jest duże prawdopodobieństwo, że jest ona źle napisana. Kopiuję to co zrobiłem jednak to nie działa. Wiem tylko że muszę stworzyć obiekt typu CSquare w klasie CInscribed.
CInscribed.h
#ifndef CINSCRIBED_H
#define CINSCRIBED_H
#include "CCircle.h"
#include "CSquare.h"
#include "CEquilateralTriangle.h"
class CInscribed : public CShape
{
private:
CCircle mCircle1,mCircle2;
CSquare mSquare;
CEquilateralTriangle mEquilateralTriangle;
protected:
virtual void doPaint(HDC hdc);
virtual void beforeBoundingRectChange(RECT& aNewBoundingRect);
RECT getBoundingRect();
virtual void afterBoundingRectChange(){}
public:
void Paint(HDC hdc);
CInscribed();
};
#endif // CINSCRIBED_H
.cpp
#include <cstdlib>
#include <iostream>
#include "CInscribed.h"
#include "CRectangle.h"
#include "CSquare.h"
void CInscribed::Paint(HDC hdc)
{
mSquare();
doPaint(hdc);
}
CInscribed::CInscribed():mSquare(),mCircle1(),mCircle2()
{
setBoundingRect(mrBoundingRect);
}