列表

详情


CPP65. 构建长方体类

描述

现在有长方形类(rectangle),请以此为基类构建长方体类(cuboid)并实现求体积的getvolume方法。

输入描述

输入三个整数分别表示长宽高。

输出描述

输出长方体的体积。

示例1

输入:

3 2 1

输出:

6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-08-05

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
        cuboid(int l, int w, int h): rectangle(l, w), height(h){}
        int getvolume()
        {
            return area() * height;
        }
		
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.getvolume();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-08-03

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
     void set(int z){
			height=z;
     }
    public:
		cuboid(int x,int y,int z):rectangle(x,y){
              this->height = z;}
      int getvolume(){
        return this->area()*this->height;}
         
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.getvolume();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-08-02

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
		cuboid(int x,int y,int h) :rectangle(x, y){
            height=h;
        }
        int getvolume(){
            return rectangle::area()*height;
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.getvolume();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-08-01

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
	  cuboid(int length,int width,int height ):	rectangle(length,width){
          this->height=height;
      }
   int getvolume(){
       return this->height*this->area(); 
    }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.getvolume();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-07-28

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
        cuboid(int _length, int _width, int _height) : rectangle(_length, _width), height(_height) {}
        int getvolume() { return area()*height; }
		
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.getvolume();
	return 0;
}

上一题