程序员面试题精选100题(52,53,54)-C++面试题

// 程序员面试题精选100题(52,53,54)-C++面试题.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
/*
class A
{
};

class B
{
public:
	B() {}
	~B() {}
};

class C
{
public:
	C() {}
	virtual ~C() {}
};*/
//
/*
class A
{
private:
	int m_value;

public:
	A(int value)
	{
		m_value = value;
	}
	void Print1()//void virtual Print1()
	{
		printf("hello world");
	}
	void Print2()
	{
		printf("%d", m_value);
	}
};
*/
//
/*struct Point3D
{
	int x;
	int y;
	int z;
};*/
//
/*
class A
{
private:
	int value;

public:
	A(int n)
	{
		value = n;
	}

	A(A &other)//here must be a reference or a point
	{
		value = other.value;
	}

	void Print()
	{
		std::cout << value << std::endl;
	}
};
*/
//
/*
int SizeOf(char pString[])
{
	return sizeof(pString);
}
void print(char* str)
{
	int i=0;
	while(str[i]!='\0')
		cout<<str[i++]<<" ";
	cout<<endl;
}*/
//
/*class A
{
public:
	A()
	{
		std::cout << "A is created." << std::endl;
	}

	~A()
	{
		std::cout << "A is deleted." << std::endl;
	}
};

class B : public A
{
public:
	B()
	{
		std::cout << "B is created." << std::endl;
	}

	~B()
	{
		std::cout << "B is deleted." << std::endl;
	}
};*/
//
/*class A
{
public:
	virtual void Fun(int number = 10)
	{
		std::cout << "A::Fun with number " << number;
	}
};

class B: public A
{
public:
	virtual void Fun(int number = 20)
	{
		std::cout << "B::Fun with number " << number;
	}
};*/
//
/*char* GetString1()
{
	char p[] = "Hello World";
	return p;
}

char* GetString2()
{
	char *p = "Hello World";
	return p;
}*/
//
/*void helloworld()
{
	char str1[] = "hello world";
	char str2[] = "hello world";
	char* str3 = "hello world";
	char* str4 = "hello world";
	if(str1 == str2)
	printf("str1 and str2 are same.\n");
	else
	printf("str1 and str2 are not same.\n");
	if(str3 == str4)
	printf("str3 and str4 are same.\n");
	else
	printf("str3 and str4 are not same.\n");
}*/
//
class Base
{
public:
	void print() { doPrint();}

private:
	virtual void doPrint() {cout << "Base::doPrint" << endl;}
};

class Derived : public Base
{
private:
	virtual void doPrint() {cout << "Derived::doPrint" << endl;}
};
//
int _tmain(int argc, _TCHAR* argv[])
{
	//1 printf("%d, %d, %d\n", sizeof(A), sizeof(B), sizeof(C));
	
	/*2这题好呀,inside C++ model 里有解释
	A* pA = NULL;//答案是Print1调用正常,打印出hello world,但运行至Print2时,程序崩溃。调用Print1时,并不需要pA的地址,因为Print1的函数地址是固定的。若是虚函数则地址不固定,名字转换不一样
	pA->Print1();//编译器会给Print1传入一个this指针,该指针为NULL,但在Print1中该this指针并没有用到。只要程序运行时没有访问不该访问的内存就不会出错,因此运行正常。
	pA->Print2();//在运行print2时,需要this指针才能得到m_value的值。由于此时this指针为NULL,因此程序崩溃了。
	*/
	
	/*Point3D* pPoint = NULL;//&(pPoint->z)的语意是求pPoint中变量z的地址(pPoint的地址0加z的偏移量8),并不需要访问pPoint指向的内存。只要不访问非法的内存,程序就不会出错。
	int offset = (int)(&(pPoint)->z);//不加取地址符就错误
	printf("%d", offset);*/
	 
	/*A a = 10;
	A b = a;//编译错误。在复制构造函数中传入的参数是A的一个实例。由于是传值,把形参拷贝到实参会调用复制构造函数。因此如果允许复制构造函数传值,那么会形成永无休止的递归并造成栈溢出。
	//因此C++的标准不允许复制构造函数传值参数,而必须是传引用或者常量引用。
	b.Print();
	//a.Print();*/

	/*char* pString1 = "google";
	int size1 = sizeof(pString1);
	int size2 = sizeof(*pString1);//1
	int size5 = strlen(pString1);
	//int size6 = strlen(*pString1);//wrong,need the parameter of a point type
	char pString2[100] = "google";
	int size3 = sizeof(pString2);//100
	int size6 = strlen(pString2);// putout 6 strlen look for the end of '\0',while sizeof返回一个对象或者类型所占的内存字节数 sizeof(2)==sizeof(int)
	int size4 = SizeOf(pString2);//虽然传入的参数是一个字符数组,当数组作为函数的参数进行传递时,数组就自动退化为同类型的指针。因此size4也是一个指针的大小为4.
	printf("%d, %d, %d, %d", size1, size2, size3, size4);
	cout<<size5<<endl;//6
	cout<<size6<<endl;//6
	print(pString1);
	print(pString2);*/

	//A* pA = new B();//接下来运行delete语句时,会调用析构函数。由于pA被声明成类型A的指针,同时基类A的析构函数没有标上virtual,因此只有A的析构函数被调用到,而不会调用B的析构函数。
	//delete pA;//由于pA实际上是指向一个B的实例的指针,但在析构的时候只调用了基类A的析构函数,却没有调用B的析构函数。这就是一个问题。如果在类型B中创建了一些资源,比如文件句柄、内存等,在这种情况下都得不到释放,从而导致资源泄漏。

	//B b;//输出B::Fun with number 10。由于a是一个指向B实例的引用,因此在运行的时候会调用B::Fun。但缺省参数是在编译期决定的。
	//A &a = b;//在编译的时候,编译器只知道a是一个类型a的引用,具体指向什么类型在编译期是不能确定的,因此会按照A::Fun的声明把缺省参数number设为10。
	//a.Fun();

	//printf("GetString1 returns: %s. \n", GetString1());//当运行到GetString1时,p是一个数组,会开辟一块内存,并拷贝"Hello World"初始化该数组。接着返回数组的首地址并退出该函数。由于p是GetString1内的一个局部变量,当运行到这个函数外面的时候,这个数组的内存会被释放掉。因此在_tmain函数里再去访问这个数组的内容时,结果是随机的。
	//printf("GetString2 returns: %s. \n", GetString2());//当运行到GetString2时,p是一个指针,它指向的是字符串常量区的一个常量字符串。该常量字符串是一个全局的,并不会因为退出函数GetString2而被释放掉。因此在_tmain中仍然根据GetString2返回的地址得到字符串"Hello World"。

	//helloworld();//str1和str2是两个字符串数组。我们会为它们分配两个长度为12个字节的空间,并把"hello world"的内容分别拷贝到数组中去。这是两个初始地址不同的数组,因此比较str1和str2的值,会不相同。str3和str4是两个指针,我们无需为它们分配内存以存储字符串的内容,而只需要把它们指向"hello world“在内存中的地址就可以了。由于"hello world”是常量字符串,它在内存中只有一个拷贝,因此str3和str4指向的是同一个地址。因此比较str3和str4的值,会是相同的。
	
	/*Base b;
	b.print();
	Derived d;
	d.print();//在print中调用doPrint时,doPrint()的写法和this->doPrint()是等价的,因此将根据实际的类型调用对应的doPrint。*/

	system("pause");
	return 0;
}
有一本书,C++ the inside ohject model,看了一遍感觉挺有用。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值