从C到C++及类与对象

语法升级

c++中 &不仅有取地址的作用,还能作为引用
引用 (&) : 可以理解为是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样

#include <stdio.h>

int main()
{
	int a = 100;
	int &b = a;
	printf("a = %d\n",a);
	printf("b = %d\n",b);

	printf("addr:a = %p\n",&a);
	printf("addr:b = %p\n",&b);	
}

打印结果为

a = 100
b = 100
addr:a = 0x7ffd52f10b34
addr:b = 0x7ffd52f10b34

引用的作用示例
在C语言中调换2个变量的值,需要通过指针操作,比较复杂,引用可以简化这种操作

//指针操作对换变量数值
#include <stdio.h>

void swap(int *p, int *q){
	*p ^= *q;   //0110 0100 ^ 0000 1010 = 0110 1110
	*q ^= *p;   //0000 1010 ^ 0110 1110 = 0110 0100 
	*p ^= *q;   //0110 1110 ^ 0110 0100 = 0000 1010 
}

int main()
{
	int a = 100;  //0110 0100
	int b = 10;   //0000 1010
	printf("a = %d, b = %d\n", a,b);
	swap(&a,&b);
	printf("a = %d, b = %d\n", a,b);

}

打印结果为

a = 100, b = 10
a = 10, b = 100
//c++中的引用操作
#include <stdio.h>

void swap(int &a, int &b){
	a ^= b;   //0110 0100 ^ 0000 1010 = 0110 1110
	b ^= a;   //0000 1010 ^ 0110 1110 = 0110 0100 
	a ^= b;   //0110 1110 ^ 0110 0100 = 0000 1010 
}

int main()
{
	int a = 100;  //0110 0100
	int b = 10;   //0000 1010
	printf("a = %d, b = %d\n", a,b);
	swap(a,b);
	printf("a = %d, b = %d\n", a,b);

}

打印结果为

a = 100, b = 10
a = 10, b = 100

概念和思维升级

函数默认值,可以给变量赋个初始值,没有传参数时,就打印默认值,有传参时,就打印传的值。

#include <stdio.h>
void debug(const char *ptr = "-------------"){
	printf("%s\n",ptr);
}
int main(){
	debug();
	debug();
	debug();
	debug();
	debug("hello");
	debug("world");
}

打印效果
在这里插入图片描述
函数重载,可以用相同的函数名声明2个不同功能的函数

#include <stdio.h>
#include <string.h>

/*C语言的做法,需要2个不同的函数名
int intcmp(int a, int b)
{
	return a-b;
}

int scmp(const char *str1, const char *str2)
{
	return strcmp(str1, str2);
}
*/
//c++中可以用2个相同的函数名
int cmp(int a, int b)
{
	return a-b;
}

int cmp(const char *str1, const char *str2)
{
	return strcmp(str1, str2);
}
int main()
{
	printf("%d\n", cmp(1, 2));
	printf("%d\n", cmp("aaaaaa", "bbbb"));
}

堆内存的分配 C语言中使用malloc申请内存,free释放内存,C++中用new申请内存,delete释放内存
用法示例

#include <stdio.h>
#include <malloc.h>
#include <string.h>

int main()
{
/*C语言中的做法
	char *p = (char *)malloc(10);
	strcpy(p, "hello");

	printf("p: %s\n", p);

	free(p);
*/


	int *intp = new int;//(int*)malloc(sizeof(int));颗粒内存

	*intp = 100;

	printf("*intp = %d\n", *intp);

	delete intp;
/
	char *p = new char[10];//堆内存

	strcpy(p, "hello");

	printf("p: %s\n", p);

	delete [] p;
	
}

类与对象

思想:方法和数据不应该分开,要绑定在一起。(拿到对象,就能拿到操作方法)
例如:
人可以作为对象,属性有姓名、年龄、身高、体重…,行为有走、跑、跳、吃饭、唱歌…

