64,682
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
virtual void display() const //注意这里const
{
std::cout << "I am Base class! " <<std::endl;
}
virtual ~Base(){}
};
class Derive:public Base
{
public:
virtual void display()
{
std::cout << "I am Derive class! " <<std::endl;
}
virtual ~Derive(){}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase-> display();
pDerive-> display();
delete pBase;
delete pDerive;
system("pause");
return 0;
}
//输出:
//I am Base class!
//I am Derive class!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template <typename T>
inline T max(T a,T b)
{
return a <b?b:a;
}
int _tmain(int argc, _TCHAR* argv[])
{
::max( "apple ", "peach ");
std::string s;
::max( "apple ",s);//error C2782: 'T max(T,T)' : template parameter 'T' is ambiguous
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int defaultFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 >= iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}
int CompareFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 < iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}
typedef int (*PF)(const int &, const int &);
int call(int iTmp1, int iTmp2, PF pFunc = defaultFunc)
{
return pFunc(iTmp1, iTmp2);
}
int _tmain(int argc, _TCHAR* argv[])
{
int iTmp1 = 1;
int iTmp2 = 2;
PF pCompare = CompareFunc;
printf( "result:%d\n ",call(iTmp1, iTmp2)); //调用默认的返回2
printf( "result:%d\n ",call(iTmp1, iTmp2, pCompare)); //调用compareFunc,返回1
printf( "result:%d\n ",call(iTmp1, iTmp2, CompareFunc)); //同上
system("pause");
return 0;
}
//输出结果为:
//result:2
// result:1
// result:1
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int CompareFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 < iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}
typedef int (*PF)(const int &, const int &);
int call(int iTmp1, int iTmp2, PF pFunc )
{
return pFunc(iTmp1, iTmp2);
}
int _tmain(int argc, _TCHAR* argv[])
{
PF pCompare = NULL; //你知道这是可以的,是吧
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a[10];
int *p=NULL;
p=&a;//error C2440: '=' : cannot convert from 'int (*)[10]' to 'int *'
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a[10];
int *p=NULL;
p=&a[4];
*(p++);
*(++p);
(*p)++; //对一个随机值加1,你知道没什么意思,是吧
*(p--);
printf( "Hello World!\n ");
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
virtual void display(std::string strShow= "I am Base class! ")
{
std::cout <<strShow <<std::endl;
}
virtual ~Base(){}
};
class Derive:public Base
{
public:
virtual void display(std::string strShow= "I am Derive class! ")
{
std::cout <<strShow <<std::endl;
}
virtual ~Derive(){}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase-> display();
pDerive-> display();
delete pBase;
delete pDerive;
system("pause");
return 0;
}
//输出结果为:
//I am Base class!
//I am Derive class!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
Base():a(0)
{
}
virtual ~Base(){}
protected:
int a;
};
class Derived2;
class Derive:public Base
{
friend class Derived2;
public:
void set(int b)
{
a=b;
}
virtual ~Derive(){}
};
class Derived2
{
public:
int get()const
{
return d.a;
}
private:
Derive d;
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived2 d;
Derive d1;
d1.set(18);
cout<<d.get()<<endl;
system("pause");
return 0;
}
//输出:0
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
Base()
{
cout<<"I was executed before main"<<endl;
}
};
Base b;
int _tmain(int argc, _TCHAR* argv[])
{
system("pause");
return 0;
}
//输出:
//I was executed before main
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A()
{
cout<<"A"<<endl;
}
};
class B
{
public:
B()
{
cout<<"B"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
system("pause");
return 0;
}
//输出:
//A
//B
//1
//1
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A()
{
cout<<"A"<<endl;
}
};
class B
{
public:
B()
{
cout<<"B"<<endl;
}
virtual void display()const=0;
};
int _tmain(int argc, _TCHAR* argv[])
{
B b;// error C2259: 'B' : cannot instantiate abstract class
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
int myfunc(double da,double db)
{
cout<<"da: "<<da<<"db: "<<db<<endl;
return da>db?da:db;
}
double myfunc(int ia,float fb)
{
cout<<"ia: "<<ia<<"fb: "<<fb<<endl;
return ia>fb?ia:fb;
}
int myfunc(short sa,double db)
{
cout<<"sa: "<<sa<<"db: "<<db<<endl;
return sa>db?sa:db;
}
double myfunc(int ia,double db)
{
cout<<"ia: "<<ia<<"db: "<<db<<endl;
return ia>db?ia:db;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned short ix=65530;
int a=myfunc(ix,20.0);
system("pause");
return 0;
}
//输出结果为ia: 65530db: 20
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
inline T const & maxj(T const & a, T const & b)
{
return a>b?a:b;
}
int _tmain(int argc, _TCHAR* argv[])
{
maxj(4,7);
maxj(4,4.2); //error C2782: 'const T &maxj(const T &,const T &)' : template parameter 'T' is ambiguous
maxj(static_cast <double> (4),4.2);
maxj<double> (4,4.2);
system("pause");
return 0;
}
//如果是直接拿max做函数名的话,没一个是正确的
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template <typename T>
inline T max(T a,T b)
{
return a <b?b:a;
}
int _tmain(int argc, _TCHAR* argv[])
{
::max( "apple ", "peach ");
std::string s;
::max( "apple ",s);
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <iostream>
class BaseException
{
};
class DerivedException:public BaseException
{
};
void makeExcp()
{
BaseException& excp=DerivedException();
throw excp;
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
makeExcp();
}
catch(DerivedException&)
{
std::cout << "caught DerivedException " <<std::endl;
}
catch(BaseException&)
{
std::cout << "caught BaseException " <<std::endl;
}
catch(...)
{
std::cout << "caught other Exception " <<std::endl;
}
system("pause");
return 0;
}
//输出:caught BaseException
//reaons for this:
Like a parameter declaration, an exception specifier for a base class can be used to catch an exception object of a derived type. Again, like a parameter declaration, the static type of the exception specifier determines the actions that the catch clause may perform. If the exception object thrown is of derived-class type but is handled by a catch that takes a base-class type,then the catch cannot use any members that are unique to the derived type.
#include "stdafx.h"
#include <iostream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
int i=-1;
const int ic=i;
const int *pic= //error C2440: 'initializing' : cannot convert from 'const int *' to 'int *const '
int * const cpi=
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <iostream>
int *func(int, int)
{
return 0;
}
typedef int *(PF)(int, int);
PF getFunc()
{
return func;
}
int _tmain(int argc, _TCHAR* argv[])
{
PF pFunc = getFunc();
system("pause");
return 0;
}
errors:
error C2091: function returns function
1>d:\c++ demo\c plus plus foundation code\csdn测试代码\homework\homework\homework.cpp(24): error C2072: 'pFunc' : initialization of a function
1>d:\c++ demo\c plus plus foundation code\csdn测试代码\homework\homework\homework.cpp(24): error C2205: 'pFunc' : cannot initialize extern variables with block scope
1>d:\c++ demo\c plus plus foundation code\csdn测试代码\homework\homework\homework.cpp(24): error C2440: 'initializing' : cannot convert from 'PF (__cdecl *)' to 'PF'
1> There are no conversions to function types, although there are conversions to references or pointers to functions