C++笔记 面向对象、构造析构拷贝初始化列表、string、new和delete、拷贝函数、this指针

本文介绍了C++的基础知识,包括类与对象、构造函数、析构函数、初始化列表、string类的使用、内存管理(new和delete)、拷贝构造函数以及模块化编程。示例代码展示了如何创建类、初始化成员变量、操作字符串、动态分配内存以及实现运算符重载。此外,还探讨了成员函数中的this指针概念及其应用。

一、面向对象

1.1 类和对象

关键字class

  1. 类是抽象的(class定义的是类)
  2. 对象是具体的(具体在函数中定义类型+变量名)

1.2类中成员的访问权限

  1. public:都可
  2. private:类内可
  3. protected:子父可

1.3代码

#include <iostream>
#include <string>

using namespace std;

class MoonCake {
	string inside;
	string outside;
	string taste;
public:
	void init(string inside, string	outside, string taste);
	void Tase();
};

void MoonCake::init(string inside, string outside, string taste) {
	this -> inside = inside;
	this -> outside = outside;
	this -> taste = taste;
}

void MoonCake::Tase() {
	cout << taste << endl;
}

int main(int argc, char *argv[])
{

	MoonCake wuRen;
	wuRen.init("aa", "bb", "cc");
	wuRen.Tase();
	return 0;
}

二、构造函数

2.1提出

对 不同区域的定义的变量确定初值,如堆区,.bss,栈区初值不同

2.2格式

类名() {

}

2.3特点

类名做函数名,没有返回值

可以有参数,也可以无参

2.4代码

class Boy {
    int age;
public:
	Boy() {
        age = 0;
    }    	
    Boy(int age = 0) {
        this->age = age;
    }
};

int main() {
    Boy boy;				//正确,age初值为0
    Boy boy();				//错误,会产生歧义,即使注释掉无参的依旧会歧义,不可这么定义
    return 0;
} 

三、析构函数

3.1特点

~类名做函数名,没有返回值,没有参数,不能重载

3.2析构函数格式

~Boy() {
    //delete i_p;
}

四、初始化列表

4.1语法规则

类名(形参1, 形参2) :成员变量1(形参1), 成员变量2(形参2) {

}

4.2代码

class Test {
    const int n;
    int *i_p;
    string str;
public:
    Test(int i):n(i), i_p(new int(i)), str("hello") {
        cout << "n = " << n << nendl;
        cout << "*i_p = " << i << endl;
        cout << "str = " << str << endl;
    }
};

4.3注意

形参列表是在构造函数之前执行的,形参列表实现的是真正的初始化操作,即开辟内存空间的同时赋初值

五、string

可以直接等号赋值(strcpy),可以加号链接(strcat),可以直接进行逻辑比较(strcmp)

5.1代码

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{

	string str1, str2;
	str1 = "hello ";
	str2 = "world";
	cout << str1 << endl << str2 << endl << str1+str2 << endl;
    if (str1 == "hello ") {
        cout << "str1 == hello\n"; 
    }
	return 0;

}

六、new和delete

对比malloc和free

6.1代码

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

	int *ptr = new int;
	cout << *ptr << endl;
	delete ptr;
	int *ptr1 = new int(5);
	cout << *ptr1 << endl;
	int *ptr2 = new int[5];
	ptr2[3] = 6;
	cout << ptr2[3] << endl;
	delete ptr1;
	delete[] ptr2;
	return 0;

}

6.2new和malloc辨析

  1. 两者都用来在堆区申请内存
  2. new是关键字,malloc是标准C库的函数
  3. new以数据类型自动开辟空间,malloc以字节为单位通过用户传递字节的大小申请内存空间
  4. new可以对堆区内存空间初始化,malloc不行

七、拷贝构造函数

7.1深拷贝和浅拷贝

  1. 浅拷贝只是指针的指向相同,即两个对象的指针指向同一片内存
  2. 深拷贝是给新对象另开辟一块空间,并使其值和前对象相同

八、模块化编程

8.1代码

  1. .h

    #ifndef __OPERATER_H__
    #define __OPERATER_H__
    #define ZEROMIN 1e-6
    class Operater {
    	char mop;
    	double mnum1;
    	double mnum2;
    public:
    	Operater();
    	Operater(Operater& that);
    	~Operater();
    	double result();
    	void Set(char mop, double mnum1, double mnum2);
    
    };
    #endif
    
  2. .cpp

    #include <iostream>
    #include "Operater.h"
    
    using namespace std;
    
    Operater::Operater() {
    	mop = 0;
    	mnum1 = 0;
    	mnum1 = 0;
    }
    Operater::Operater(Operater& that) {
    	mop = that.mop;
    	mnum1 = that.mnum1;
    	mnum2 = that.mnum2;
    }
    Operater::~Operater() {
    	cout << "~Operater()\n";
    }
    double Operater::result() {
    	switch (mop) {
    		case '+':
    			return mnum1 + mnum2;
    			break;
    		case '-':
    			return mnum1 - mnum2;
    			break;
    		case '/':
    			if (mnum2 >= -ZEROMIN && mnum2 <= ZEROMIN) {
    				cout << "zero !\n";
    				return -1;
    			}
    			return mnum1 / mnum2;
    			break;
    		case '*':
    			return mnum1 * mnum2;
    			break;
    		default:
    			cout << "mop err!\n";
    			break;
    	}
    	return 0;
    }
    
    void Operater::Set(char mop, double mnum1, double mnum2)  {
    	this ->mop = mop;
    	this ->mnum1 = mnum1;
    	t
    
  3. main.cpp

    #include <iostream>
    #include "Operater.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
    	Operater o1;
    	o1.Set('/', 0, 0);
    	cout << o1.result() << endl;
    	o1.Set('+', 5, 6);
    	cout << o1.result() << endl;
    
    	return 0;
    
    }
    
    

九、this指针

9.1引子

成员函数不占内存空间

均放在代码段

9.2 this指针

  1. this指针是类中的一个隐藏的成员变量
  2. 指向对象的首地址
  3. 只有在成员函数内可见
  4. 类外不可见,初始化列表也不可见

9.3 用法

 #include <iostream>

using namespace std;

class Test {

	int i;
public:
	Test& autuadd() {
		cout << i << endl;
		i++;
		return *this;
	}
	Test() {
		i = 0;
	}
	
};

int main(int argc, char *argv[])
{

	Test t1;
	t1.autuadd().autuadd().autuadd();
	return 0;

}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值