zoukankan      html  css  js  c++  java
  • 课程小结

    一.

    先补充一个比较容易遗忘的知识点:同一个类的对象A和B,我先定义A后定义B,那么对象A和对象B的构造函数和析构函数的调用顺序如下:
    1.调用A的构造函数
    2.调用B的构造函数
    ···
    3.调用B的析构函数
    4.调用A的析构函数

    其实可以这样理解,想像一下A和B是两个循环体,那么构造函数就像循环体的{,析构函数就像循环体的},那么可以类比内嵌循环的左右大括号匹配顺序来判别调用先后。

    二.

    特殊对象的生命周期:
    1.全局对象的生命周期:在main函数之前构造,在main函数结束或者调用exit函数时结束。
    2.局部自动(auto)对象的生命周期:函数调用,建立对象时构造。函数结束时析构。存在的周期相当于调用函数的时长,多次调用就多次构造析构。
    3.局部静态(static)对象的生命周期:函数调用时构造,整个程序结束的时候析构,期间不构造也不析构。

    三.

    对象数组:
    构建对象所属的类必须有无参构造函数或者形参皆有缺省值

    // 一个小练习:
    class Student
    {
    	private:
    		int num;
    	public:
    		Student(int a = 10):num(a){};
    		void display();
    };
    
    void Student::display()
    {
    	cout << num << endl;
    }
    
    Student stu[15];
    
    int main()
    {
    	int i,j;
    	int n;
    	
    	for(i = 1; i <= 5; i++)
    	{
    		scanf("%d",&n);
    		
    		Student s(n); //前五个数组元素通过输入的值进行初始化,后面五个不进行操作
    		stu[i] = s;
    	}
    	
    	for(i = 1; i <= 10; i++)
    	{
    		stu[i].display();
    	}
    	
    	return 0;
    }
    
    输入:1 2 3 4 5
    输出:
    1
    2
    3
    4
    5
    10
    10
    10
    10
    10
    
    

    对象数组的标准定义方式:在定义时初始化。

    Student stu[15] = {Student(0),Student(1),Student(2),Student(3),Student(4),Student(5)}; //对对象元素进行初始化
    

    动态创建对象数组:

    int main()
    {
    	Student *p = new Student[15]; //指向对象数组的指针
    	p[1] = Student(1);
    	p[2] = Student(2);
    	p[3] = Student(3);
    	p[4] = Student(4);
    	p[5] = Student(5);
    	
    	for(int i = 1; i <= 10; i++)
    	{
    		p[i].display();
    	}
    	
    	delete []p; //删除释放空间
    	
    	return 0;
    }
    

    关键点:类内必须有无参或者缺省构造函数。

    四.

    指向对象的指针和this指针

    指向对象成员(public)的指针:顾名思义。
    定义方法:类型 * 指针名 = &对象.对象成员

    指向函数成员(public)的指针:指向类中的函数成员。
    定义方法:数据类型名 ( 类名 :: *指针变量名 )( 参数列表 )

    #include <iostream>
    using namespace std;
    
    class Student
    {
    	private:
    		int num;
    	public:
    		Student(int a):num(a){};
    		void display();
    };
    
    void Student::display()
    {
    	cout << num << endl;
    }
    
    int main()
    {
    	Student stu(10);
    	void (Student:: *p)(); //指向成员函数的指针 
    	
    	p = &Student::display; //获得地址 
    	
    	(stu.*p)(); //调用成员函数
    	
    	//相当于下面的语句:
    	stu.display();
    	
    	return 0;
    }
    

    this指针:指向类对象的首地址。成员函数通过this指针知道自己当前被哪一个对象所用。
    它是一个隐含在成员函数的指针。
    比如下面这串代码:

    class Student
    {
    	private:
    		int num;
    	public:
    	        ··· 
    		void display(); // void display(Student *this)
    };
    
    void Student::display() // void Student::display(Student *this)
    {
    	cout << num << endl; // cout << this -> num << endl;
    }
    

    注释处编译器自动处理函数的定义和声明。

    注意:1.this指针可以显示使用。2.this指针是const指针,不能修改其值。3.当形参和数据成员同名时,操作时需要在数据成员前加上this指针。this -> x = x

  • 相关阅读:
    Leetcode题目practice
    文件操作
    39个奇葩代码注释,拿走不谢
    Spring Boot 之配置导入,强大到不行!
    Git 的这个神技,学会爽歪歪~
    同事天天写垃圾代码,就没办法?
    for (;;) 与 while (true),哪个更快?
    Spring Boot 怎么打一个可执行 Jar 包?
    程序员真的是太太太太太太太太难了!
    面试官:new一个对象有哪两个过程?
  • 原文地址:https://www.cnblogs.com/qq952693358/p/5535817.html
Copyright © 2011-2022 走看看