函数返回值:
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;
}
请思考为什么