Tuesday, June 24, 2014

Thực hành 12 - Bài 1 - Hình


Đề bài : ThucHanh12_Class_TinhChat_KeThua.pdf


Viết chương trình định nghĩa các lớp Shape (biểu diễn 1 hình trong hình học phẳng), Triangel (biểu diễn tam giác) , Rectangel (biểu diễn hình chữ nhật) , Square (biểu diễn hình vuông) , Circle (biểu diễn hình tròn) . Biết rằng giữa chúng có mối quan hệ thừa kế như sau :

                           Shape

 Triangle            Rectangle           Circle

                           Square
______________________________________________________________________


Code : http://pastebin.com/cr66vbeA  hoặc  Shape.cpp

_______________________________________________________________________


#include <iostream>

#include <cstdlib>

#include <cmath>

using namespace std;


class Shape {

public:
Shape();
double getArea();
double getPerimeter();
void draw();

protected:
};

class Circle : public Shape {

public:
Circle(double = 0.0);
double getRadius();
double getArea();
double getPerimeter();
void draw();

private:
double radius;
};



class Triangle : public Shape {

public:
Triangle(double = 0.0, double = 0.0, double = 0.0);
double getArea();
double getPerimeter();
void draw();

private:
double a;
double b;
double c;
};



class Rectangle : public Shape {

public:

Rectangle(double = 0.0, double = 0.0);
double getLength();
double getWidth();
double getArea();
double getPerimeter();
void draw();

protected:
double length;
double width;

private:
double a;
double b;
};



class Square : public Rectangle {

public:

Square(double = 0.0);
void draw();

private:
double length;
double width;
};



int main()
{

Shape s;
Triangle t(3, 4, 5);
Rectangle r(5, 2);
Circle c(6);
Square sq(r.getLength());
cout << s.getArea() << "\t" << s.getPerimeter() << endl;
s.draw(); cout << endl;
cout << t.getArea() << "\t" << t.getPerimeter() << endl;
t.draw(); cout << endl;
cout << r.getLength() << "\t" << r.getWidth() << "\t" << r.getArea() << "\t" << r.getPerimeter() << endl;
r.draw(); cout << endl;
cout << c.getRadius() << "\t" << c.getArea() << "\t" << c.getPerimeter() << endl;
c.draw(); cout << endl;
cout << sq.getLength() << "\t" << sq.getWidth() << "\t" << sq.getArea() << "\t" << sq.getPerimeter() << endl;
sq.draw(); cout << endl;
system("PAUSE");
return 0;
}



Shape::Shape()
{
}

double Shape::getArea() {return -1;}

double Shape::getPerimeter() {return -1;}

void Shape::draw() {cout << "Ve hinh";}

Triangle::Triangle(double a, double b, double c)

: a(a), b(b), c(c)
{

}

double Triangle::getArea()

{
double HOP = getPerimeter() / 2.0; // HOP = Half of Perimeter
return sqrt(HOP * (HOP - a) * (HOP - b) * (HOP - c));
}


double Triangle::getPerimeter()
{
return (a + b + c);
}


void Triangle::draw()
{
cout << "Ve hinh tam giac voi do dai 3 canh la " << a << ", " << b << ", " << c;
}


Rectangle::Rectangle(double a, double b)

: a(a), b(b)

{
length = getLength();
width = getWidth();
}


double Rectangle::getLength()
{
if (a > b) return a;
else return b;
}



double Rectangle::getWidth()
{
if (a < b) return a;
else return b;
}


double Rectangle::getArea() {return (length * width);}


double Rectangle::getPerimeter() {return (2 * (length + width));}


void Rectangle::draw()
{
cout << "Ve hinh chu nhat voi chieu dai " << length << " va chieu rong " << width;
}


Circle::Circle(double radius)

 : radius(radius)

{
}

double Circle::getRadius() {return radius;}

double Circle::getArea() {return (radius * radius * 3.14);}

double Circle::getPerimeter() {return (2 * radius * 3.14);}

void Circle::draw()
{
cout << "Ve hinh tron voi ban kinh " << radius;
}

Square::Square(double length)

 : length(length)
{
width = length;
}

void Square::draw()
{
cout << "Ve hinh vuong chieu dai canh la " << length;
}

BACK TO TOP