C++编程求救!!!
发布网友
发布时间:2022-05-04 12:25
我来回答
共3个回答
热心网友
时间:2022-06-21 21:32
#include <iostream>
#include <string>
using namespace std;
//---------------------- 1 -----------------------------------------
class Box
{
public:
Box(float sideLen);
float GetArea();
float GetVolume();
friend ostream& operator <<(ostream& out, const Box& self);
private:
float mSideLen;//边长
float mArea;//表面积
float mVolume;//体积
};
Box::Box(float sideLen)
:mSideLen(sideLen)
{
mArea = mSideLen * mSideLen * 6;
mVolume = mSideLen * mSideLen * mSideLen;
}
float Box::GetArea()
{
return mArea;
}
float Box::GetVolume()
{
return mVolume;
}
ostream& operator <<(ostream& out, const Box& self)
{
out << "立方体面积:" << self.mArea <<"立方体体积为:" << self.mVolume << endl;
return out;
}
//---------------------------------2---------------------------------
class Shape
{
public:
virtual float GetArea(){ return 0;}
virtual float GetPerim(){return 0;}
};
class Rectangle : public Shape
{
public:
Rectangle(float width, float height);
float GetArea();
float GetPerim();
private:
float mWidth;
float mHeight;
};
Rectangle::Rectangle(float width, float height)
:mWidth(width)
,mHeight(height)
{
}
float Rectangle::GetArea()
{
return mWidth * mHeight;
}
float Rectangle::GetPerim()
{
return (mWidth + mHeight) * 2;
}
const float PI = 3.1415926 ;
class Circle : public Shape
{
public:
Circle(float radius);
float GetArea();
float GetPerim();
private:
float mRadius;
};
Circle::Circle(float radius)
:mRadius(radius)
{
}
float Circle::GetArea()
{
return PI * mRadius * mRadius;
}
float Circle::GetPerim()
{
return PI * mRadius * 2;
}
//=================== 3
class Student
{
public:
Student(string name);
string GetName();
void SetScore(int score);
friend bool operator < (const Student& one, const Student& other);
private:
string mName;
int mScore;
};
Student::Student(string name)
:mName(name)
{
}
string Student::GetName()
{
return mName;
}
void Student::SetScore(int score)
{
mScore = score;
}
bool operator < (const Student& one, const Student& other)
{
if(one.mScore < other.mScore)
return true;
return false;
}
int main()
{
//=== 1====
Box box(5);
cout<<box;
//=== 2====
Shape* pRect = new Rectangle(10, 20);
float area = pRect->GetArea();
Shape* pCircle = new Circle(5);
area = pCircle->GetArea();
delete pRect;
delete pCircle;
//==== 3 ====
Student* stu[4];
cout<< "输入4个学生成绩\n" << endl;
string name[4] = {"学生1","学生2","学生3","学生4"};
for(int i = 0; i < 4; ++i)
{
stu[i] = new Student(name[i]);
int score;
cin >>score;
stu[i]->SetScore(score);
}
//
int maxScoreIndex = 0,minScoreIndex = 0;
for(int i = 1; i < 4; ++i)
{
if(*stu[i] < *stu[minScoreIndex])
{
minScoreIndex = i;
}
if(!(*stu[i] < *stu[maxScoreIndex]))
{
maxScoreIndex = i;
}
}
cout <<"分数最高的为:" << stu[maxScoreIndex]->GetName() << " 分数最低的为:" << stu[minScoreIndex]->GetName() << endl;
//============
int a;
cin >> a;
return 0;
}
热心网友
时间:2022-06-21 21:32
这些题目都是书上的示例啊,谭浩强那本C++面向对象程序设计上都有,你对着书上套,很容易的。
热心网友
时间:2022-06-21 21:33
分少,时间少。。。不然给你写了。 。。。。。