目录
01:什么是命名空间?其作用是什么?匿名命名空间有什么特点?
03:什么是常量指针和指向常量的指针?什么是数组指针和指针数组?什么是函数指针和指针函数?请举例说明。
04:new/delete与malloc/free的区别是什么?(面试常考)
08:在C++中,在堆上申请空间存放字符串内容的通用做法是怎样,用new表达式完
是好的只能说差不多的我,和被灌酒倒下的开哥
01:命名空间
namesapce.cc
#include <iostream>
/* using std::cout; */
/* using std::endl; */
using namespace std;
namespace wd{
int num=10;
void print(){
cout<<"爱来自CPP miao: wd::print()"<<endl;
}
}//enf of namespace wd
//可以加分号可以不加,建议不加
/* using namespace wd; */
namespace cd{
int num=10;
}//end of namespace cd
void test(){
using namespace wd;
wd::print();
cout<<wd::num<<endl;
cout<<endl;
print();
cout<<num<<endl;
}
int num=100;
void test01(){
using namespace cd;
cout<<endl;
/* cout<<num<<endl; */
cout<<cd::num<<endl;
/* using编译指令冲突,少用 */
using wd::num;
/* using cd::num; */
//chong tu
cout<<num<<endl;
//using声明会对全局的同名内容起到屏蔽作用
}
int main(void)
{
test();
return 0;
}
anoymous.cc
#include <iostream>
using std::cout;
using std::endl;
namespace {
int num=10;
void print(){
cout<<"爱来自CPP miao: wd::print()"<<endl;
}
}//enf of namespace anoymous
//匿名空间中的实体不能跨文件(模块)使用
//不要和全局变量取一样的名字喵
//static
//匿名空间的作用域是,从声明开始到文件结束
void test(){
cout<<::num <<endl;
::print();
cout<<endl;
print();
cout<<num<<endl;
}
void test01(){
}
int main(void)
{
test();
return 0;
}
mutinamespace.cc
#include <iostream>
/* using std::cout; */
/* using std::endl; */
using namespace std;
namespace wd{
int num=10;
//不能在命名空间内访问
void print(){
cout <<num <<endl;
//可以在函数内
cout<<"爱来自CPP miao: wd::print()"<<endl;
}
}//enf of namespace wd
namespace wd{
int val=10;
}//视为同一个命名空间
void test(){
using namespace wd;
wd::print();
cout<<wd::num<<endl;
cout<<endl;
print();
cout<<num<<endl;
cout<<endl;
cout<<val<<endl;
}
int main(void)
{
test();
return 0;
}
nested.cc
#include <iostream>
using std::cout;
using std::endl;
namespace kx{
int num=100;
void func(){cout <<"kaixin: func()"<<endl;}
namespace bkx{
int num=200;
void func(){cout <<"kaixin::bkx func()"<<endl;}
}//end bkx namespace
}//end kx namespace
void test(){
cout<<kx::bkx::num<<endl;
kx::bkx::func();
using namespace kx::bkx;
cout<<num<<endl;
func();
}
void test01(){
using kx::bkx::num;
using kx::bkx::func; //using kx::bkx::func()报错
cout<<num<<endl;
func();
}
int main(void)
{
test();
test01();
return 0;
}
externA.cc
#include <iostream>
using std::cout;
using std::endl;
int num=10;
void print(){
cout<<"爱来自CPP miao: print()"<<endl;
}
namespace wd{
int val=10;
void display(){
cout<<"爱来自CPP miao: wd::display()"<<endl;
}
}// end wd
externB.cc
#include <iostream>
using std::cout;
using std::endl;
//外部引入声明
extern int num;
extern void print();
//联合编译时,视为一个wd命名空间
namespace wd{
extern int val;
extern void display();
/* int val; */
}
void test(){
cout<<num<<endl;
print();
cout << wd::val <<endl;
wd::display();
}
int main(void)
{
test();
return 0;
}
e_noA.cc
#include <iostream>
using std::cout;
using std::endl;
static int num=890234824;
e_noB.cc
#include <iostream>
using std::cout;
using std::endl;
extern int num;
void test(){
cout<<num<<endl;
}
int main(void)
{
test();
return 0;
}
02:const
const.cc
#include <iostream>
using std::cout;
using std::endl;
#define MAX "hello"
void test(){
cout <<MAX-1<<endl;
//替换后,双引号引起来的字符串,会自动被识别为const char*
//MAX-1的效果实际上是指针偏移
cout<<MAX+1<<endl;
//cout +str 直接输出字符串内容
const int num =1000;
cout <<num<<endl;
int i=1;
int i_2=2;
const int * p=&i; //指向常量的指针i(只读权限指针)
cout <<*p<<endl;
/* *p=2; */
p=&i_2;
cout <<*p<<endl;
i_2=90;
cout <<*p<<endl;
//指向常量的指针 的另一种写法
int const * p1=&i;
cout <<*p1<<endl;
p1=&i_2;
cout <<*p1<<endl;
/* *p1=890809; */
const int i_3=3;
const int i_5=5;
//常量指针 不能改变指向
int * const pp=&i;
cout<<*pp<<endl;
/* pp=&i_2; */
const int* const p2=&i_3;
cout <<*p2<<endl;
/* p2=&i_5; */
}
int main(void)
{
test();
return 0;
}
const02.cc
#include <iostream>
using std::cout;
using std::endl;
void test(){
int arr[5]={1,2,3,4,5};
cout<<arr<<endl;
cout<<arr+1<<endl;
cout<<&arr<<endl;
cout<<&arr+1<<endl;
//数组指针
int (*p)[5]=&arr;
cout<<p+1<<endl;
for(int i=0;i<5;i++){
cout<<(*p)[i]<<" ";
}
cout<<endl;
}
//指针数组
//存放元素为指针的数组
void test01(){
int num1=1,num2=2,num3=3;
int *p1=&num1;
int *p2=&num2;
int *p3=&num3;
int *arr[3]={p1,p2,p3};
for(int i=0;i<3;i++){
cout<<*arr[i]<<" ";
}
cout<<endl;
}
void test02(){
int num1=1,num2=2,num3=3;
int arr[3]={num1,num2,num3};
cout<<arr<<endl;
cout<<&num1<<endl;
}
//函数指针
//指向函数的指针,可以利用这种指针调用函数
void print(){
cout<<"print()"<<endl;
}
int func(int a,int b){
return a+b;
}
/* int func1(){ */
/* int num=100; */
/* /1* return # *1/ */
/* } */
void test03(){
void (*p)()=print;
p();
//完整写法
void (*p1)()=&print;
(*p1)();
int (*p2)(int,int )=func;
(*p2)(3,3);
}
//定义函数指针需要确定其指向的函数的,返回类型和参数信息
int main(void)
{
test();
test01();
test02();
return 0;
}
03:delete / new
new.cc
/* #include <string.h> */
#include <cstring>
using std::cout;
using std::endl;
void test(){
/* int *p=(int*)malloc(sizeof(int)); */
/* *p=100; */
/* free(p); */
int *p1=new int(4);
cout<<*p1<<endl;
delete p1;
p1=nullptr;
//new表达式不指定初始化的值
//就会按照该类型的默认值进行初始化
int *p2=new int();
cout<<*p2<<endl;
delete p2;
p2=nullptr;
//不加()无法确保进行了初始化
/* int *p3=new int; */
/* cout<<*p3<<endl; */
char* p4=new char();
cout<<*p4<<endl;
delete p4;
p4=nullptr;
}
void test01(){
int *p=new int[10]();
for(int i=0;i<10;i++){
cout<<p[i]<<" ";
}
cout<<endl;
delete[] p;
p=nullptr;
int *p1=new int[6]{1,2,3,4,5,6};
for(int i=0;i<6;i++){ //小心访问越界i<10
cout<<p1[i]<<" ";
}
cout<<endl;
delete[] p1;
p1=nullptr;
char* pstr=new char[3]{'h','e','\0'};
cout<<pstr<<endl;//可以直接获取字符串的内容
delete [] pstr;
pstr=nullptr;
const char* ptr2="i love xixi";
cout<<sizeof(ptr2)<<endl;
cout<<strlen(ptr2)<<endl;
char* p2=new char[strlen(ptr2)+1]();
strcpy(p2,ptr2);
cout<<p2<<endl;
delete [] p2;
p2=nullptr;
}
int main(void)
{
test();
test01();
return 0;
}
04:作业
01:什么是命名空间?其作用是什么?匿名命名空间有什么特点?
命名空间:
程序员命名的内存空间,每个名字空间都是一个名字空间域,存放在名字空间的全局实体只在本空间域内有效
作用:
通过名字空间将内部的实体和其他全局实体分隔开,从而合理的解决命名冲突
匿名命名空间:
1,匿名命名空间内的实体不能跨模块调用
2,如果定义了和空间内实体重名的全局实体,会导致访问冲突(即使加上了::作用域限定符,也只能访问到全局的实体
02:const
关键字与宏定义的区别是什么?(面试常考)
1,发生时机不同
const 编译阶段 ;宏定义 预处理阶段
2,类型和安全检查不同
宏没有类型,不进行类型检查;cosnt变量,赋予只读权限,进行类型检查
03:什么是常量指针和指向常量的指针?什么是数组指针和指针数组?什么是函数指针和指针函数?请举例说明。
1,
常量指针const pointer : (只读权限的指针)
const int* int const*
指向常量的指针pointer to const:
int * const
2,
数组指针,指向数组的指针
指针数组,元素是指针的数组
3,
函数指针 :指向函数的指针,定义时要清楚返回值和参数类型
指针函数 :返回值为指针的函数,确保返回值的生存期比函数的生存期长
04:new/delete
与malloc/free
的区别是什么?(面试常考)
1,malloc/free是库函数,new/delete是表达式(不是函数的写法)
2,new返回相应类型的指针,malloc返回void*
3,new直接初始化为各类型默认值,malloc不会初始化
4,malloc参数是字节数,new不需要传递字节数,自动获取相应类型大小
5,new可以传递要初始化的值,也可以不传递
05:以下代码输出的是___? 27
int foo(int x,int y)
{
if(x <= 0 ||y <= 0)
return 1;
return 3 * foo(x-1, y/2);
}
cout << foo(3,5) << endl;
06:若执行下面的程序时,从键盘上输入5,则输出是() 6
int main(int argc, char** argv)
{
int x;
cin >> x;
if(x++ > 5)
{
cout << x << endl;
}
else
{
cout << x-- << endl;
}
return 0;
}
07:写出下面程序的结果: 2,5
int main()
{
int a[5]={1,2,3,4,5};
int *ptr=(int *)(&a+1);
printf("%d,%d",*(a+1),*(ptr-1));
}
08:在C++中,在堆上申请空间存放字符串内容的通用做法是怎样,用new表达式完成
new.cc
测试代码喵
#include <iostream>
using std::cout;
using std::endl;
int foo(int x,int y)
{
if(x <= 0 ||y <= 0)
return 1;
return 3 * foo(x-1, y/2);
}
int func(int x)
{
if(x++ > 5)
{
cout << x << endl;
}
else
{
cout << x-- << endl;
}
return 0;
}
void ff(){
int a[5]={1,2,3,4,5};
int *ptr=(int *)(&a+1);
cout<<&a<<" "<<*(&a)<<endl;
cout<<&a+1<<" "<<*(&a+1)<<endl;
// shu zu zhi zhen +20
cout<<a<<" "<<*(a)<<endl;
cout<<a+1<<" "<<*(a+1)<<endl;
cout<<ptr<<" "<<*(ptr)<<endl;
cout<<ptr-1<<" "<<*(ptr-1)<<endl;
cout<<*(a+1)<<" "<<*(ptr-1)<<endl;
/* 0x7ffed15617e0 0x7ffed15617e0 */
/* 0x7ffed15617f4 0x7ffed15617f4 */ //+20,可恶!
/* 0x7ffed15617e0 1 */
/* 0x7ffed15617e4 2 */
/* 0x7ffed15617f4 23360 */
/* 0x7ffed15617f0 5 */
}
void test(){
cout << foo(3,5) << endl;
cout << func(5) << endl;
ff();
}
int main(void)
{
test();
return 0;
}