结构化(面向过程)程序设计的不足

1.PNG
2.PNG
3.PNG

面向对象程序设计(OOP)

4.PNG
5.PNG
1_LI (2).jpg

类和对象

6.PNG
7.PNG

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Rect{//class 类关键字 Rect 类名
  4. public:
  5. int w,h;//成员变量
  6. int Square(){return w*h;}//成员函数
  7. int Parameter(){return 2*(w+h);}
  8. void Init(int _w,int _h){
  9. w=_w;
  10. h=_h;
  11. }
  12. };//分号不能少
  13. int main(void)
  14. {
  15. int w,h;
  16. cin>>w>>h;
  17. Rect r;//r是一个对象
  18. r.Init(w,h);//调用成员函数
  19. cout<<"The square of the rectangle is"<<ends<<r.Square()<<endl;
  20. cout<<"The parameter of the rectangle is"<<ends<<r.Parameter()<<endl;
  21. }

内存分配

8.PNG

对象间的运算

9.PNG

类的成员变量和成员函数的用法

用法一:对象名.成员名

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Rect{
  4. public:
  5. int w,h;
  6. int Square(){return w*h;}
  7. int Parameter(){return 2*(w+h);}
  8. void Init(int _w,int _h){
  9. w=_w;
  10. h=_h;
  11. }
  12. };
  13. int main(void)
  14. {
  15. Rect r1,r2;
  16. r1.w=5;//对象r1中的成员变量w初始化为5
  17. r2.Init(5,4);//对象r2中的成员变量w和h分别初始化为5和4
  18. return 0;
  19. }

用法二:指针->成员名

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Rect{
  4. public:
  5. int w,h;
  6. int Square(){return w*h;}
  7. int Parameter(){return 2*(w+h);}
  8. void Init(int _w,int _h){
  9. w=_w;
  10. h=_h;
  11. }
  12. };
  13. int main(void)
  14. {
  15. Rect r1,r2;
  16. Rect *p1=&r1,*p2=&r2;
  17. p1->w=5;//作用在对象r1上
  18. p2->Init(5,4);//作用在对象r2上
  19. return 0;
  20. }

用法三:引用名.成员名

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Rect{
  4. public:
  5. int w,h;
  6. int Square(){return w*h;}
  7. int Parameter(){return 2*(w+h);}
  8. void Init(int _w,int _h){
  9. w=_w;
  10. h=_h;
  11. }
  12. };
  13. void fixRect(class Rect rr){//参数是类本身
  14. rr.h=7;
  15. rr.w=8;
  16. return;
  17. }
  18. void FixRect(class Rect &rr){//参数是类的引用
  19. rr.h=7;
  20. rr.w=8;
  21. return;
  22. }
  23. int main(void)
  24. {
  25. Rect r;
  26. r.h=5,r.w=6;
  27. cout<<r.Square()<<ends<<r.Parameter()<<endl; //30 22
  28. fixRect(r);
  29. cout<<r.Square()<<ends<<r.Parameter()<<endl;//30 22(不会对实参产生影响)
  30. FixRect(r);
  31. cout<<r.Square()<<ends<<r.Parameter()<<endl;//56 30
  32. return 0;
  33. }

用法四:类的定义和类的成员函数分开写

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Rect
  4. {
  5. public:
  6. int w,h;
  7. int Square(void);
  8. int Parameter(void);
  9. void Init(int,int);//函数原型
  10. };
  11. int Rect::Square(void)//Rect::说明该函数是Rect类的成员函数,不是普通的函数
  12. {
  13. return w*h;
  14. }
  15. int Rect::Parameter(void)
  16. {
  17. return 2*(w+h);
  18. }
  19. void Rect::Init(int _w,int _h)
  20. {
  21. w=_w;
  22. h=_h;
  23. }
  24. int main(void)
  25. {
  26. Rect r1,r2,r3;
  27. Rect *p=&r2;
  28. Rect &r=r3;
  29. r1.Init(7,8);//通过对象调用
  30. p->Init(5,6);//通过对象指针调用
  31. r.Init(3,4);//通过对象引用调用
  32. std::cout<<r1.Square()<<ends<<p->Parameter()<<ends<<r3.Square();//56 22 12
  33. return 0;
  34. }

类成员的可访问范围

1.PNG
注意:若某个成员没有上述关键字,则被缺省地认为是私有成员
1.PNG

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Employee
  4. {
  5. private:
  6. string szName;
  7. public:
  8. int salary;
  9. void SetName(string);
  10. void GetName(void);
  11. int AverageSalary(Employee &,Employee &);
  12. };
  13. void Employee::SetName(string name)
  14. {
  15. szName=name;
  16. }
  17. void Employee::GetName(void)
  18. {
  19. std::cout<<szName;
  20. }
  21. int Employee::AverageSalary(Employee &e1,Employee &e2)
  22. {
  23. return (e1.salary+e2.salary)/2;
  24. }
  25. int main(void)
  26. {
  27. Employee E1,E2;
  28. E1.salary=5000,E2.salary=10000;
  29. E1.szName="Tom";//编译出错 main函数不是类成员函数,不可通过其访问类私有成员
  30. E1.SetName("Tom");//OK 通过公有成员函数SetName访问私有成员变量szName
  31. E2.SetName("Jerry");//OK
  32. E1.GetName();//OK 通过公有成员函数SetName访问私有成员变量szName
  33. cout<<endl;//Tom
  34. E2.GetName();//OK
  35. cout<<endl;//Jerry
  36. std::cout<<E1.AverageSalary(E1,E2);//7500 该函数同时访问两个同类对象的公有变量salary
  37. return 0;
  38. }

隐藏机制及其作用

4.PNG
2_LI.jpg
准确地讲,是在SetName()中增加判断是否越界的语句,若越界则删除越界的部分

成员函数的重载和参数缺省

成员函数可以重载,也可以带缺省参数
1.PNG
注意:使用缺省参数时避免函数重载时候的二义性