C++ 笔试题练习(一)

1.

在这里插入图片描述

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<Windows.h>

class ClassA
{
    public:
    virtual ~ ClassA()
    {
    }
    virtual void FunctionA()
    {
    }
};
class ClassB
{
    public:
    virtual void FunctionB()
    {
    }
};
class ClassC: public ClassA, public ClassB
{
    public:
};


int _tmain(int argc, _TCHAR* argv[])
{
	ClassC aObject;
	ClassA *pA = &aObject; 
	ClassB *pB = &aObject;
	ClassC *pC = &aObject;

	ClassA* pA2;
	//pA2 = static_cast<ClassA*>(pB); // 无法强制转换(A、B两个类没有关系)。pA2 = static_cast<ClassA*>(pC); 可以生效。
	
	//void* pVoid = static_cast<void*>(pB);	// 使用无符号指针 void* 进行中转,可以生效。
	//pA2 = static_cast<ClassA*>(pVoid);

	//pA2 = pB;	// 同理,A、B两个类没有关系,无法运算。

	//pA2=static_cast<ClassA*>(static_cast<ClassC*>(pB)); // 用 C 这个类做了一次中转,可以生效。
	
	return 0;
}


2.

在这里插入图片描述

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<Windows.h>
#include<iostream>
using namespace std;
#define SUB(a) (a)-(a)

int _tmain(int argc, _TCHAR* argv[])
{
	int a=2,b=3,c=5,d;
	d = SUB(a+b)*c;	// 其实就是 (a+b)-(a+b)*c
	cout << d << endl;
	
	getchar();
	return 0;
}

3.

在这里插入图片描述

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<Windows.h>
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char str[]="ABCD", *p=str;
	for(int i=0;i<100;i++){
		printf("%d\n", *(p+i));	// *(p+0) = A,以此类推 +3就是 D,+4 就是结尾(字符串的结尾都是 NULL,NULL 对应的 ASCII 为 0)
	}
		
	getchar();
	return 0;
}


4.

在这里插入图片描述

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<Windows.h>
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int x;
    for(x = 3; x < 6; x++){
        printf( (x%2)?("*%d"):("#%d"),x); // x%2 为 "真" 时执行 ":" 前面的,为 "假" 时执行 ":" 后面的。
	}
		
	getchar();
	return 0;
}


5.

在这里插入图片描述

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<Windows.h>
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char s[]="ABCD",*p;
	for(p=s+1;p<s+4;p++){
		printf("%s\n",p); 	// "%s" 是一个陷阱,因为是字符串所以后面的也要跟着一起输出。
	}

	getchar();
	return 0;
}


6.

在这里插入图片描述

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<Windows.h>
#include<iostream>
#include <stdint.h>
using namespace std;

struct Date{
    char a;
    int b;
    int64_t c;
    char d;
}d;		// char 占 1 字节,对其后为 4 。int 占 4 字节,对其后为 8 。int64_t 占 8 字节,对其后为 16 。char 为 1 字节,对其后为 24 (总体来说,变量最大的占 8 字节,所以按照 8 字节对其)。

int _tmain(int argc, _TCHAR* argv[])
{
	Date data[2][10];
	cout << sizeof(d) << endl;	// 输出结构体大小
	cout << &data << endl;	// 取 data 对象的起始地址。
	cout << &data[1][5].c << endl;	// 取 data[1][5].c 的地址

	getchar();
	return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值