MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids
class CLine
{
int m_nX1;
int m_nY1;
int m_nX2;
int m_nY2;
public:
// constructors
CLine();
CLine(int x1, int y1, int x2, int y2);
// destructor
~CLine();
// set the line data
void SetPoints(int x1, int y1, int x2, int y2);
// draw the line
void Draw();
}
// this calls CLine()
CLine MyLine;
// this is a pointer to a CLine class
CLine *pMyLine;
// this calls CLine()
pMyLine = new CLine;
// this is a pointer to a CLine class
CLine *pMyLine;
// this calls CLine(int x1, int y1, int x2, int y2)
pMyLine = new CLine(0,0,10,10);
// this calls CLine(int x1, int y1, int x2, int y2)
CLine MyLine(0,0,10,10);
All of these construct a line. Some initialize it to its default settings and others copy coordinates. The 'new' keyword is used to create new things in C++, like malloc in C. You need to call 'delete' for everything you say new to, like free in C. This goes for classes as well as other data types. I could allocate an array of 100 integers with:
// a pointer to some integers
int *pNumbers;
// make memory for 100 of them
pNumbers = new int[100];
// set the first element to 0
pNumbers[0]=0;
// set the last element to 99
pNumbers[99]=99;
// free the memory.
delete [] pNumbers;
Notice the [] after the delete. This is to tell the program to delete the entire array. If you say 'delete pNumbers;' you will only free memory for the first element. You will then be 'leaking' memory. Memory leaks are when you forget to delete memory. This may end up crashing your computer if you use all the computers memory.
Sorry, let's get back to the constructors for CLine. The code for these constructor functions which automagically get called when a new line is created will look like:
CLine::CLine()
{
m_nX1=0;
m_nX2=0;
m_nY1=0;
m_nY2=0;
}
CLine::CLine(int x1, int y1, int x2, int y2)
{
m_nX1=x1;
m_nX2=x2;
m_nY1=y1;
m_nY2=y2;
}
// this is a pointer to a CLine class
CLine *pMyLine;
// this calls CLine()
pMyLine = new CLine;
// memory for the class is cleared up and ~CLine() is called
delete pMyLine;
{
// this calls CLine()
CLine MyLine;
}
// this '}' ends the section of the program where MyLine is
// valid. ~CLine() will be called. (MyLine goes out of 'scope')
For our class, ~CLine() doesn't need to do anything. However, sometimes you may want to put your cleanup code here. Like deleting any allocated memory in your class. Since we have nothing to do out function code is empty:
CLine::~CLine()
{
// do nothing
}
Let's fill in the other 2 functions.
void CLine::SetPoints(int x1, int y1, int x2, int y2)
{
m_nX1=x1;
m_nX2=x2;
m_nY1=y1;
m_nY2=y2;
return;
}
void CLine::Draw()
{
// psuedo code here, these are operating system functions to draw a line
MoveTo(m_nX1, m_nY1);
LineTo(m_nX2, m_nY2);
return;
}
CLine *pLine = new CLine(0,0,10,10);
pLine->Draw();
delete pLine;
CLine MyLine;
MyLine.SetPoints(0,0,10,10);
MyLine.Draw();
That's it for the class. Now this class can be used in other classes. You can imagine a CSquare class that has 4 Cline classes in it:
class CSquare
{
CLine m_LineTop;
CLine m_LineLeft;
CLine m_LineBottom;
CLine m_LineRight;
...
}
Or better yet, the point of all of this class stuff, you can use the CLine class to make your own class. This is done a ton in Visual C. Lets say you wanted to draw lines in your program, and you thought the line class might be nice, but it is missing an important feature, it doesn't let you set the line color. Instead of writing a whole new class, you can simple inherit the CLine class. This would look like this:
class CColorLine : public CLine
{
public:
void Draw(long color);
};
What's going on here? Well with this class we have all the functionality of our other class, but now we can use this other Draw() function which allows us to set the color. The CPP code would look like:
void CColorLine::Draw(long color)
{
// psuedo code here, these are operating system functions to draw a line
SetColor(color);
CLine::Draw();
return;
}
CColorLine MyLine;
MyLine.SetPoints(0,0,10,10);
// assuming 0 is black, this will draw a black line.
MyLine.Draw(0);