您的位置:首页 > 编程语言 > C语言/C++

5-24 c++语言之【基础知识】

2019-05-24 13:45 1456 查看

  最近一段时间继续开始了c++的学习,作为c plus plus 难免会与c语言做一个对比,很明显的感受到c++语言注重代码的复用性和拓展性,而c语言更加注重其算法的高效性,这也是今后需要注意的地方,避免用c++语言写出c语言的思想,接下来就是正文

关于c++的语言特点:

  1. c++是一门面向对象的语言(很重要)
  2. 面向对象的三大特点(封装,继承,多态)
  3. c++库函数的头文件没有.h
  4. endl 换行并清空缓冲区

第一个,命名空间作用域

首先  :: 为作用域运算符(其中一个用法),在初写c++程序中有这样一段 using namespce std;也就是打开命名空间std ,有了这样一句话,才能使用标准库函数里的函数,其次,:: 前没有内容代表是全局的,

那么由此看来作用就如下,1. 确定了哪个命名空间下的哪个变量 2.区分系统函数和自定义函数 

实例

#include<iostream>
using namespace std;
int nAge = 500;
namespace Bird
{
int nAge = 20;
}
namespace Person
{
int nAge = 100;
}
int main()
{
cout << Bird::nAge << endl;
cout << Person::nAge << endl;
cout << ::nAge << endl;

system("pause");
return 0;
}

 

第二个,动态内存的分配与释放,静态内存和动态内存的区别就在于是不是在编译期系统自动分配内存,而动态内存的申请在c语言中是malloc函数,在c++中变为new函数,(而在源码中可以看到也是通过malloc去实现的)而new函数的参数列表不再是指定的大小,而是直接写类型就可以了,而同样是在堆区申请,得手动释放,就得用delete函数

实例

#include<iostream>
using namespace std;
int main()
{
int *p = new int;
cin >> *p;
cout << *p << endl;

int *arr = new int[];
delete[] arr;
arr = NULL;

int *arr1 = new int(10);
cout << *arr1 << endl;
delete arr1;
arr1 = NULL;

struct Node
{
int i;
char b;
char c;
};
Node *p = new Node;
delete p;
p = NULL;

system("pause");
return 0;
}

 

第三个,范围for   bool类型,首先c++可以在任意地方定义变量,不同于c语言只能在所用变量的上方定义变量,而我想说的范围for 是在c++新标准中定义的,可以实现自动遍历,但缺点是只能从头遍历,

而bool类型原来在c语言中并没有,是通过宏定义自己定义的,在c++中就规定了bool类型,区别于windows文件中的BOOL,其大小就不一样,BOOL为4个字节,bool为1个字节

实例

#include<iostream>using namespace std;
int main()
{
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
for(int nVal : arr)
cout << nVal << endl;
system("pause");

}

 

第四个,string 字符串类型,在c++中,关于字符串的使用无疑是大大方便了程序员,只要引用了<string>,拼接,判断,复制,只需要用 + == =来实现就可以

实例

#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "aaa";
str = "aaasssddd";
cout << str << endl;

str += "wer";
cout << str << endl;

if(str == "aaa")
cout << "YES!" << endl;
return 0;
system("pause");

}

 

第四个,函数重载 ,函数默认参数,函数重载是指在同一个作用域,名字一样,参数列表不同的函数,那么在编写程序的时候即使函数名一致,而参数列表不用,在调用的时候,编译器会根据你在调用时的参数列表自己找到相应的函数,而默认参数是指,在声明函数的时候有些值先赋上,默认是从右向左,并且中间不能为空,传入时是从左向右,所以即使调用时不赋值,但是也必须从右向左按顺序

实例

#include<iostream>
using namespace std;
/*void Show(int a,int b);    */                //重载函数
void Show(int a);
void Show(int a,int b,int c = 0,int d = 1);    //重载默认参数从左向右进行设置默认参数

int main()
{

Show(1);
Show(2,3);

system("pause");
return 0;
}
//void Show(int a,int b)
//{
//    cout << a * b << endl;
//}
void Show(int a)
{
cout << 3.14 * a << endl;
}
void Show(int a,int b,int c,int d)
{
cout << a << " " << b << " " << c << " " << d << endl;
}

 

第五个,引用,引用的作用定义两个变量指向同一个地址空间,相当于给变量起别名,但是定义引用必须进行初始化,并且,引用只能用一次如果用第二次的话就是赋值,最后引用的必须是一个合法的存储单元,像NULL就不可以进行引用,引用的符号是&,接下来利用引用来交换两个数的值

