C++之类的基本定义
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
class Bird {
public:
int height;
int width;
int length;
double average;
double get();
void set(int len, int width, int height, double average);
};
double Bird::get() {
int volume= length * width * height;
return (double) volume / average;
}
void Bird::set(int len, int w, int h, double avg) {
length = len;
width = w;
height = h;
average = avg;
}
Bird bird1;
Bird bird2;
int main() {
system("chcp 65001");
bird1.height = 10;
bird1.width = 20;
bird1.length = 30;
bird1.average = (double) (bird1.height + bird1.width + bird1.length) / 3;
int volume = bird1.height * bird1.width * bird1.length;
double volume_div_avg = (double) volume / bird1.average;
cout << "bird1对象外部赋值求出体积除以平均数的值:" << volume_div_avg << endl;
cout << "-----------------------------------------------" << endl;
double avg = (double) (10 + 20 + 30) / 3;
bird2.set(10, 20, 30, avg);
volume_div_avg = bird2.get();
cout << "bird2对象调用set(),get()求出体积除以平均数的值:" << volume_div_avg << endl;
return 0;
}
输出:
Active code page: 65001
bird1对象外部赋值求出体积除以平均数的值:300
-----------------------------------------------
bird2对象调用set(),get()求出体积除以平均数的值:300