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();
}