C++构造函数不能有返回类型
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/25 18:29:44
C++构造函数不能有返回类型
定义一个圆柱体类模版,数据成员有圆柱体底半径,圆柱体高,成员函数有带参数构造函数,求圆柱体体积函数,求圆柱体表面积函数,要求成员函数在类外定义,主程序测试时实例化为float和double型,半径和高从键盘输入
下面是我的代码:
#include
using namespace std;
#define PI 3.14
template
class Cylinder
{
public:
Cylinder(T r,T h);
T area();
T volume();
protected:
T radius,height;
};
------------------------------
#include "Cylinder.h"
template
T Cylinder::Cylinder(T r,T h)
{
radius=r;
height=h;
}
template
T Cylinder::area()
{
return ((PI*radius*radius*2)+(2*PI*radius*height));
}
template
T Cylinder::volume()
{
return (PI*radius*radius*height);
}
-----------------------------
#include "Cylinder.h"
int main()
{
int r,h;
cin>>r>>h;
Cylinder cy(r,h);
cout
定义一个圆柱体类模版,数据成员有圆柱体底半径,圆柱体高,成员函数有带参数构造函数,求圆柱体体积函数,求圆柱体表面积函数,要求成员函数在类外定义,主程序测试时实例化为float和double型,半径和高从键盘输入
下面是我的代码:
#include
using namespace std;
#define PI 3.14
template
class Cylinder
{
public:
Cylinder(T r,T h);
T area();
T volume();
protected:
T radius,height;
};
------------------------------
#include "Cylinder.h"
template
T Cylinder::Cylinder(T r,T h)
{
radius=r;
height=h;
}
template
T Cylinder::area()
{
return ((PI*radius*radius*2)+(2*PI*radius*height));
}
template
T Cylinder::volume()
{
return (PI*radius*radius*height);
}
-----------------------------
#include "Cylinder.h"
int main()
{
int r,h;
cin>>r>>h;
Cylinder cy(r,h);
cout
模板类的定义要放在一个文件中,不能分开来定义.
第二
template<class T>T Cylinder<T>::Cylinder(T r,T h){ //class T后多了个T
//Cylinder.h
#include <iostream>
using namespace std;
#define PI 3.14
template <class T> class Cylinder{
public:
Cylinder(T r,T h);
T area();
T volume();
protected:
T radius,height;
};
template<class T> Cylinder<T>::Cylinder(T r,T h){ //class T后多了个T
radius=r;
height=h;
}
template<class T>T Cylinder<T>::area(){
return ((PI*radius*radius*2)+(2*PI*radius*height));
}
template<class T>T Cylinder<T>::volume(){
return (PI*radius*radius*height);
}
//main.cpp
#include "Cylinder.h"
int main(){
int r,h;
cin>>r>>h;
Cylinder <float> cy(r,h);
cout<<cy.area()<<endl;
cout<<cy.volume()<<endl;
cin>>r>>h;
Cylinder <double> cy2(r,h);
cout<<cy2.area()<<endl;
cout<<cy2.volume()<<endl;
return 0;
}
第二
template<class T>T Cylinder<T>::Cylinder(T r,T h){ //class T后多了个T
//Cylinder.h
#include <iostream>
using namespace std;
#define PI 3.14
template <class T> class Cylinder{
public:
Cylinder(T r,T h);
T area();
T volume();
protected:
T radius,height;
};
template<class T> Cylinder<T>::Cylinder(T r,T h){ //class T后多了个T
radius=r;
height=h;
}
template<class T>T Cylinder<T>::area(){
return ((PI*radius*radius*2)+(2*PI*radius*height));
}
template<class T>T Cylinder<T>::volume(){
return (PI*radius*radius*height);
}
//main.cpp
#include "Cylinder.h"
int main(){
int r,h;
cin>>r>>h;
Cylinder <float> cy(r,h);
cout<<cy.area()<<endl;
cout<<cy.volume()<<endl;
cin>>r>>h;
Cylinder <double> cy2(r,h);
cout<<cy2.area()<<endl;
cout<<cy2.volume()<<endl;
return 0;
}
C++构造函数不能有返回类型
c#构造函数没有显示的返回类型什么意思
函数必须有返回值,否则不能使用函数?
C语言中的返回类型有哪些?分别什么含义?
C#中 ” 构造函数可以有参数,也可以有返回值“ 这句话对吗?
C++定义的函数不能有返回值,晕
为何构造函数不能成为虚函数?
构造函数为什么不能是虚函数
1.C语言函数默认的返回值类型是_____.2.下列条件:10
c语言 有参有返回值函数 1..为什么要使用函数?2.函数分哪些类型3.函数的参数与返回值.
什么是构造地貌,构造地貌的类型有哪些?
在C语言中,函数返回值的类型最终取决于( ) A函数定义时再函数首部所说明的函数类型 Breturn语句中表达式