急~!!求简单C++编程题答案

2025-02-23 11:15:51
推荐回答(2个)
回答1:

第一题
#include
class Shape
{
public:
float r;
float w,h;
};
class Circle:public Shape
{
public:
double GetArea(float r)
{
return (3.14*r*r);
}
double GetPerim(float r)
{
return (3.14*2*r);
}
};
class Retangle:public Shape
{
public:
float GetArea(float w,float h)
{
return (w*h);
}
float GetPerim(float w,float h)
{
return ((h+w)*2);
}
};
void main()
{
char option;
do
{
cout<<"Please select the shape you want "< cout<<"1.Retangle"< cout<<"2.Cicle"< cout<<"Your option is :";
int selection;
cin>>selection;
for(int i=0;selection!=1&&selection!=2;i++)
{
cout<<"Input error!, please input again ";
}
Retangle retangle;
Circle circle;
if(selection==1)
{
cout<<"Please input width and height ";
cin>>retangle.h>>retangle.w;
cout<<"The area of retangle is "< cout<<"The perim of retangle is "< }
else if(selection==2)
{
cout<<"Please input radius of the circle ";
cin>>circle.r;
cout<<"The area of circle is "< cout<<"The perim of circle is "< }
cout<<"Do you want to continue ?, input y or Y means yes ";
cin>>option;
}while(option=='y'||option=='Y');
}
第二题
#include
class Boat
{
public:
float x;
friend float totalWeight();
};
class Car
{
public:
float y;
friend float totalWeight();
};
float totalWeight(float x,float y)
{
return (x+y);
}
void main()
{
Boat boat;
Car car;
cout<<"please input boat`s weight and car`s weight"< cin>>boat.x>>car.y;
cout<<"The total weight of them is "<}
第三题
头文件
#ifndef ORIGIN_H
#define ORIGIN_H
template
T origin(T x)
{
return x;
}
#endif
#ifndef SWAP_H
#define SWAP_H
template
T myswap(T x,T y)
{
T temp;
temp=x;
x=y;
y=temp;
return x;
}
#endif
CPP文件
#include "er.h"
#include
void main()
{
int a,b;
cout<<"Please input two integer:"< cin>>a>>b;
cout< float c,d;
cout<<"Please input two float :"< cin>>c>>d;
cout< char e,f;
cout<<"Please input two char :"< cin>>e>>f;
cout<}

回答2:

class Shape
{
public:
Shape(){}
~Shape(){}
virtual float GetArea() = 0; //定义纯虚函数
virtual float GetPerim() = 0;
}

class Rectangle : public Shape
{
private:
float m_fLength;
float m_fWidth;
public:

Rectangle(float fLength,float fWidth){
m_fLength = fLength;
m_fWidth = fWidth;
}
~Rectangle(){}
float GetArea(){
return m_fWidth*m_fLength;
}
float GetPerim(){
return 2*(m_fWidth+m_fLength);
}

}
#define PI 3.1415926
class Circle : public Shape
{
private:
float m_fDiameter;
public:
Circle(float fDiameter){
m_fDiameter = fDiameter;
}
~Circle(){}
float GetArea(){
return PI*(m_fDiameter*m_fDiameter)/4;
}
float GetPerim(){
return PI*m_fDiameter;
}

}