C++面试相关题目集合选摘

关于继承、多态、重写、重载等:

#ifndef _PRINTHELLO_H_
#define _PRINTHELLO_H_

#include<stdio.h>


class PrintHello {
public:
	static PrintHello* instance();
	static void uninstance();
	~PrintHello();
private:
	PrintHello();
private:
	static PrintHello* _instance;
};
//唯一标识符
PrintHello* PrintHello::_instance = NULL;
#define PrintHelloInstance PrintHello::instance()

PrintHello::PrintHello() {
	printf("Hello World!\n");
}

PrintHello* PrintHello::instance() {
	if(_instance==NULL){
		_instance = new PrintHello();
	}
	return _instance;
}

void PrintHello::uninstance() {
	if (_instance){
		delete _instance;
		_instance = NULL;
	}
}

PrintHello::~PrintHello() {
	printf("~Hello World!\n");
}

#endif

#ifndef _SHAPE_H_
#define _SHAPE_H_

class Triangle;
class Rectangle;
class Shape
{
public:
	bool isShape() {
		return true;
	}
	virtual int getSides(){//声明为虚函数,子类重写才起作用
		printf("---in Shape::getSides()---\n");
		return 0;
	}
	int getSides(Triangle* tri){
		printf("---in Shape::getSides(Triangle* tri)---\n");
		return 3;
	}
	int getSides(Rectangle* rec){
		return 4;
	}
	virtual void vvv() = 0;//纯虚函数不能实例化
protected:
private:
};

class Triangle : public Shape
{
public:
	int getSides(){//重写,实现多态
		printf("---in Triangle::getSides()---\n");
		return 3;
	}
	int getSides(int i){//重载,子类特有
		return i;
	}
	void vvv(){//子类必须实现基类的接口
		printf("in Triangle::vvv\n");
	}
protected:
private:
};

class Rectangle : public Shape
{
public:
	int getSides(int i){//重载,子类特有
		return i;
	}
protected:
private:
};

#endif

#include "hello.hpp"
#include <iostream>
#include "Shape.h"

int fn1(void){
	printf("fn1...\n");
	return 0;
}

int a = fn1(); //全局变量,运行于main之前
//在main函数之前调用

/*class*/ struct A 
{
	int aa;
public:
	A(){ int abc = 10; };
	virtual ~A(){};
public:
	void hello(){ printf("Hello World!\n"); }
};

/*class*/ struct B : A
{
	int aa;
	int bb;
	static int cc;
public:
	B(){};
	virtual ~B(){};
};

/*class*/ struct C : B
{
	char cc;
public:
	C(){};
	~C(){};
};


int main(int argc, char* argv[]){
	printf("I am in main----1111\n");
	PrintHello::instance()->uninstance();
	printf("I am in main----2222\n");

	char *p[] = { "TENCENT", "CAMPUS", "RECRUITING" };
	char **pp[] = {p+2, p+1, p};//{ "RECRUITING", "CAMPUS", "TENCENT" }
	char ***ppp = pp;// "RECRUITING"
	printf("%s", **++ppp);// "CAMPUS"
	printf("%s", *++*++ppp);// p + 1

	float a[10], x;
	float b = *&a[1];//优先级从右到左

	const char *pStr = "sss";//字符串常量
	pStr = "abb";

	char a1[] = { 'a', 'b', 'c' };
	char a2[] = { "abc" };//{'a', 'b', 'c', '\0'}

	int i = 10, j = 2;
	i *= j + 8;//优先级 + > *=

	char acX[] = "abc";//{ 'a', 'b', 'c', '\0' }
	char acY[] = { 'a', 'b', 'c' };
	char *szX = "abc";
	char *szY = "abc";//szX和szY指向同一内存地址
	acX[0] = 'h';
	acY[0] = 'h';

	A sa;//sizeof(sa)==8
	B sb;//sizeof(sb)==16
	C sc;//sizeof(sc)==20//字节对齐
	printf("\nsizeof(A)=%d, sizeof(B)=%d, sizeof(C)=%d\n", sizeof(sa), sizeof(sb), sizeof(sc));

	//多态测试
	Shape* pShape = new Triangle();
	printf("Shape=%d\n", pShape->getSides());
	printf("Shape=%d\n", pShape->getSides((Triangle*)pShape));
	//顺序从后面往前!!!
	printf("Shape=%d, %d\n", pShape->getSides(), pShape->getSides((Triangle*)pShape));

	if ('q'==getchar()) {
		exit(0);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值