求圆半径和周长
#include <iostream>
using namespace std;
struct Cir
{
private:
int r;
public:
void set_r(int i);
void show();
};
void Cir::set_r(int i)//设置半径
{
r = i;
}
void Cir::show()//打印周长面积
{
double Pi = 3.14;
double l = 2*Pi*r;
double s = Pi*r*r;
cout<< "周长=" << l << endl;
cout<< "面积=" << s << endl;
}
int main()
{
Cir cir;
int i;
cin >> i;
cir.set_r(i);
cir.show();
return 0;
}
汽车
#include <iostream>
using namespace std;
struct Car
{
string brand;
string color;
int speed;
void dispaly();
void accelerate(int amount);
void set(string a,string b,int c);
};
void Car::dispaly()
{
cout << "汽车信息如下:" << endl;
cout << "品牌:" << brand <<endl;
cout << "颜色:" << color <<endl;
cout << "初速度:" << speed << "km/h" << endl;
}
void Car::accelerate(int amount)
{
speed += amount;
}
void Car::set(string a,string b,int c)
{
brand = a;
color = b;
speed = c;
}
int main()
{
Car car;
string a,b;int c;
cin >> a >> b >> c;
car.set(a,b,c);
car.dispaly();
int amount;
cout << "请输入加速值:";
cin >> amount;
car.accelerate(amount);
car.dispaly();
return 0;
}