1.
#include <iostream>
#include <iomanip>
using namespace std;
class RECTANGLE
{
int length;
int width;
public:
void set_length(int l);
void set_width(int w);
int get_length(void);
int get_width(void);
void show(void);
};
void RECTANGLE::set_length(int l)
{
length=l;
}
void RECTANGLE::set_width(int w)
{
width=w;
}
int RECTANGLE::get_length(void)
{
return length;
}
int RECTANGLE::get_width(void)
{
return width;
}
void RECTANGLE::show(void)
{
int c;
int s;
c=(length+width)*2;
s=length*width;
cout << setw(5) << left << "c=" << c << setw(5) << "s=" << s << endl;
}
int main()
{
cout << "Hello World!" << endl;
RECTANGLE r1;
r1.set_length(5);
r1.set_width(6);
cout << "length=" << r1.get_length() << endl;
cout << "length=" << r1.get_width() << endl;
r1.show();
return 0;
}
2.
#include <iostream>
using namespace std;
class CIRCLE
{
int radius;
public:
void set_r(int r);
void show(double pi=3.14);
};
void CIRCLE::set_r(int r)
{
radius=r;
}
void CIRCLE::show(double pi)
{
double c,s;
c=2*pi*radius;
s=pi*radius*radius;
cout << "c=" << c << endl;
cout << "s=" << s << endl;
}
int main()
{
CIRCLE r1;
r1.set_r(3);
r1.show();
cout << "Hello World!" << endl;
return 0;
}
3.
#include <iostream>
using namespace std;
class CAR
{
string brand;
string color;
int speed;
public:
void display(int n);
void accelerate(int amount,int size);
void set(string b,string c,int s);
};
void CAR::display(int n)
{
if(n==0)
{
cout << "汽车品牌:" << brand << endl;
cout << "汽车颜色:" << color << endl;
cout << "汽车速度:" << speed << endl;
}
else if(n==1)
{
cout << "汽车品牌:" << brand << endl;
}
else if(n==2)
{
cout << "汽车颜色:" << color << endl;
}
else if(n==3)
{
cout << "汽车速度:" << speed << endl;
}
}
void CAR::accelerate(int amount,int size)
{
int i;
for(i=1;i<=amount;i++)
{
speed+=size;
}
}
void CAR::set(string b,string c,int s)
{
brand=b;
color=c;
speed=s;
}
int main()
{
CAR c1;
c1.set("大众","黑色",40);
c1.display(0);
c1.accelerate(1,10);
c1.display(3);
cout << "Hello World!" << endl;
return 0;
}