#include<iostream>
using namespace std;

void Swap(int &a,int &b);
int main()
{
int a = 100;
int b = 200;
Swap(a,b);
cout << a << " " << b << endl;

system("pause");
return 0;
}
void Swap(int &a,int &b)
{
int Temp = NULL;
Temp = a;
a = b;
b = Temp;
}

 

第六个,函数的参数传递,函数的传递方式在c语言中说了两种,值传递,地址传递,在c++中加入一个引用传递,区别在于值传递改变不了传递的内容,但是其他两种可以,一个是通过地址进行修改,另一个是通过给一个变量起的别名进行修改,接下来可以尝试利用引用来进行链表添加

#include<iostream>
using namespace std;
struct Node
{
int id;
struct Node *next;
};
void AddNode(Node **ppHead,Node **ppEnd,int id);
int main()
{
Node *pHead = NULL;
Node *pEnd = NULL;

system("pause");
return 0;
}
void AddNode(Node **ppHead,Node **ppEnd,int id)
{
Node *pNode = new Node;
pNode->id = id;
pNode->next = NULL;
}

 

第七个 类 对象 ,这是c++学习中最重要和基础的部分,

类的关键字是class,其后加类名,类中可以装变量和函数,而通过类声明的变量叫做对象,在类中有三类访问修饰符:

  1. public 公共的,所有位置都可使用
  2. protected 受保护的,只有本类和派生类可以用
  3. private 只有本类可以使用

而类的定义方式和结构体很相似,结构体默认为public,类默认为private,先做一个小练习,定义一个雇员类

#include<iostream>
using namespace std;
class CEmployee
{
private:
int m_nAge;
int m_nYear;
int m_nMoney;
public:
void SetAge(int Age)
{
m_nAge = Age;
}
void SetYear(int Year)
{
m_nYear = Year;
}
void SetMoney(int Money)
{
m_nMoney = Money;
}
public:
int GetAge()
{
return m_nAge;
}
int GetYear()
{
return m_nYear;
}
int GetMoney()
{
return m_nMoney;
}
public:
void Show()
{
cout << m_nAge << endl;
cout << m_nYear << endl;
cout << m_nMoney << endl;
}
};
using namespace std;
int main()
{
CEmployee ps1;
ps1.SetAge(20);
ps1.SetYear(10);
ps1.SetMoney(20000);

CEmployee ps2;
ps2.SetAge(25);
ps2.SetYear(2);
ps2.SetMoney(2000);

ps1.Show();
ps2.Show();

system("pause");
return 0;
}

通过这段代码可以很清晰的看出什么是面向对象,在主函数中一切都是对象去调用函数,我们所注重的就是把对象这个类去完善,不知道大家有没有发现一个问题,在这个雇员类中的成员属性,是私有的,如何进行赋值和初始化呢,这个就需要介绍接口了,既然私有类只能在本类使用,那么我就在本类中定义一个公有的函数去初始化私有的属性,这个函数就叫接口,接口的形式有很多,但目的都是接通私有的,用户可以进行我规定的修改。

 

第八个,构造析构,在类中有一个特殊的函数,叫构造函数,它的名字同类名,并且没有返回值,之所以特殊是因为,在主函数创建对象的时候,构造函数自动调用,一般是用来给成员属性初始化使用,而析构函数,它的名字就是在类名前加一个取反~符,不允许赋参数,用来删除类内指针成员在堆区分配的空间,它也很特殊,在对象生命周期结束时,自动进行调用,非常重要的一点,构造函数可以有多个,但析构函数只能有一个,

实例

#include<iostream>
#include<string>
using namespace std;
class CPerson
{
public:
//-----------------------构造函数-----------------------
CPerson()
{
m_nAge = 1;
b_Sex = true;
str_Name = "bb";
}
CPerson(string Name)
{
m_nAge = 1;
b_Sex = true;
str_Name = Name;
}
//-----------------------构造函数-----------------------
private:
int m_nAge;
bool b_Sex;
string str_Name;
public:
void Show()
{
cout << m_nAge << endl;
cout << b_Sex << endl;
cout << str_Name << endl;
}
public:
~CPerson()
{

}
};

