C++ primer习题记录——第十九章

本文详细记录了C++ Primer一书中第十九章的重要习题,涵盖9.3、19.6、19.9等多个部分,深入探讨C++语言的关键概念和技术,对于学习和备考蓝桥杯等编程竞赛极具参考价值。
摘要由CSDN通过智能技术生成

19.1:

#include<cstdlib>
#include<iostream>
using namespace std;
void* operator new(size_t size)
{
	if (void* mem = malloc(size))
	{
		return mem;
	}
	else
	{
		throw bad_alloc();
	}
}
void operator delete(void* mem)noexcept
{
	free(mem);
}

9.3:

#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <typeinfo>
using namespace std;

class A
{
public:
	A()
	{
		cout << "A()" << endl;
	}
	virtual ~A()
	{
		cout << "~A()" << endl;
	}
};

class B :public A
{
public:
	B()
	{
		cout << "B()" << endl;
	}
	virtual ~B()
	{
		cout << "~B()" << endl;
	}
};
class C :public B
{
public:
	C()
	{
		cout << "C()" << endl;
	}
	virtual ~C()
	{
		cout << "~C()" << endl;
	}
};
class D :public B, public A
{
public:
	D()
	{
		cout << "D()" << endl;
	}
	virtual ~D()
	{
		cout << "~D()" << endl;
	}
};
int main(int argc, char** argv)
{
	A* pa = new C;
	if (B* pb = dynamic_cast<B*>(pa))
	{
		cout << "True" << endl;
	}
	else
	{
		cout << "False" << endl;
	}//因为指针类型的转换失败返回为0可以使用条件中赋值判断

	try
	{
		C& cp = dynamic_cast<C&>(*pa);//正确,*pa的类型是C  
		cout << "cp" << endl;
	}
	catch (std::bad_cast e)
	{
		cout << e.what() << endl;
	}//引用类型失败返回的是bad_cast  

	B* pbb = new B;
	if (C* pc = dynamic_cast<C*>(pbb))
	{
		cout << "True" << endl;
	}
	else
	{
		cout << "False" << endl;
	}

	A* paa = new D;
	if (B* pc = dynamic_cast<C*>(paa))
	{
		cout << "True" << endl;
	}
	else
	{
		cout << "False" << endl;
	}

	cin.get();
	return 0;
}

19.6:

#include<iostream>
using namespace std;
class Base{
	virtual void print()
	{}
};
class Derived:public Base{};
int main()
{
	Derived* dp = new Derived;
	Base* bp = dp;
	bool b = (typeid(*dp)== typeid(*bp));
	cout << b << endl;
}

19.9:

#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <typeinfo>
using namespace std;

int main(int argc, char** argv)
{
	int arr[41];
	double i = 6.23;
	vector<int> vec1;
	int* p = arr;
	cout << typeid(arr).name() << endl;
	cout << typeid(i).name() << endl;
	cout << typeid(vec1).name() << endl;
	cout << typeid(p).name() << endl;

	cin.get();
	return 0;
}

19.12:

#include <string>
#include <iostream>

class Screen {
public:
    using pos = std::string::size_type;

    static const std::string Screen::* data() { return &Screen::contents; }
    static const pos Screen::* pcursor() { return &Screen::cursor; }
    Screen() = default;
    Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht* wd, c) { }

    char get() const { return contents[cursor]; }
    char get(pos r, pos c) const { return contents[r * width + c]; }

private:
    pos cursor = 0;
    pos height = 0, width = 0;
    std::string contents;
};

int main()
{
    // const std::string Screen::*pdata;
    // pdata = &Screen::contents;
    // auto pdata = &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值