Home > Back-end >  To the great god ask a few questions
To the great god ask a few questions

Time:10-03

1, using VC6 using MFC AppWizard generated Windows single document interface;
2, this paper analyzes some some classes in the generated code is derived, respectively what classes derived from?
3, add a new base class

The class hierarchy
{
Public:
Virtual void the draw ()=0;
//other virtual functions
};

On the basis of the design line, rectangle, ellipse derived classes, and give the related data members and member functions of derived class design and virtual functions the draw,

4, add the code in the menu and button realize draw a straight line, rectangle, oval, point straight line button, for example, can draw a straight line on the screen, the drawing code requirements in ontouch,

CodePudding user response:

Refer to this
Question asked is: define a Shape base class, and then define its derived class Circle, Rectangle,
Triangle, which requires each derived class has the function of the output area, specify to use c + + in the implementation of polymorphic,

I am so think about this question,
A, define Shape
First of all, a graphic class, will draw the graphics involved, to draw out
Consider where the graphic drawing, so we should for graphics class defines a type Point center members,
To a graphics, due to different so each type of graphic drawing, need to realize your derived class derived classes, here only define a
Pure virtual function for its derived classes to implement this function, virtual void paint (const)=0;
Second, the graphics area, need to output the area of the graph, to the output area, requires that the area, but every graphics class
Quadrature method is different, so we define a void printArea () const; Used to output the graphics area,
Define a pure virtual function virtual float area ()=0; To define Shape object is forbidden, and require it to send
Class to implement this function;

In this way, we will define the Sharp class,
The class hierarchy
{
Public:
Shape (const Point & amp; C=Point ()) : center (c) {}
Virtual void paint (const)=0;
Void printArea (const)
{
Cout & lt; }
Protected:
Virtual float area (const)=0;//pure virtual function
Private:
Point center;//save the center of the graphic
};
Second, define the Circle class

We derived from the Sharp class Circle class, only need to implement your own area function and draw graphics functions, as well as the constructor,
First of all, in order to realize those functions of the above, need to add new data to Circle class members: radius, so we add a
Float the radius; Data members,
Second: realize the constructor need: 1, define the radius, to initialize the data members 2, transmitted to the base class constructor base class
Constructors to initialize parameters is derived from the base class data members,
Then: member function area,
Finally: draw graphics member function, here only give a function declaration,

This definition in addition to the Circle class
Circle. H
The class Circle: public Shape
{
Public:
Circle (float r=0.0) : the radius (r) {}
Circle (const Point & amp; C, float r) : Shape (c), the radius (r) {}
Void paint () const;
Protected:
Float area () the const;
Private:
Float the radius;
The static const float PI=3.14 F;
};

Circle the CPP
Const float Circle: : PI;

Float Circle: : area (const)
{
Radius, return PI * * radius;
}
Void Circle: : paint (const)
{
.
}

The realization of the other classes and Circle,

When we realized after these classes, how do we use polymorphism in c + +?
C + + polymorphism is referenced by the base class pointer to a base class, we define the following function is used to c + + polymorphic
,
Int main ()
{
Shape * Shape;
Circle Circle (10);
A Rectangle Rectangle (2, 2);
Shape=& amp; Circle;
Shape. PrintArea ();
Shape=& amp; A rectangle.
Shape. PrintArea ();
return 0;
}

Program output result: 314

Through this program, you can see that we known class derived from the new class, if not add new functionality is only need to implement the constructor and
Virtual functions, if you want to implement new functions, such as graphics rotation Angle, you will need to define new member function, with
And the realization of the function of member variables,

CodePudding user response:

reference 1st floor songhtao response:
reference this
Question asked is: define a Shape base class, and then define its derived class Circle, Rectangle,
Triangle, which requires each derived class has the function of the output area, specify to use c + + in the implementation of polymorphic,

I am so think about this question,
A, define Shape
First of all, a graphic class, will draw the graphics involved, to draw out
Consider where the graphic drawing, so we should for graphics class defines a type Point center members,
To a graphics, due to different so each type of graphic drawing, need to realize your derived class derived classes, here only define a
Pure virtual function for its derived classes to implement this function, virtual void paint (const)=0;
Second, the graphics area, need to output the area of the graph, to the output area, requires that the area, but every graphics class
Quadrature method is different, so we define a void printArea () const; Used to output the graphics area,
Define a pure virtual function virtual float area ()=0; To define Shape object is forbidden, and require it to send
Class to implement this function;

In this way, we will define the Sharp class,
The class hierarchy
{
Public:
Shape (const Point & amp; C=Point ()) : center (c) {}
Virtual void paint (const)=0;
Void printArea (const)
{
Cout & lt; }
Protected:
Virtual float area (const)=0;//pure virtual function
Private:
Point center;//save the center of the graphic
};
Second, define the Circle class

We derived from the Sharp class Circle class, only need to implement your own area function and draw graphics functions, as well as the constructor,
First of all, in order to realize those functions of the above, need to add new data to Circle class members: radius, so we add a
Float the radius; Data members,
Second: realize the constructor need: 1, define the radius, to initialize the data members 2, transmitted to the base class constructor base class
Constructors to initialize parameters is derived from the base class data members,
Then: member function area,
Finally: draw graphics member function, here only give a function declaration,

This definition in addition to the Circle class
Circle. H
The class Circle: public Shape
{
Public:
Circle (float r=0.0) : the radius (r) {}
Circle (const Point & amp; C, float r) : Shape (c), the radius (r) {}
Void paint () const;
Protected:
Float area () the const;
Private:
Float the radius;
The static const float PI=3.14 F;
};

Circle the CPP
Const float Circle: : PI;

Float Circle: : area (const)
{
Radius, return PI * * radius;
}
Void Circle: : paint (const)
{
.
}

The realization of the other classes and Circle,

When we realized after these classes, how do we use polymorphism in c + +?
C + + polymorphism is referenced by the base class pointer to a base class, we define the following function is used to c + + polymorphic
,
Int main ()
{
Shape * Shape;
Circle Circle (10);
A Rectangle Rectangle (2, 2);
Shape=& amp; Circle;
Shape. PrintArea ();
Shape=& amp; A rectangle.
Shape. PrintArea ();
return 0;
}

Program output result: 314

Through this program, you can see that we known class derived from the new class, if not add new functionality is only need to implement the constructor and
Virtual functions, if you want to implement new functions, such as graphics rotation Angle, you will need to define new member function, with
And the realization of the function of member variables,
look
  • Related