​ 车也可以作为对象,属性有轮胎、方向盘、车灯…,行为有载人、放音乐、放空调…

​ 具有相同性质的对象,我们可以抽象称为,人属于人类,车属于车类
C语言的例子(创建一个数组,往数组的尾端加数据)(有封装思想,数据和方法都封装了,但是数据和方法却没有被整体的封装)

//arr.h
#ifndef _ARR_
#define _ARR_

typedef struct arr{
	int data[100];
	int tail;
}ARR;

void init(ARR *arr);
void addtail(ARR *arr, int data);
void show(ARR *arr);

#endif
//arr.c
#include "arr.h"
#include <stdio.h>

void init(ARR *arr)
{
	arr->tail = 0;
}

void addtail(ARR *arr, int data)
{
	arr->data[arr->tail++] = data;
}

void show(ARR *arr)
{
	int i = 0;
	for(;i<arr->tail; i++)
		printf("%d, ", arr->data[i]);
	printf("\n");
}
main.c
#include "arr.h"

int main()
{
	ARR arr;
	init(&arr);

	int n = 10;
	while(n--)
		addtail(&arr, n);

	show(&arr);
	
	ARR arr1;
	init(&arr1);

	int i = 0;
	for(;i<10; i++)
		addtail(&arr1, i);

	show(&arr1);
}

打印的结果为

9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 

C语言的例子(创建一个数组,往数组的尾端加数据)(有更高的封装思想,将数据和方法整体封装,使用者不知道具体数的操作是怎么样的,只有正确初始化Init,才能间接的拿到addtail和show)

//arr.h
#ifndef _ARR_
#define _ARR_

typedef struct arr{
	int data[100];
	int tail;

	//void (*init)(struct arr *arr);//②会出现段错误
	void (*addtail)(struct arr *arr, int data);//④不完全声明
	void (*show)(struct arr *arr);//⑤不完全声明
}ARR;
void init(struct arr *arr); //③把这个函数拿出来,由用户初始化,通过这个函数调用剩下的两个函数④⑤进行声明的实现

#endif
//arr.c
#include "arr.h"
#include <stdio.h>


static void addtail(ARR *arr, int data)
{
	arr->data[arr->tail++] = data;
}

static void show(ARR *arr)
{
	int i = 0;
	for(;i<arr->tail; i++)
		printf("%d, ", arr->data[i]);
	printf("\n");
}

void init(ARR *arr)//③
{
	arr->tail = 0;

	arr->addtail = addtail;
	arr->show = show;
}
//main.c
#include "arr.h"

int main()
{
	ARR arr;//①构造的是一个空白的对象,也就是说是随机的,所以里面的三个指针函数指向哪也是不可预知的。⑥
	init(&arr);//⑦

	int n = 10;
	while(n--)
		arr.addtail(&arr, n);//⑧

	arr.show(&arr);
//	arr.tail = 0;

	arr.show(&arr);

}

打印的结果为

9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 

但是这种封装还不算是最高级的,因为使用者可以操作结构体的成员,导致整个结构体数据的破坏。还有声明⑥⑦和调用⑧的过程不够面向对象,所以C++引用了类

类的申明

封装的意义:将属性和行为作为一个整体,叫做类,来表现生活中的事物。
1.属性 成员属性、成员变量
2.行为 成员函数、成员方法
将属性和行为放在不同的权限下,加以控制:

权限关键字作用特殊
公共权限public类的内外都可以访问X
保护权限protected类内可以访问,类外不可以访问子类可以访问父类成员
私有权限private类内可以范根,类外不可以访问子类不可访问父类成员
class 类名
{
private:
	私有的数据和成员函数;
public:
	公用的数据和成员函数;
protected:
	保护的数据和成员函数
};

类关键字声明示例

