Xây dựng lớp tam giác trong C++

Yêu cầu:

Chúng ta cần xây dựng các lớp mô tả đa giác : hình chữ nhật và tam giác. Hai đa giác này có đặc điểm cơ bản, như là chiều rộng và chiều cao để tính diện tích. Do vậy, ta có thể xây dựng 1 lớp đa giác (polygons) gồm các thành viên: width và height. Ta sẽ tạo ra 2 lớp dẫn xuất: lớp tam giác (CTriangle) và lớp hình chữ nhật (CRectangle) kế thừa từ lớp đa giác (CPolygon).

/************************************************************ * Author: VNCODING * History * 2014/11/28 first create VNCODING *************************************************************/ #include using namespace std; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return width * height; } }; class CTriangle: public CPolygon { public: int area () { return width * height / 2; } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << "Area of rectangle: " << rect.area() << endl; cout << "Area of triangle: " << trgl.area() << endl; system("pause"); return 0; }

Chú ý:

Ở đây, thành viên width và height được khai báo protected thay vì private. Vì thành viên của lớp dẫn xuất chỉ có thể truy cập được thành viên public và protected của lớp cơ sở.
Trong ví dụ này, thành viên của lớp CRectangle và CTriangle có cùng quyền truy cập như trong lớp cơ cở CPolygon.

CPolygon::width // protected access CRectangle::width // protected access CPolygon::set_values() // public access CRectangle::set_values() // public access

Đó là bởi vì, trong ví dụ này ta dùng kiểu kế thừa public.

class Rectangle: public Polygon { /* ... */ }

Kết quả:

Xây dựng lớp tam giác trong C++
Xây dựng lớp tam giác và hình chữ nhật kế thừa từ lớp đa giác

  • bài tập lập trình C/C++
  • hướng đối tượng C++

Xây dựng lớp tam giác trong C++


Viết định nghĩa lớp TamGiac để biểu diễn khái niệm tam giác trong mặt phẳng với các phương thức thiết lập, huỷ bỏ (nếu có). Các hàm thành phần nhập, xuất, tịnh tiến, quay, phóng to, thu nhỏ và vẽ tam giác (tùy chọn).

Tịnh tiến theo vector v (a,b) : x’= x +a; y’=y+b;

Phép quay tâm O, góc quay α

x’ = xcosα – y sinα;


y’= xsinα  + ycosα;

diem.h

#pragma once

#include

using namespace std;

class diem

{

private:

float x,y;

public:

diem();

diem(float,float);

~diem(void);

void nhap();

void xuat();

void setx(float);

void sety(float);

diem vector(diem);

float getx();

float gety();

void setxy(float,float);

void tinhtien(float=0,float=0);

void quay(float);

};

tamgiac.h

#pragma once

#include"diem.h"

class tamgiac

{

private:

diem A,B,C;

public:

tamgiac();

tamgiac(diem,diem,diem);

~tamgiac(void);

void nhap();

void xuat();

void tinhtien(float=0,float=0);

void quay(float);

void thuphong(float);

};

diem.cpp

#include "diem.h"

diem::diem()

{

}

void diem::setx(float a)

{

x=a;

}

void diem::sety(float a)

{

y=a;

}

float diem::getx()

{

return x;

}

float diem::gety()

{

return y;

}

void diem::setxy(float a,float b)

{

x=a;

y=b;

}

diem::diem(float a,float b)

{

x=a;y=b;

}

void diem::nhap()

{

cout<<"\nhoanh do x=";cin>>x;

cout<<"tung do y=";cin>>y;

}

void diem::xuat()

{

cout<<"( "<

}

diem::~diem(void)

{

}

void diem::tinhtien(float a,float b)

{

x+=a;

y+=b;

}

void diem::quay(float rad)

{

x=x*cos(rad)+y*sin(rad);

y=x*sin(rad)+y*cos(rad);

}

diem diem::vector(diem a)

{

diem t;

t.setx(a.getx()-x);

t.sety(a.gety()-y);

return t;

}

tamgiac.cpp

#include "tamgiac.h"

tamgiac::tamgiac()

{

}

tamgiac::tamgiac(diem x,diem y,diem z)

{

A=x;B=y;C=z;

}

tamgiac::~tamgiac(void)

{

}

void tamgiac::nhap()

{

int kt;

do{

kt=0;

cout<<"\nnhap dinh A";

A.nhap();

cout<<"\nnhap dinh B";

B.nhap();

cout<<"\nnhap dinh C";

C.nhap();

diem d,e;

d=A.vector(B);

e=A.vector(C);

if(float(d.getx()*e.gety())==float(d.gety()*e.getx()))

{

kt=1;

cout<<"\ntam giac khong ton tai! nhap lai!";

}

cout<<"\n";

}while(kt);

}

void tamgiac::xuat()

{

cout<<"\ntam giac tao boi 3 diem: ";

A.xuat();cout<<" , ";B.xuat();cout<<" , ";C.xuat();

}

void tamgiac::tinhtien(float m,float n)

{

A.tinhtien(m,n);

B.tinhtien(m,n);

C.tinhtien(m,n);

}

void tamgiac::quay(float rad)

{

A.quay(rad);

B.quay(rad);

C.quay(rad);

}

void tamgiac::thuphong(float k)

{

A.setxy(A.getx()*k,A.gety()*k);

B.setxy(B.getx()*k,B.gety()*k);

C.setxy(C.getx()*k,C.gety()*k);

}

main.cpp

#include"tamgiac.h"

void main()

{

float x,y,rad,k;

tamgiac a;

a.nhap();

a.xuat();

cout<<"\n\ntinh tien theo vector v(x,y):\nx=";cin>>x;

cout<<"y=";cin>>y;

a.tinhtien(x,y);

a.xuat();

cout<<"\nquay goc: rad=";cin>>rad;

a.quay(rad);

a.xuat();

cout<<"\nnhap he so thu phong k=";cin>>k;

a.thuphong(k);

a.xuat();

system("pause");

}