四、酒店场景。
四、酒店场景。
四、酒店场景。
要求
要求
要求:
定义一个客人类 Guest。包含成员属性:编号 Num、姓名 Name、房费 Fee、当前酒店入住人数 Count。
其中编号 Num 需要程序自动生成。
现在要求实现以下 Guest 的成员函数:构造函数、Show()显示 Guest 的信息、 GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。
并定义 3 个 Guest 对象来对成员函数进行测试。
样例:
#include<bits/stdc++.h>
using namespace std;
class Guest
{
public:
Guest(string Nick , int cost);
static int cnt;
static int sum_cost;
static int GetTotalIncome()
{
return sum_cost;
}
static int GetCount()
{
return cnt;
}
void show();
string name;
int fee;
int num;
};
int Guest :: cnt = 0;
int Guest :: sum_cost = 0;
void Guest :: show()
{
cout << "Name : " << name << endl;
cout << "Num :" << num << endl;
cout << "Fee :" << fee << endl;
}
Guest :: Guest(string Nick , int cost)
{
cnt ++;
name = Nick , fee = cost;
num = cnt;
sum_cost += cost;
}
int main()
{
Guest aa("张三" , 900);
Guest bb("李四" , 100);
Guest cc("王五" , 1000);
cout <<"Now Count :" << aa.GetCount() << endl;
cout << "Now income :" << aa.GetTotalIncome() << endl;
aa.show();
bb.show();
cc.show();
}
五、抽象类
S
h
a
p
e
五、抽象类 Shape
五、抽象类Shape
要求:
要求:
要求:它拥有一系列虚函数:Input()输入类需要的信息、Show() 显示类的信息、Perimeter()计算周长、Area()计算面积。
先定义 Circle、Square、 Triangle 来继承 Shape 并实现其虚函数。要求创建 Circle、Square、Triangle 的对象,用基类指针指向这些对象,并调用成员函数。
样例输入:
input radius:2
input length of each side:3 4 5
input width and height: 4 6
样例输出:
Radius:2
Area=12.5808
Perimeter=12.5664
Lenght_A:3
Lenght_B:4
Lenght_C:5
Area=6
Perimeter=12
width:4
height:6
Area=24
Perimeter=20
#include<bits/stdc++.h>
using namespace std;
#define PII 3.1415926
class Shape
{
public:
virtual void Input() = 0;
virtual void Show() = 0;
virtual double Perimeter() = 0;
virtual double Area() = 0;
};
//圆形
class Circle : public Shape
{
public:
double radius;
virtual double Perimeter();
virtual double Area();
virtual void Input()
{
double r;
cin >> r;
radius = r;
}
virtual void Show();
};
void Circle :: Show()
{
cout << "Radius:" << radius << endl;
cout << "Area=" << Area() << endl;
cout <<"Perimeter=" << Perimeter() << endl;
}
double Circle :: Perimeter()
{
double ans = 2 * PII * radius;
return ans;
}
double Circle :: Area()
{
double ans = PII * radius * radius;
return ans;
}
//三角形
class Triangle : public Shape
{
public:
double Lenght_A;
double Lenght_B;
double Lenght_C;
virtual double Perimeter();
virtual double Area();
virtual void Input()
{
double a , b , c;
cin >> a >> b >> c;
Lenght_A = a , Lenght_B = b , Lenght_C = c;
}
virtual void Show()
{
cout << "Lenght_A:" << Lenght_A << endl;
cout << "Lenght_B:" << Lenght_B << endl;
cout << "Lenght_C:" << Lenght_C << endl;
cout << "Area=" << Area() << endl;
cout <<"Perimeter=" << Perimeter() << endl;
}
};
double Triangle :: Perimeter()
{
double ans = Lenght_A + Lenght_B + Lenght_C;
return ans;
}
double Triangle :: Area()
{
double s = (Lenght_A + Lenght_B + Lenght_C) / 2;
double ans = sqrt(s * (s - Lenght_A) * (s - Lenght_B) * (s - Lenght_C));
return ans;
}
//正方形
class Square : public Shape
{
public:
double width;
double heigh;
virtual double Perimeter();
virtual double Area();
virtual void Input()
{
double w , d;
cin >> w >> d;
width = w , heigh = d;
}
virtual void Show()
{
cout << "width:" << width << endl;
cout << "heigh:" << heigh << endl;
cout <<"Area=" << Area() << endl;
cout <<"Perimeter=" << Perimeter() << endl;
}
};
double Square :: Perimeter()
{
double ans = 2 * (width + heigh);
return ans;;
}
double Square :: Area()
{
double ans = width * heigh;
return ans;;
}
int main()
{
cout << "input radius:";
Shape *p;
Circle C;
Square S;
Triangle T;
p = &C;
p->Input(); // 1 . 要保证虚函数在每一个子类里传入的参数个数一致
p->Show(); // 2 . 写的每一个虚函数都要有对应的实现,不然无法创建子类
cout << "input length of each side:";
p = &T;
p->Input();
p->Show();
cout << "input width and height:";
p = &S;
p->Input();
p->Show();
}