class CRectangle
{
public:
int w,h;
int Area()
{
return w*h;
}
int Perimeter()
{
return 2*(w+h);
}
void Init(int w_,int h_)
{
w=w_;
h=h_;
}
};//必须有分号
CRectangle r1,r2;
r1.w=5;
r2.Init(5,4);
CRectangle r1,r2;
CRectangle *p1=&r1;
CRectangle *p2=&r2;
p1->w=5;
p2->Init(5,4);//Init作用在p2指向的对象上
CRectangle r2;
CRctangle &rr=r2;
rr.w=5;
rr.Init(5,4);//rr的值变了,r2的值也变
void Print(CRectangle &r)
{
cout<<r.Area()<<","<<r.Perimeter();
}
CRectangle r3;
r3.Init(5,4);
Print (r3);
class CRectangle
{
public:
int w,h;
int Area();//
};
int CRectangle :: Area()
{
return w*h;
}
CRectangle ::
说明后面的函数是CRectangle类的成员函数,而非普通函数,那么一定要通过对象或对象的指针或对象的引用才能调用class className()
{
private:
私有属性和函数
public:
共有属性和函数
protected:
保护属性和函数
};
class Location
{
public:
void value X(int val=0){x=val;}
int value X(){return x;}
};
Location A;
A.value X();//编译错误,编译器无法判断调用哪个value X
原文:https://www.cnblogs.com/2002ljy/p/12244943.html