printf()函数的返回值以及多态实现

函数返回值:

printf函数的一般形式为:
  int printf(const char *format,[argument]);
  以上形式,我们在Visual C++里输入“printf(”将会看到。
  说明printf函数类型为整型,其返回值是整型值。
  其值实际为printf控制输出的字符数。
   printf()函数实际上是将所有参数按字符输出,根据该函数的参数1(const char *format),我们不难理解。
  例如:
      
    int  a,b;
    a = printf("gelin\n"); //a的值为6,
  b = printf("the value of printf is:%d",a); //b的值为24
  printf("\n%d\n",b);
以上程序将会输出:
gelin
the value of printf is:6
24
C++的多态性
#include <iostream>

using namespace std;

class A
{
public:
	virtual void func1()
	{
		cout<<"A::func1";
	}
	void func2()
	{
		cout<<"A::func2";
	}
};

class B:public A
{

public:

	void func1()
	{
		cout<<"B::func1";
	}
	void func2()
	{
		cout<<"B::func2";
	}
};

void main()
{
	B x;

	B* pB = &x;

	A* pA = &x;

	pB->func1();

	pB->func2();

	pA->func1();

	pA->func2();

	printf("\n");
}

//结果为:B::func1Bfunc2B::func1A::func2

找错:
#include <iostream>

using namespace std;

class A
{
public:
	A(){}

	virtual ~A(){}

	virtual int f1()
	{
		//cout<<"Tencent";
		return -1;
	}
};

class B:public A
{
public:

	B()
	{
		//memset(this, 0, sizeof(B));
		m_nIntB = 0;
	}

	~B(){}

	int f1()
	{
		return m_nIntB;
	}

private:
	int m_nIntB;

};

int main()
{
	A *p = new B();

	cout<<p->f1()<<endl;

	delete p;

	return 0;
}

#include <iostream>

using namespace std;

class CTest
{
public:
	CTest()
	{
		m_nData = 0;
	}

	~CTest(){}

	int GetData()
	{
		return m_nData;
	}

private:

	int m_nData;
};

CTest& CreateObj()
{
	CTest test;

	return test;
}

int main()
{
	CTest test = CreateObj();

	cout<<test.GetData()<<endl;

	return 0;
}

//结果为:随即的一个任意数字,因为CreateObj()函数返回的对象,随着调用的结束,该内存空间以及被释放,故结果为随即的任意数字。

#include "stdio.h"
int main(int argc, char* argv[])
{
 printf("%d\n",int(10/3));//3
 printf("%f\n",double(10/3));//3.000000
 printf("%f\n",10/3);//0.000000
 int i = 0;
 printf("%d,%d\n",i++,i++);//0,0
 i = 0;
 printf("%d,%d\n",i++,++i);//1,1
 i = 0;
 printf("%d,%d\n",++i,++i);//2,1
 i = 0;
 printf("%d,%d\n",++i,i++);//1,0
 return 0;
}
  
请思考为什么


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值