列表

详情


CPP67. 多态实现求面积体积

描述

现在有长方形类(rectangle),和以此为基类构建的长方体类(cuboid),运用多态在两个类中实现getval方法,在长方形类中是求面积功能,在长方体类中是求体积功能。

输入描述

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

输出描述

第一行输出一个整数表示长方体的面积。
第二行输出一个整数表示长方体的体积。

示例1

输入:

3 2 1

输出:

6
6

原站题解

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

C++ 解法, 执行用时: 2ms, 内存消耗: 432KB, 提交时间: 2022-05-31

#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 getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		// write your code here...
        virtual int getval(){
            return this->length*this->width;
        }
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		cuboid(int x,int y,int z):rectangle(x,y){
			height=z;
		}
		// write your code here...
        virtual int getval(){
            return this->rectangle::getval()*this->height;
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	rectangle b(x,y);
	
	rectangle *p=&b;
	cout<<p->getval()<<'\n';
	
	p=&a;
	cout<<p->getval();
	return 0;
}

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

#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 getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		// write your code here...
        virtual int getval()
        {
            return length * width ;
        }
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		cuboid(int x,int y,int z):rectangle(x,y){
			height=z;
		}
		// write your code here...
        virtual int getval()
        {
            return getlength() * getwidth() * height;
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	rectangle b(x,y);
	
	rectangle *p=&b;
	cout<<p->getval()<<'\n';
	
	p=&a;
	cout<<p->getval();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 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 getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		// write your code here...
        virtual int getval()
        {
            return length*width;
        }
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		cuboid(int x,int y,int z):rectangle(x,y){
			height=z;
		}
		// write your code here...
        int getval()
        {
            return (getlength()*height*getwidth());
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	rectangle b(x,y);
	
	rectangle *p=&b;
	cout<<p->getval()<<'\n';
	
	p=&a;
	cout<<p->getval();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-04-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 getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		// write your code here...
        virtual int getval(){
            return length*width;
        }
        
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		cuboid(int x,int y,int z):rectangle(x,y){
			height=z;
		}
		// write your code here...
        int getval(){
            return height* this->rectangle::getval();
        }
        
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	rectangle b(x,y);
	
	rectangle *p=&b;
	cout<<p->getval()<<'\n';
	
	p=&a;
	cout<<p->getval();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-03-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 getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		// write your code here...
        virtual int getval(){
        return length*width;
        }
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		cuboid(int x,int y,int z):rectangle(x,y){
			height=z;
		}
		// write your code here...
        virtual int getval(){
            return getlength()*getwidth()*height;
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	rectangle b(x,y);
	
	rectangle *p=&b;
	cout<<p->getval()<<'\n';
	
	p=&a;
	cout<<p->getval();
	return 0;
}

上一题