//arr.h
#ifndef _ARR_
#define _ARR_
/*
typedef struct arr{
	int data[100];
	int tail;

	//void (*init)(struct arr *arr);
	void (*addtail)(struct arr *arr, int data);
	void (*show)(struct arr *arr);
}ARR;
void init(struct arr *arr); 
*/
class ARR{
public:
	ARR():tail(0){
//		tail = 0;	
	}
	void addtail(int data);
	void show(void);
private:
	int data[100];
	int tail;
}
#endif
/*
#include <stdio.h>
class A{
public:
	void show()
	{
		printf("vvvvvvvvvvv\n");
	}
	void setdata(int data)//可以在类里面直接写一个函数
	{
		a = data;
	}
	int getdata(void);//⑨也可以在类里面声明一个函数
private:
	int a;
}
int A::getdata(void)//⑩然后在外面写函数的内容,只需要在函数前加A::
{
	return a;
}
*/

#include "arr.h"
#include <stdio.h>
/*
static void addtail(ARR *arr, int data)
{
	arr->data[arr->tail++] = data;
}

static void show(ARR *arr)
{
	int i = 0;
	for(;i<arr->tail; i++)
		printf("%d, ", arr->data[i]);
	printf("\n");
}

void init(ARR *arr)//③
{
	arr->tail = 0;

	arr->addtail = addtail;
	arr->show = show;
}
*/
void ARR::addtail(int data)
{
	this->data[tail++] = data;//因为有重名data,所以需要加个this区别开
}

void ARR::show(void)
{
	int i = 0;
	for(;i<tail; i++)
		printf("%d, ", data[i]);//因为没有人来抢这个data,所以不用加this,编译器知道指向谁
	printf("\n");
}
/*
void init(ARR *arr)//因为上面C++已经做好了这些初始化,所以这个init也没有必要了
{
	arr->tail = 0;

	arr->addtail = addtail;
	arr->show = show;
}
*/
//main.c
#include "arr.h"

int main()
{
/*
	ARR arr;
	init(&arr);

	int n = 10;
	while(n--)
		arr.addtail(&arr, n);

	arr.show(&arr);
//	arr.tail = 0;  //这种操作会破坏数据,导致show的内容为空
	arr.show(&arr);
*/
	ARR arr;
	
	arr.addtail(1);
	arr.addtail(2);
	arr.addtail(3);
	arr.addtail(4);
	arr.show();
}

打印结果为

1234

类的成员函数(类内/类外实现)

构造函数(类的初始化,创建类时自动调用;初始化表,this指针)
	默认构造函数A();
	拷贝构造函数A(const A &x); //深浅拷贝问题
	赋值构造函数operator=?;
析构函数(类的解构,销毁类时自动调用)
	~A();
普通成员函数

代码示例

#include <stdio.h>

class A{
public:
	A()//函数创建的时候被主动调用
	{
		printf("A()\n");
	}

	A(int data)
	{
		printf("A(int data)\n");
	}
	~A(){//当对象被销毁的时候会被调用
		printf("A~~~~~~~~~~~~~\n");
	}
};

int main()
{
	//called when new object is created!

	A *p = new A(1000);
	A  x;
	A  m(100);//强调构造这个函数调用的构造函数是这种带整型的构造函数
	A  y = 10;//只要产生一个新的对象就会调用构造函数,隐式的调用构造函数实现一次构造
	A  z = y;//拷贝构造函数,调用的是系统的默认构造函数,这边没填参,但是析构函数函数填参了,所以会多打印一个A~~~~~~~~~~~~~
}

打印结果为

A(int data)
A()
A(int data)
A(int data)
A~~~~~~~~~~~~~
A~~~~~~~~~~~~~
A~~~~~~~~~~~~~
A~~~~~~~~~~~~~

浅拷贝示例(拷贝的时候只拷贝了数据,但是没有给新的数据赋予新的地址,所有会有2个数据公用一个地址,当被拷贝的数据被释放时,就会出现段错误(释放2次内存),因为拷贝的数据没有地址了。类似于拷贝了一个桌面快捷方式,实际应用程序没有拷贝,所以当应用程序被删除时,拷贝的快捷方式也会失效)

