1. #include"iostream"
  2. using namespace std;//名称空间
  3. class Rectangle //计算矩形的面积
  4. {
  5. public :
  6. int w,l;//成员变量,和为对象占用的内存空间
  7. void Init(int w_,int l_); //成员函数:函数与C与几乎一致
  8. int Sget(); //可以通过行为改变属性的值
  9. };
  10. void Rectangle::Init(int w_,int l_) //归属::行为
  11. {
  12. w=w_;
  13. l=l_;
  14. }
  15. int Rectangle::Sget()
  16. {
  17. return w*l;
  18. }
  19. int main()
  20. { int w,l;
  21. Rectangle r;//r是一个对象,通过类定义的变量为对象
  22. cin >>w >>l;
  23. r.Init(w,l);
  24. cout <<"面积为:"<< r.Sget()<< endl;
  25. return 0;
  26. }
  1. //构造函数存在的意义与必要性:避免声明
  2. #include"iostream"
  3. using namespace std;//名称空间
  4. class Rectangle //计算矩形的面积
  5. {
  6. public :
  7. int w,l;//成员变量,和为对象占用的内存空间
  8. void Init(int w_,int l_); //成员函数:函数与C与几乎一致
  9. int Sget(); //可以通过行为改变属性的值
  10. };
  11. void Rectangle::Init(int w_,int l_) //归属::行为
  12. {
  13. w=w_;
  14. l=l_;
  15. }
  16. int Rectangle::Sget()
  17. {
  18. return w*l;
  19. }
  20. int main()
  21. { int w,l;
  22. Rectangle r;//r是一个对象,通过类定义的变量为对象
  23. Rectangle r1;
  24. cin >>w >>l;
  25. r.Init(w,l);
  26. r1.w=3;
  27. r1.l=4;
  28. cout <<"面积为:"<< r.Sget()<< endl;
  29. cout <<"面积为:"<< r1.Sget()<< endl;
  30. return 0;
  31. }
  1. /*
  2. *构造函数存在的意义与必要性,构造函数是一种特殊的成员函数,构造指初始化对象(可重载),析构函数是编译器自动调用
  3. *可以认为是对内存空间的高效管理,在分配内存,回收内存上的很严谨,大大降低了bug概率
  4. *名字与类名相同,却没有返回值
  5. *无法像一般函数一样显式的调用,创建对象时自动执行
  6. */
  7. #include"iostream"
  8. using namespace std;//名称空间
  9. class Rectangle //计算矩形的面积
  10. {
  11. private:
  12. int w,l;
  13. public :
  14. Rectangle(int w_,int l_);//析构函数有~ ,析构函数不能进行重载
  15. void Init(int w_,int l_); //成员函数:函数与C与几乎一致
  16. int Sget(); //可以通过行为改变属性的值
  17. };
  18. Rectangle::Rectangle(int w_,int l_)//构造函数
  19. {
  20. w=w_;
  21. l=l_;
  22. }
  23. void Rectangle::Init(int w_,int l_) //归属::行为
  24. {
  25. w=w_;
  26. l=l_;
  27. }
  28. int Rectangle::Sget()
  29. {
  30. return w*l;
  31. }
  32. int main()
  33. {
  34. Rectangle * ptr;
  35. ptr=new Rectangle(3,4);//通过堆来申请内存chuck
  36. //Rectangle r(3,4);//通过栈来传参
  37. cout <<"面积为:"<<ptr->Sget()<< endl;
  38. return 0;
  39. }

