Diameter 1 float GetSize const void SetSize float Diameter void Draw private

# Diameter 1 float getsize const void setsize float

This preview shows page 19 - 28 out of 41 pages.

Diameter = 1); float GetSize() const ; void SetSize( float Diameter); void Draw(); private : float m_Diameter; };
class CircleShape { public: CircleShape(SimpleWindow &W, float x, float y, const color &Color, float Diameter); void Draw(); color GetColor() const; void GetPosition(float &x, float &y) const; float GetSize() const; SimpleWindow& GetWindow() const; void SetColor(const color &Color); void SetPosition(float x, float y); void SetSize(float Diameter); private: SimpleWindow &Window; float XCenter; float YCenter; color Color; float Diameter; }; No Inheritance
TriangleShape::Draw() void TriangleShape::Draw() { const float Pi = 3.1415; const Position Center = GetPosition(); const float SLength = GetSideLength(); // Compute c, distance from center of triangle // to the top vertex, and a, the distance from // the center to the base of the triangle float c = SLength / (2.0 * cos(30 * Pi / 180.0)); float a = tan(30 * Pi / 180.0) * .5 * SLength;
TriangleShape::Draw() // Create an array containing the positions of // the vertices of the triangle vector<Position> TrianglePoints(3); TrianglePoints[0] = Center + Position(0, -c), TrianglePoints[1] = Center + Position(-0.5*SLength, a); TrianglePoints[2] = Center + Position( 0.5*SLength, a); // Draw the triangle GetWindow().RenderPolygon(TrianglePoints, 3, GetColor(), HasBorder()); }
Using Shapes #include "rect.h" #include "ellipse.h" #include "triangle.h" SimpleWindow Window("TestShapes", 17, 7, Position(4, 4)); int ApiMain() { Window.Open(); TriangleShape T(Window, Position(3.5, 3.5), Red, 3.0); T.Draw(); RectangleShape R(Window, Position(8.5, 3.5), Yellow, 3.0, 2.0); R.Draw(); EllipseShape E(Window, Position(13.5, 3.5), Green, 3.0, 2.0); E.Draw(); return 0; }
Fun with Shapes
Inheritance and Member Access class SomeClass { public : void MemberFunction(); int MyPublicData; protected : int MyProtectedData; private : int MyPrivateData; }; void SomeClass::MemberFunction() { MyPublicData = 1; // access allowed MyProtectedData = 2; // access allowed MyPrivateData = 3; // access allowed } void NonMemberFunction() { SomeClass C; C.MyPublicData = 1; // access allowed C.MyProtectedData = 2; // illegal C.MyPrivateData = 3; // illegal }
Inheritance and Member Access class BaseClass { public : int MyPublicData; protected : int MyProtectedData; private : int MyPrivateData; }; class DerivedClass : public BaseClass { public : void DerivedClassFunction(); // ... }; void DerivedClass::DerivedClassFunction() { MyPublicData = 1; // access allowed MyProtectedData = 2; // access allowed MyPrivateData = 3; // illegal }
Controlling Inheritance Public inheritance class DerivedClass : public BaseClass { public : // ...

#### You've reached the end of your free preview.

Want to read all 41 pages?

• Winter '03
• Pro.Lei