C++面试相关题目集合选摘
2017-12-27 13:19
375 查看
关于继承、多态、重写、重载等:
#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 _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; }
相关文章推荐
- C/C++ 笔试、面试题目汇总1——代码相关
- 多态,虚函数,纯虚函数,抽象类的相关问题(c++面试常见题目)
- C/C++ 笔试、面试题目汇总1——代码相关
- C++面试题目集合(持续跟新)
- C++面试题目集合
- C++笔试面试题目集合
- 转:C/C++笔试,面试题目汇总
- C/C++ 笔试、面试题目大汇总-1
- C/C++ 笔试、面试题目大汇总
- C/C++ 笔试、面试题目大汇总
- C/C++面试题目汇总
- linux c/c++ 面试题目整理(一)
- 不错的C++面试题目
- 面试遇到的Windows相关的题目
- 面试之C++题目汇总
- 笔试题1:C/C++ 笔试、面试题目大汇总
- C/C++&nbsp;笔试、面试题目(转)
- c++面试题目
- c++相关题目
- [笔面] 设计模式相关面试题目