C  类 - 图1```cpp //构造函数的简单分类:空参数,有参数,拷贝构造

include”iostream”

using namespace std;//名称空间 class Rectangle //计算矩形的面积 { public : int w,l;//成员变量,和为对象占用的内存空间 Rectangle(){}; //空参数,比如打印一句话,拷贝构造会是系统默认构造消失,故加上空构造 //Rectangle(int w,int l) 初始化一个对象更为简单 Rectangle(Rectangle p);//拷贝构造 void Init(int w,int l); //成员函数:函数与C与几乎一致 int Sget(); //可以通过行为改变属性的值 }; Rectangle::Rectangle(Rectangle p) { w=p->w; l=p->l; } void Rectangle::Init(int w,int l) //归属::行为 { w=w; l=l; } int Rectangle::Sget() { return w*l; } int main() { int w,l; Rectangle r;//r是一个对象,通过类定义的变量为对象 Rectangle r1; cin >>w >>l; r.Init(w,l); r1.w=3; r1.l=4; Rectangle r2(&r1); cout <<”面积为:”<< r.Sget()<< endl; cout <<”面积为:”<< r1.Sget()<< endl; cout <<”面积为” << r2.Sget()<<endl; return 0; }

  1. ```cpp
  2. //构造函数的简单分类:空参数,有参数,拷贝构造
  3. #include"iostream"
  4. using namespace std;//名称空间
  5. class Rectangle //计算矩形的面积
  6. {
  7. public :
  8. int w,l;//成员变量,和为对象占用的内存空间
  9. Rectangle(){}; //空参数,比如打印一句话
  10. //Rectangle(int w_,int l_) 初始化一个对象更为简单
  11. Rectangle(Rectangle *p);//拷贝构造
  12. void Init(int w_,int l_); //成员函数:函数与C与几乎一致
  13. int Sget(); //可以通过行为改变属性的值
  14. };
  15. Rectangle::Rectangle(Rectangle *p)
  16. {
  17. this->w=p->w;//this指向当前函数的对象
  18. this->l=p->l;
  19. }
  20. void Rectangle::Init(int w_,int l_) //归属::行为
  21. {
  22. this->w=w_;
  23. this->l=l_;
  24. }
  25. int Rectangle::Sget()
  26. {
  27. return this->w*this->l;
  28. }
  29. int main()
  30. { int w,l;
  31. Rectangle r;//r是一个对象,通过类定义的变量为对象
  32. Rectangle r1;
  33. cin >>w >>l;
  34. r.Init(w,l);//this指向r
  35. r1.w=3; //this指向r1
  36. r1.l=4;
  37. //Rectangle r2(&r1);
  38. cout <<"面积为:"<< r.Sget()<< endl;
  39. cout <<"面积为:"<< r1.Sget()<< endl;
  40. // cout <<"面积为" << r2.Sget()<<endl;
  41. return 0;
  42. }
  1. enum Rectangle_Kind
  2. {
  3. com_R=0;
  4. spe_R=1;
  5. };
  6. typedef struct R
  7. {
  8. enum R_K;
  9. R_K=com_R;
  10. };
  1. /*!构造函数的简单分类:空参数,有参数,拷贝构造*/
  2. #include"iostream"
  3. using namespace std;//!<名称空间
  4. /**
  5. * 这是一个计算矩形面积的类
  6. */
  7. typedef struct R_K
  8. {
  9. int w,l; /*!two integer value in this struct*/
  10. }
  11. class Rectangle
  12. {
  13. public :
  14. int w,l; /*!< two integer value */
  15. Rectangle(){}; //!< a member function which is Null,but Rectangle(int w_,int l_) 初始化一个对象更为简单
  16. Rectangle(Rectangle *p);//!<析构函数
  17. void Init(int w_,int l_); //!<成员函数,初始化长度
  18. int Sget(); //!<可以通过行为改变属性的值
  19. };
  20. Rectangle::Rectangle(Rectangle *p)
  21. {
  22. this->w=p->w;
  23. this->l=p->l;
  24. }
  25. void Rectangle::Init(int w_,int l_)
  26. {
  27. this->w=w_;
  28. this->l=l_;
  29. }
  30. int Rectangle::Sget()
  31. {
  32. return this->w*this->l;
  33. }
  34. int main()
  35. { int w,l;
  36. Rectangle r;/*!< this is an object */
  37. Rectangle r1;
  38. cin >>w >>l; /*!< input two integer value */
  39. r.Init(w,l);/*!< this is an ptr point to obj r*/
  40. r1.w=3;
  41. r1.l=4;
  42. cout <<"r的面积为:"<< r.Sget()<< endl;
  43. cout <<"r1的面积为:"<< r1.Sget()<< endl;
  44. return 0;
  45. }