/*
实验任务
1.P50页(1)声明一个CPU类
2.P58 4-13
笔记查询:
https://www.runoob.com/cplusplus/cpp-overloading.html
https://www.runoob.com/cplusplus/cpp-inheritance.html
https://www.runoob.com/cplusplus/cpp-polymorphism.html
*/
1
#include <iostream>
using namespace std;
enum CPU_Rank { p1 = 1, p2, p3, p4, p5, p6, p7 };
class CPU {
private:
CPU_Rank rank;
int frequency;
float voltage;
public:
CPU(CPU_Rank r, int f, float v)
{
rank = r;
frequency = f;
voltage = v;
cout << "构造了一个CPU!" << endl;
}
~CPU() {
cout << "析构了一个CPU!" << endl;
}
CPU_Rank GetRank() const { return rank; }
int GetFrequency() const { return frequency; }
float GetVoltage() const { return voltage; }
void SetRank(CPU_Rank r) { rank = r; }
void SetFrequency(int f) { frequency = f; }
void SetVoltage(float v) { voltage = v; }
void Run() { cout << "CPU开始运行!" << endl; }
void Stop() { cout << "CPU停止运行!" << endl; }
};
int main() {
CPU a(p6, 300, 2.8);
a.Run();
a.Stop();
}
2
#include <iostream>
using namespace std;
class Circle {
public:
Circle(float radius) {
this->radius= radius;
}~Circle() {
}
float getArea() {
return 3.14 * radius * radius;
}
private:
float radius;
};
int main() {
float radius;
cout << "请输入圆的半径:";
cin >> radius;
Circle p(radius);
cout << "半径为" << radius << "的圆的面积为" << p.getArea() << endl;
return 0;
}