C++ - 类

2016-01-13  本文已影响65人  Mitchell
r1.w = 5;
r2.Init(3,4);
CRegtangle r1 ,r2;
CRegtangle * p1 = &r1;
CRegtangle * p2 = &r2;
p1->w = 5;
p2->Init(3,4);
CRegtangle r2;
CRegtangle & rr = r2;
rr.w = 5;
rr.Init(3,4);
class CRegtangle{
    public:
        int w,h;
        int Area();
        int Perimeter();
        void Init(int w_,int h_);
};

类的成员函数的另一种写法

int Rectangle::Area(){
  return w*h;
}
int Rectangle::Perimeter()}
   return 2*(w+h);
}
void Rectangle::init(int w_,int h_){
w = w_;h = h_;
}
class Man{
   int nAge;  //私有成员
  char szName[20];//私有成员
public:
    void SetName(char * Name){
        strcpy(szName,Name);
    }
}
class Employee{
  private:
char szName[30];
public:
int salary;
void setName(char * name);
void setName(char * name);
void averageSalary(Employee e1,Employee e2);
};
void CEmployee::setName(char *name){
  strcpy(szName,name);//ok
}
void Employee:getName(char * name){
  strcpy(name,szName);//ok
}
上一篇下一篇

猜你喜欢

热点阅读