debai31
//Bai31: TINH CHU VI VA DIEN TICH CAC HAM
#include<iostream>
#include<math.h>
using namespace std;
//Khai bao lop Hinh
class Hinh
{
private:
public:
virtual void nhap()=0;
virtual void hien()=0;
virtual float TinhDT()=0;
virtual float TinhCV()=0;
};
//Khai bao lop HinhCN
class HinhCN:public Hinh
{
private:
float a,b;
public:
void nhap();
void hien();
float TinhDT();
float TinhCV();
};
//Khai bao lop HinhTG
class HinhTG:public Hinh
{
private:
float a,b,c;
public:
void nhap();
void hien();
float TinhDT();
float TinhCV();
};
//Khai bao lop HinhTR
class HinhTR:public Hinh
{
private:
float r;
public:
void nhap();
void hien();
float TinhDT();
float TinhCV();
};
//==========CHUONG TRINH CHINH=======
int main()
{
Hinh *m[50];
int i,n=0;
char traloi1,traloi2,traloi3;
do
{
cout<<"CHUONG TRINH TINH ";
cout<<"
1.Nhap kich thuoc cac hinh";
cout<<"
2.Hien thi dien tich va chu cac hinh";
cout<<"
3.Ket thuc chuong trinh";
cout<<"
Nhap vao lua chon: "; cin>>traloi1;
if(traloi1=='1')
{
do
{
cout<<"
Nhap kich thuoc cho hinh nao (1-CN,2-TG,3-TR)? "; cin>>traloi2;
switch(traloi2)
{
case '1':
m[n]=new HinhCN; break;
case '2':
m[n]=new HinhTG; break;
case '3':
m[n]=new HinhTR; break;
default:
cout<<"Lua chon sai.";
break;
}
if(traloi2=='1' || traloi2=='2' || traloi2=='3') m[n++]->nhap();
cout<<"
Co nhap nua khong (c/k)? "; cin>>traloi3;
}
while(traloi3=='c' || traloi3=='C');
}
else if(traloi1=='2')
{
cout<<"Dien tich va chu vi cac hinh da nhap:
";
for(i=0;i<n;i++)
{
//if(typeid(*m[i])==typeid(HinhCN)) cout<<i+1<<".Hinh chu nhat:
";
//if(typeid(*m[i])==typeid(HinhTG)) cout<<i+1<<".Hinh tam giac:
";
//if(typeid(*m[i])==typeid(HinhTR)) cout<<i+1<<".Hinh tron:
";
m[i]->hien();
cout<<"
\tDien tich: "<<m[i]->TinhDT();
cout<<"
\tChu vi: "<<m[i]->TinhCV()<<endl;
}
}
}
while(traloi1!='3');
cout<<endl;
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top