#include <stdio.h>
class A{
public:
	A()
	{
		printf("A()\n");
		p = new char[10];
	}
	~A()
	{
		printf("~A()\n");
		delete [] p;
	}
private:
	char *p;
};
int main()
{
	A x;
	A y = x;
}

打印结果为

free(): double free detected in tcache 2
run: line 1:3 Aborted (core dumped) LD_LIBRARY_PATH=/usr/local/gcc-9.2.0/lib64 ./a.out

可以通过拷贝构造函数修改上面代码

#include <stdio.h>
#include <string.h>
class A{
public:
	A()
	{
		printf("A()\n");
		p = new char[10];
		strcopy(p,"hello");
	}
	A(const A &x)
	{
		printf("A(const A &x)\n");
		p = new char[10];
		strcpy(p,x.p);
	}
	~A()
	{
		printf("~A()\n");
		delete [] p;
	}
private:
	char *p;
};
int main()
{
	A x;
	A y = x;
}

打印结果为

A()
A(const A &x)
~A()
~A()

但是这个只能解决调用了构造函数(A y = x;)的情况,要是使用者通过这样(y = x;)来操作,这样就没有调用到构造函数,所以还是会出现问题,需要继续学后面的内容去解决。

常成员静态成员与友元

常成员、常对象(C++推荐const而不用#define,mutable)
(const数据成员只在某个对象生存期内是常量,而对于整个类而言却是可变的(static例外))。

常数据成员(构造函数初始化表赋值)
class A{
public: A():x(100){}
const int x;
};
常成员函数
void func() const{}
常对象
const A a;

示例

#include <stdio.h>
class A{
public:
	A(int a = 50, int data = 1000):b(data){//:b(data) 称为初始化表赋值
//		b = data; // 上面的b(data)效果为这样,且在常数据成员中只能像上面这么写
		this->a = a;
		printf("AAAAAAAAAAAAAA\n");
	}
	~A(){
		printf("~~~~~~~~~~\n");
	}
	void show(void) const
	{
		printf("b = %d\n",b);
		printf("a = %d\n",a);
//		a++;//会报错,const不允许被操作
	}
private:
	int a;
	const int b;
};
int main ()
{
	A x(10);
	x.show();
	A y(100);
	y.show();
	A z;
	z.show();
}

打印结果为

AAAAAAAAAAAAAA
b = 1000
a = 10
AAAAAAAAAAAAAA
b = 1000
a = 100
AAAAAAAAAAAAAA
b = 1000
a = 50
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~

静态成员(属于类不属于对象)

静态成员的申明
static int x;
static const int x = 10;
静态数据成员初始化
类外:static int A::x = 10;
静态成员函数
static void func();//能访问静态成员
调用方法 A::func();

示例(不需要基于对象修改成员,可以基于类来修改)

#include <stdio.h>
class A{
public:
	static void func(void)
	{
		printf("vvvvvvvvvvvv\n");
	}
	static int data;
};
int A::data = 10;
int main()
{
	A a;
	a.func();
	A::func();

	A x;
	x.data = 100;
	printf("x.data = %d\n",x.data);
	A::data = 1000;
	printf("x.data = %d\n",x.data);
}

打印结果为

vvvvvvvvvvvv
vvvvvvvvvvvv
x.data = 100
x.data = 1000

友元(破坏封装:让别人可以使用私有成员)

友元类
friend class B;
友元函数
friend void func();
友元成员函数
friend void B::func();

示例(对private的成员进行操作,给他取别名。不通过友元的方式会报错)

#include <stdio.h>

class A;

class B{
public:
	void printfA(A &x);	
};

class A{
public:
	A()
	{
		x = 100;
	}

//	friend class B;
	friend void B::printfA(A &x);
private:
	int x;
};


void B::printfA(A &x)
{
	printf("%d\n", x.x);
}

int main()
{

	A a;
//	printf("%d\n", a.x);

	B b;
	b.printfA(a);
}

打印结果为

100
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值