C语言程序编写?
#include<typeinfo>
using namespace std;
const int PI=3.14;
template<typename T>
class Circle{
private:
T r;
public:
Circle(){r=0;}//无参尘迹构造函数
Circle(T x):r(x){}/尺宽/带参构造函数
void show()
{
cout<<r<<endl;
}
T area()
{
return PI*r*r;
}
};
int main()
{
/******int*****/
Circle<int> a(3);
a.show();//输出半径
cout<<a.area()<<endl;//输出面积
/***double***/
Circle<double> b(3.2);
b.show();//输出半径
cout<<b.area()<<endl;//输出面积
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
template <class T>
class Circle {
T radius;
public:
燃坦 Circle(){}
Circle(T r) : radius(r) {}
double area() { return M_PI * radius * radius; }
};
int main()
{
Circle<int> c1(2);
Circle<float> c2(2.5);
cout << c1.area() << endl;
扮段竖 cout << c2.area() <<厅大 endl;
return 0;
}
#include<iostream>
#define PI 3.1415926
using namespace std;
template<class T>
class circle
{
private:
T r;
public:
circle(T r1);
circle();
double area(); 前伍
void show()const;
};
template<class T>
circle<T>::circle(T r1)
{ r = r1;
}
template<class T>
circle<T>::circle()
{
r = 1;
}
template<class T>
double circle<T>::area()
{
return PI*r*r;
}
template<class T>
void circle<T>::show()const
{
cout << r << endl;
}
int main()
{
circle<int>c1(5);
cout << c1.area() << endl; 余指
c1.show(); 竖悔配
circle<double>c2(8.8);
cout << c2.area() << endl;
c2.show();
}