C++学习(三)

static
静态修饰的变量,必须先声明,再实现,例如
 1 class  Person{
 2     public:
 3        startic  int age;
 4         startic initAge(){
 5             //运行异常
 6              age = 10 ;                 
 7         }
 8 }
 9 
10 
11 正确写法
12 class  Person{
13     public:
14        startic  int age;
15         startic initAge(){                          
16         }
17         void setage(){
18             age = 30;        
19         }
20 }
21 
22 int Person::age = 10;
23 void  main(){
24     Person p;
25     p.setage();
26     Person::age =32;
27     Person::initAge();
28 }
this
为什么会有this?
当通过p.setage();和getage();时,
在代码区域会保存多份代码副本,
this指针指向相对应的代码区域
 
 
 1 class Person{
 2     public:
 3         char * name;
 4         int age;
 5         int age = NULL;//C++中,int默认值是系统默认值 -64664,NULL就表示0;或者直接写0也可以。
 6         //默认持有隐式的this(Person * const this)
 7         //指针常量:只允许更改this指针地址的值,而不能更改指针地址。
 8      void change(){
 9             //此行代码运行报错,指针常量不可改地址。而常量指针(const int*)只可以改指针,不能改值
10             //this = 0x2343;
11             this->age = 88;
12      }
13 }
14 
15 
16 class Person{
17     public:
18         char * name;
19         int age;
20         int age = NULL;//C++中,int默认值是系统默认值 -64664,NULL就表示0;或者直接写0也可以。
21         //默认持有隐式的this( const Person * const this),常量指针常量,只读,不能修改地址且不能修改地址对应的值
22      void change2() const{
23             //下边2行代码都不能运行
24             this = 0x2343;
25             this->age = 88;
26      }
27 }

 

友元函数和友元类
友元函数:
 1 #include <iostream>
 2 #include "string.h"
 3 using namespace std;
 4 
 5 class Student {
 6     private:
 7     int age ;
 8     
 9     public:
10     Student(int age){
11     this->age = age;
12     }
13     int getAge(){
14     return this->age;
15     }
16     //定义友元函数,可以访问所有私有成员
17     friend void updateAge(Student* stu,int age);
18 };
19 
20 void updateAge(Student* stu,int age){
21     cout << "原始数据: "<<stu->getAge() << endl;
22     //如果不设置友元函数则不能修改私有的age
23     stu->age = age;
24 }
25     
26 int main(){
27     Student s = Student(20) ;
28     updateAge(&s,88);
29     cout << "更改后数据:"<<s.getAge() << endl;
30     getchar();
31     return 0;
32 }
33 //log:
34 原始数据: 20
35 更改后数据:88

 

友元类:
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Pig{
 5     private:
 6         int age;
 7         char* name;
 8         //友元类
 9         friend class PigImpl;
10     
11 };
12 
13 class PigImpl{
14     public:
15       Pig pig;
16       
17     void changeAge(int age){
18         pig.age = age;    
19     }        
20     int getAge(){
21         return pig.age;
22     }
23 };
24 
25 int main(){
26     PigImpl mPigImpl;
27     mPigImpl.changeAge(100);
28     cout << mPigImpl.getAge() <<endl;
29     return 0;
30 }

 

总结:静态函数、友元函数、构造函数、拷贝构造函数、析构函数的区别
Pig.h文件
 1 #ifndef PIG_H
 2 #define PIG_H
 3 #include <iostream>
 4 using namespace std;
 5 
 6 
 7 class Pig {
 8 private:
 9     int age
10     char *name;
11 public:
12     static int age;
13 
14     Pig();
15 
16     Pig(char *);
17     //普通函数
18     Pig(char *, int);
19     //析构函数
20     ~Pig();
21     //拷贝构造函数
22     Pig(const Pig &pig);
23 
24     //普通函数
25     int getAge();
26 
27     char *getName();
28 
29     void setAge(int);
30 
31     void setName(char *);
32 
33     //静态函数的生命
34     static void changeTag(int age);
35 
36     //友元函数
37     friend void changeAge(Pig* pig,int age);
38     //常量指针常量
39     void showInfo() const;
40 };
41 
42 #endif
Pig.cpp文件
 1 #include "Pig.h"
 2 
 3 /**此类是对 Pig.h声明函数的实现类*/
 4 Pig::Pig() {
 5 
 6 }
 7 
 8 Pig::Pig(char *name) {
 9 
10 }
11 
12 Pig::Pig(char *name, int age) {
13 
14 }
15 
16 Pig::~Pig() {}
17 
18 Pig::Pig(const Pig &pig) {}
19 
20 //静态属性实现,不需要写static关键字
21 int Pig::id = 188;
22 
23 //静态函数不需要写static关键字
24 void changeTag(int age){
25 
26 }
27 
28 //友元函数的实现不需要写 friend关键字,也不需要写  XXX::
29 changeAge(Pig* pig,int age){
30 
31 }
32 
33 Pig::showInfo() const {
34 
35 }
运算符重载
类外的运算符重载
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Point{
 5     private:
 6         int x;
 7         int y;
 8     
 9      public:
10         Point(int x, int y):x(x),y(y){}
11     
12         int getX(){
13         return this->x;
14         }
15         int getY(){
16         return this->y;
17         }
18 
19     };
20 
21 // 对象1 + 对象2。在C++、KT里是不被允许的,可以自定义重写运算符。
22 //此为类外重写,一般都是放置类内
23 Point operator + (Point p1,Point p2){
24     int x = p1.getX()+p2.getX();
25     int y = p1.getY()+p2.getY();
26     Point p(x,y);
27     return p;
28 }
29 
30 int main(){
31     Point p1(100,200);
32     Point p2(200,300);
33     Point p = p1 + p2;
34     cout << p.getX()<<"  ,  "<<p.getY() <<endl;
35     return 0;
36 }
37 //log: 300,500

 

类内的运算符重载
 
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Point{
 5 private:
 6     int x;
 7     int y;
 8     
 9 public:
10     Point(int x, int y):x(x),y(y){}
11     int getX(){
12         return this->x;
13     }
14     int getY(){
15         return this->y;
16     }
17     //不需要传入两个参数,本身自己持有this,this指针指向自己的地址
18     /**
19     *const 不可变
20     * & 提高性能,如果没有 该引用的话会生成一个副本
21     * & 只是引用,给这块内存重新取别名,内存地址是一样的
22     */
23     
24     Point operator + (const Point &p2){
25         int x = this->x + p2.x;
26         int y = this->y + p2.y;
27         Point p(x,y);
28         return p;
29     }
30     //运算符重载
31     void operator ++(){
32         this->x = x+1;
33         this->y = y+1;
34      }
35     void operator ++(int){
36         this->x = x+1;
37         this->y = y+1;
38     }
39 };
40 
41 
42 int main(){
43     Point p1(100,200);
44     Point p2(200,300);
45     Point p = p1 + p2;
46     p++;
47     ++p;
48     cout << p.getX()<<"  ,  "<<p.getY() <<endl;
49     return 0;
50 }
51 //log: 302,502

 

 

 

 

posted on 2023-06-15 18:32  斌仔16  阅读(10)  评论(0)    收藏  举报