作业
main.cpp
#include "class.h"
int main()
{
Rect r1;
r1.init(2.2,3.5);
r1.set_w(2.5);
r1.set_h(2.5);
r1.show();
return 0;
}
class.cpp
#include "class.h"
void Rect::set_w(double w)
{
this->width=w;
cout << width << endl;
}void Rect::set_h(double h)
{
this->height=h;
cout << height << endl;
}void Rect::init(double w,double h)
{
this->width=w;
this->height=h;
cout << width <<endl;
cout << height << endl;
}void Rect::show()
{
double area=this->width*this->height;
double perimeter=(this->width+this->height)*2;
cout << "area:" << area << endl;
cout << "perimeter:" << perimeter << endl;
}
class.h
#include <iostream>
using namespace std;
class Rect
{
private:
double width;
double height;
public:
void set_w(double w);
void set_h(double h);
void init(double w,double h);
void show();
};