int main()
{
CPerson ps;
ps.Show();

CPerson ps1("cyc");
ps1.Show();
//CPerson ps1;

system("pause");
return 0;
}
#include<iostream>
using namespace std;
class CPerson
{
private:
char *psz_Name;
public:
CPerson()
{
psz_Name = new char[10];
strcpy_s(psz_Name,10,"cyc");
}
//-----------------析构函数------------------------
~CPerson()
{
delete[] psz_Name;
psz_Name = NULL;
}
//-----------------析构函数------------------------
public:
void Show()
{
cout << psz_Name << endl;
}
};

int main()
{
CPerson ps;
ps.Show();

system("pause");
return 0;
}

 

针对以上,做了一个小练习,编写学生类的链表,首先需要认识的是,c++设计中的原则之一,单一职责(一个类中只装和这个类中有关的)目的是为了增加复用性,那么设计一个学生类,一个链表类就可以了,代码如下

#include<iostream>
#include<string>
using namespace std;
class CStudent
{
private:
string m_strName;
int m_nValue;
public:
CStudent()
{
m_strName = " ";
m_nValue = 0;
}
public:
void SetInfo(int id,string name)
{
m_nValue = id;
m_strName = name;
}
void Show()
{
cout << m_nValue << " " << m_strName << endl;
}
};

class CList
{
private:                                    //考虑类的时候,以对象的属性进行考虑,比如在链表中类的属性就包括长度,结点,头尾结点
struct Node
{
CStudent id;
struct Node *next;
};
int m_nLen;
Node *m_pHead;
Node *m_pEnd;
public:
CList()
{
m_pHead = NULL;
m_pEnd = NULL;
m_nLen = 0;
}
~CList()
{
while(m_pHead)
DeleteNode();
}
public:
void AddNode(CStudent std)                    //在传参的时候,类的属性可以看作类内的全局变量,不用外部传入,比如人的年龄,在人这个类中就可以使用,不用外部进行传入
{
Node *pNode = new Node;
pNode->id = std;
pNode->next = NULL;

if(m_pHead == NULL)
{
m_pHead = pNode;
}
else
{
m_pEnd->next = pNode;
}
m_pEnd = pNode;
m_nLen++;
}
void DeleteNode()
{
if(m_pHead == NULL)
return;
if(m_nLen == 1)
{
m_pHead = NULL;
m_pEnd = NULL;
m_nLen = 0;
}
if(m_nLen > 1)
{
Node *pDel = NULL;
pDel = m_pHead;
m_pHead = m_pHead->next;
delete pDel;
pDel = NULL;
}
}
void Show()
{
Node *pMark = m_pHead;
while(pMark)
{
pMark->id.Show();
pMark = pMark->next;
}
}

};
int main()
{
CStudent std1;
std1.SetInfo(1,"cyc");
CStudent std2;
std2.SetInfo(2,"xmx");
CStudent std3;
std3.SetInfo(3,"wdh");
CStudent std4;
std4.SetInfo(4,"pjh");

CList list;
list.AddNode(std1);
list.AddNode(std2);
list.AddNode(std3);
list.AddNode(std4);
list.Show();
system("pause");
return 0;
}

 

第九个,对象的种类,做一个表格吧

 

对象种类

生命周期

1

栈区的局部对象 在声明的大括号结束时结束

2

堆区的指针对象 遇到delete结束

3

main函数外的全局对象 程序结束时结束

4

临时对象 当前一行结束时结束

前三个都很好理解,通过下断点调试就可以获得,而临时对象有很多种用法,其中一个用法就是作为一个临时变量存在,接着函数的返回值,

为什么c++中申请空间用new而不用malloc,其中最大的原因就是new可以出发构造和析构

 

第十个,this指针

在说this指针之前,首先要明白几件事,1. 空类的大小是1个字节,占位。2.成员变量,在创建对象的时候分配空间,而成员函数,编译的时候就放在代码区中,只有一份

那么问题就来了,既然成员函数只有一个,那多个对象调用同一个函数的时候,如何确定函数参数列表中的参数是属于哪个对象的成员变量的呢?

这里就涉及到了this指针,this指针存在于成员函数的参数列表之中,一般情况下是不显示的,用来装调用者对象的地址,这样就能连接类里变量和函数,写一个实例,用注释的方式把隐藏的部分写出来

this指针还有一个作用就是区分,成员函数使用的变量是全局的,还是类内的,类内就在对象前加this->

#include<iostream>
using namespace std;
int m_nAge = 0;

