Design of a segment base class, no parameters when creating an object, it requires the user to enter the length. Similarly, its derived class is also required to enter the right-angled triangle the length of two orthogonal sides in generating object.

Source:

// right triangle in a derived class rectangle, rectangular class parameter is also input from the keyboard. These classes are designed and tested their function.

#include < iostream >

#include < cmath >

using namespace std;

class Line // base class segment

{

protected:

  double sizeA;

public:

  Line()

  {

    cout << "length of the input segment:" << endl;

    cin >> sizeA;

  }

  Line(double a)

  {

    sizeA = a;

  }

  double getLength()

  {

    return sizeA;

  }

};

class Triangle: public Line // triangle class

{

protected:

  double sizeB, sizeC;

public:

  Triangle()

  {

    cout << "Enter the length of the line:" << endl;

    cin >> sizeB;

    sizeC = sqrt(sizeB * sizeB + sizeA * sizeA);

  }

  void printSize()

  {

    cout << "right triangle, the three sides are:";

    cout << "A: " << sizeA << ", b: " << sizeB << ", C: " << sizeC << endl;

  }

};

class Rectangle: public Triangle // rectangle class

{

protected:

  double sizeD;

public:

  Rectangle()

  {

    sizeC = sizeA;

    sizeD = sizeB;

  }

  void printSize()

  {

    cout << "rectangular, four sides are:";

    cout << "A: " << sizeA << ", b: " << sizeB << ", C: " << sizeC << ", D: " << sizeD << endl;

  }

};

void main()

{

  Line *l = new Line();

  cout << "segment length:" << l-> getLength () << endl;

   Triangle *t = new Triangle();

  t->printSize();

  Rectangle *r = new Rectangle();

  r->printSize();

  system("pause");

}

 operation result:

Guess you like

Origin www.cnblogs.com/duanqibo/p/11890311.html