class CPerson
{
private:
int m_nAge;
public:
CPerson(int Age):m_nAge(Age)
{

}
public:
void Show(/*CPerson *this = &ps*/)
{
cout << ::m_nAge << endl;
cout << this->m_nAge << endl;

}
};

int main()
{
CPerson ps(100);
cout << &ps << endl;
ps.Show(/*&ps*/);

system("pause");
return 0;
}

 

第十一  const      static

在介绍这两个关键字之前,先介绍初始化列表,初始化列表的结构    构造函数名:成员属性(初始化的值) ,如果有多个成员属性需要初始化的话,那么先定义的就先初始化,初始化顺序就是定义成员变量的顺序

而还有需要使用初始化列表的情况,就是一个对象在另一个类中使用,而这个对象作为另一个类的成员属性,需要初始化,此时的初始化就需要在初始化列表中初始化,举个例子吧

//----------------------------初始化列表的顺序------------------------------------
#include<iostream>
using namespace std;
class CPerson
{
private:
int m_nAge;
int m_nSex;
public:
CPerson(int i):m_nAge(i),m_nSex(m_nAge)
{
cout << m_nAge << endl;
cout << m_nSex << endl;
}

};
int main()
{
CPerson ps(100);

system("pause");
return 0;
}
//----------------------------------在类中对其他对象初始化-------------------------
#include<iostream>
using namespace std;
class CChina
{
private:
int m_nAge;
public:
CChina(int a):m_nAge(a)
{

}
public:
void Show()
{
cout << m_nAge << endl;
}

};

class CPerson
{
private:
CChina china;
public:
CPerson():china(123)            //通过初始化列表给类中对象初始化
{
china.Show();
}

};
int main()
{
CPerson ps;

system("pause");
return 0;
}

接下来就介绍关键字 const static 这两个在c语言中曾经介绍过,const 限定一个变量不允许被改变,static 则是声明了一个静态变量,先来列个表,展示一下两者在类中的区别

 

const

static

成员常变量/静态变量

只能在初始化列表中初始化 只能在类外进行初始化

常函数/静态函数

只能使用成员属性,不能修改 只能使用静态成员属性

常对象

只能使用常函数  

1.static作为静态变量声明的关键字,也是类似于成员函数(可以声明后通过查看类的大小进行验证)在编译器就存在了,并且所有的对象共享,可以在类外直接初始化,也可以 类名::静态变量 进行初始化

2.静态函数之所以只能使用静态成员属性的本质原因:static函数没有this指针,无法确定使用的是哪个对象的,但是静态变量可以共享,所有静态函数可以无this指针的使用静态成员属性,如果自己实在想使用,也可以手动加一个this指针

3.const有一个特点就是定义就必须初始化,而且不可修改

4.常函数只能使用成员属性的本质原因:常函数的this指针也是const类型的,*this是不可修改的,这也解释了为什么常对象只能用常函数,因为常对象的类型是  const 类名   *  和常函数的指针类型相符,普通对象的类型是类名 *,安全性可以提升但不能减低。

#include<iostream>
using namespace std;

class CPerson
{
private:
const int m_nAge;
public:
CPerson():m_nAge(100)
{

}
public:
void Show(/*const CPerson *this = const CPerson * */) const
{
cout << m_nAge << endl;
}
void Showshow(/*CPerson *this = CPerson * */)        //之所以常量对象不能调用常量函数就是因为 this指针的类型不符
{
cout << m_nAge << endl;
}

};
int main()
{
const CPerson ps;
ps.Show(/*const CPerson * */);
//    ps.Showshow(/*CPerson * */);

system("pause");
return 0;
}
#include<iostream>
using namespace std;

class CPerson
{
public:
static int m_nAge;
int m_nSex;
public:
CPerson():m_nSex(500)
{
}
public:
static void Show()                                        //本质在于函数列表内没有this指针,存储本对象的地址
{
cout << m_nAge << endl;
//cout << m_nSex << endl;
}
static void ShowShow(CPerson *pThis)                    //手动添加的可以使用非静态成员变量的静态成员函数
{

cout << pThis->m_nSex << endl;
}

};

int CPerson::m_nAge = 100;

int main()
{

CPerson ps;
ps.ShowShow(&ps);

CPerson ps1;
CPerson::m_nAge = 200;
ps.Show();

system("pause");
return 0;
}

 

 

接下来就要研究 类之间的关系了,基础知识就介绍到这里了

 

 

2019-05-24 13:42:38 编程小菜鸟自我反思,大佬可以提出自己的意见和建议,谢谢!!!

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: