例题2-1
#include "stdafx.h"
#include"iostream"
int fun(int a,int b)
{
int c;
c=a+b;
return c;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a,b,c;
std::cout<<"请输入两个整数:";
std::cin>>a>>b;
c=fun(a,b);
std::cout<<"the sum of a and b is"<<c;
return 0;
}
例题2-2
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
const double pi=3.14159;
double s=0;
s=pi*4.0*4.0;
cout<<s<<endl;
return 0;
}
例题2-3
//用数组来处理菲欧娜博数列问题
#include "stdafx.h"
#include"iostream"
#include"iomanip"//设置域宽函数
using namespace std;
int main()
{
int i;
int f[20]={1,1};
for(i=2;i<20;i++)
f[i]=f[i-2]+f[i-1];
for(i=0;i<20;i++)
{if(i%5==0)
cout<<endl;
cout<<setw(8)<<f[i];
}
cout<<endl;
return 0;
}
例2-4
//将一个二维数组行和列互换,存到另一个二维数组中。
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2],i,j;
cout<<"array a:"<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
cout<<a[i][j]<<" ";
b[j][i]=a[i][j];
}
cout<<endl;
}
cout<<"array b"<<endl;
for(i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
return 0;
}
例2-5
//输入三个字符串,要求将字母按从小到大的顺序输出。
#include "stdafx.h"
#include"iostream"
#include"string"
using namespace std;
int main()
{
string string1,string2,string3,temp;
cout<<"please input three strings:";
cin>>string1>>string2>>string3;
if(string1>string3)
{
temp=string2;
string2=string3;
string3=temp;
}
if(string1<=string2)
cout<<string1<<" "<<string2<<" "<<string3<<endl;
else if(string1<=string3)
cout<<string2<<" "<<string1<<" "<<string3<<endl;
else cout<<string2<<" "<<string3<<" "<<string1<<endl;
return 0;
}
例2-6
//引用参数分析
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
void swa(int &p1,int &p2);
int a,b;
cout<<"input two numbles:";
cin>>a>>b;
swap(a,b);
cout<<a<<' '<<b;
return 0;
}
void swap(int &p1,int &p2)
{
int p;
p=p1;
p1=p2;
p2=p;
}
例3-1
//判断用户的输入,如果用户输入的大于0,则屏幕上显示“正数”
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int a;
cin>>a;
if(a>0) cout<<"正数"<<endl;
return 0;
}
例3-2
//判断键盘输入的整数是否为偶数,是输出yes,不是输出no
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int x;
cout<<"请输入一个整数:\n";
cin>>x;
if(x%2==0)
cout<<"yes"<<endl;
else cout <<"no"<<endl;
return 0;
}
例3-3
//计算分段函数
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
float x,y;
cout<<"请输入x的值";
cin>>x;
if(x==10)
cout<<"not define x."<<endl;
else {if(x<=1) y=x+5;
else if (x<10) y=2*x;
else y=3/(x-10);
cout <<"x= "<<x<<"y= "<<y;
}
return 0;
}
例3-4
//求一元二次方程的根。系数由键盘输入;
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
float a,b,c;
float delta,x1,x2;
cout<<"please input three numbles a(a!=0),b,c:"<<endl;
cin>>a>>b>>c;
delta=b*b-4*a*c;
if(delta>=0)
if(delta>0)
{
delta=sqrt(delta);
x1=(-b+delta)/(2*a);
x2=(-b-delta)/(2*a);
cout<<"have two deffierent :";
cout<<"x1="<<x1<<'\t'<<"x2="<<x2<<endl;
}
else
{
cout<<"have two same solutions:";
cout<<"x1=x2="<<-b/(2*a)<<endl;
}
else cout<<"no solution"<<endl;
return 0;
}
例3-5
//将学生成绩转化为等级;
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int score;
cout<<"please input the score of student:"<<endl;
cin>>score;
if(score>100||score<0)
cout<<"input warnning"<<endl;
else switch(score/10)
{
case 10:
case 9:cout<<"A lever"<<endl;break;
case 8:cout<<"B lever"<<endl;break;
case 7:cout<<"C lever"<<endl;break;
case 6:cout<<"E lever"<<endl;break;
default:cout<<"E lever"<<endl;
}
}
例3-6
//求使s=1+1/2+1/3+...+1/n的值大于10的n的最小值;
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
float sum=0;
int n=1;
while(sum<10)
{
sum=sum+1.0/n;
n++;
}
cout<<"sum="<<sum<<endl;
cout<<"n="<<n<<endl;
return 0;
}
3-7
//输出1-100的奇数,并求解其和;
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int n=1,sum=0;
do
{
if(n%2!=0)
{
cout<<n<<"\t";
sum=sum+n;
}
n++;
}
while(n<=100);
cout<<"sum="<<sum<<endl;
return 0;
}
例3-8
//能被1,2,4,8整除的,这些数称作为8的因子,请列举48的所有因子,并列出所有因子之和;
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int sum=0,t;
cout<<"48的所有因子为:"<<endl;
for(int i=1;i<=48;i++)
{
if(48%i==0)
{
t=i;
sum+=t;
cout<<t<<"\t";
}
}
cout<<"48的所有因子之和为:"<<sum<<endl;
return 0;
}
例3-9
//利用for语句嵌套打印乘法表;
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int fac=1;
cout<<"\n 乘法表"<<endl;
for(int i=1;i<10;i++)
cout<<"\t"<<i;
cout<<endl<<"----------------------------------------------------------------------------"<<endl;
for(int i=1;i<10;i++)
{
cout<<i<<"|";
for(int j=1;j<10;j++)
{
fac=i*j;
cout<<"\t"<<fac;
}
cout<<endl;
}
return 0;
}
例3-10
//利用goto语句计算1-100的累加和;
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int i=1;
int sum=0;
loop:
if(i<=100)
{
sum+=i;
i++;
goto loop;
}
cout<<"1-100的累加和为:"<<sum<<endl;
return 0;
}
例4-1
//单向传值实例;
#include "stdafx.h"
#include"iostream"
using namespace std;
void swap(int x,int y)
{
int z;
z=x;
x=y;
y=z;
cout<<"the x,y is:"<<x<<" "<<y<<endl;
}
int main()
{
int m,n;
cin>>m>>n;
swap(m,n);
cout<<"the m,n is"<<m<<" "<<n;
return 0;
}
例4-2
//传递地址实例;
#include "stdafx.h"
#include"iostream"
using namespace std;
void fun(int a[2])
{
for(int i=0;i<2;i++)
a[i]=a[i]+2;
}
int main()
{
int b[2]={2,4};
cout<<b[0]<<'\t'<<b[1]<<endl;
fun(b);
cout<<b[0]<<"\t"<<b[1]<<endl;
return 0;
}
例4-3
//传递引用实例;
#include "stdafx.h"
#include"iostream"
using namespace std;
void swap(int &x,int&y)
{
int z;
z=x;
x=y;
y=z;
cout<<"x="<<x<<"\t"<<"y="<<y<<endl;
}
int main()
{
int m,n;
cin>>m>>n;
swap(m,n);
cout<<"m="<<m<<"\t"<<"n="<<n<<endl;
return 0;
}
例4-4
//函数返回值实例;
#include "stdafx.h"
#include"iostream"
using namespace std;
int max(int x,int y)
{
int z;
z=x>y?x:y;
return z;}
int main()
{
int m,n,z;
cout<<"please input two numbles"<<endl;
cin>>m>>n;
z=max(m,n);
cout<<"the max of m,n is"<<z;
return 0;
}
例4-5
//利用弦截法求解方程f(x)=x^3-5x^2+16x-80=0的根;
#include "stdafx.h"
#include"iostream"
using namespace std;
double f(double);
double xpoint(double,double);
double root(double,double);
int main()
{
double x1,x2,f1,f2,x;
do
{
cout<<"input x1,x2: ";
cin>>x1>>x2;
f1=f(x1);
f2=f(x2);
}
while(f1*f2>=0);
x=root(x1,x2);
cout<<"A root of equation is: "<<x<<endl;
return 0;
}
double f(double x)
{
double y;
y=x*x*x-5*x*x+16*x-80;
return y;
}
double xpoint(double x1,double x2)
{
double y;
y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
return y;
}
double root(double x1,double x2)
{
double x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0)
{
y1=y;
x1=x;
}
else x2=x;
}
while(fabs(y)>=0.00001);
return x;
}
例4-6
/利用递归求解年龄问题;
#include "stdafx.h"
#include"iostream"
using namespace std;
int age(int);
int main()
{
cout<<age(5)<<endl;
return 0;
}
int age(int n)
{
int c;
if(n==1)
{
c=10;
}
else c=age(n-1)+2;
return c;
}
例4-7
//编程求解1~10中各个数的平方;
#include "stdafx.h"
#include"iostream"
using namespace std;
inline int power_int(int x)
{
return x*x;
}
void main()
{
for(int i=1;i<=10;i++)
{
int p=power_int(i);
cout<<i<<"*"<<i<<"="<<p<<endl;
}
}
例4-8
//利用函数模板编写一个使用冒泡排序法进行排序的函数模板,并对int和char数组进行排序;
#include "stdafx.h"
#include"iostream"
using namespace std;
template<class T>
void B(T *list,int length)
{
T temp;
for(int i=length-1;i>=1;i--)
for(int j=0;j<=i;j++)
{
if(list[j]<list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
int main()
{
int nums[]={1,2,3,9,5};
B(nums,5);
for(int i=0;i<5;i++)
{
cout<<nums[i]<<" ";
}
cout<<endl;
char chars[]={'a','b','g','e','u','q'};
B(chars ,6);
for(int i=0;i<6;i++)
{
cout<<chars[i]<<" ";
}
return 0;
例5-1
//类的定义的示例;
class Tclock
{
public:
void settime(int newH ,int newM ,int news);
void showtime();
private :
int hour,minute,second;
};
void Tclock::settime(int H,int M,int S)
{
hour=H;minute=M;second=S;
}
void Tclock::showtime ()
{
cout<<hour<<":"<<minute <<":"<<second;
}
例5-2
//类的定义的示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class Tclock
{
public:
void settime(int newH ,int newM ,int news);
void showtime();
private :
int hour,minute,second;
//void showtime();
};
void Tclock::settime(int H,int M,int S)
{
hour=H;minute=M;second=S;
}
void Tclock::showtime ()
{
cout<<hour<<":"<<minute <<":"<<second;
}
void main()
{
Tclock c1;
c1.settime (20,12,15);
c1.showtime ();
}
例5-3
//对象的使用的示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class Tclock
{
public:
void settime(int newH ,int newM ,int news);
void show();
private :
int hour,minute,second;
};
class Tdate
{
public:
int year,month,day;
void setdate(int Y,int M,int D)
{
year=Y;month=M;day=D;
}
void show()
{
cout <<year<<"-"<<month<<"-"<<day<<endl;
}
};
void Tclock::settime(int H,int M,int S)
{
hour=H;minute=M;second=S;
}
void Tclock::show()
{
cout<<hour<<":"<<minute <<":"<<second;
}
void main()
{
Tclock c1;
Tdate d1,*d2;
d2=&d1;
d2->setdate(2014,9,10);
(*d2).show();
c1.settime (20,12,15);
c1.show();
}
例 5-4
//对象赋值示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
/*class Tclock
{
public:
void settime(int newH ,int newM ,int news);
void showtime();
private :
int hour,minute,second;
};*/
class Tdate
{
public:
int year,month,day;
void setdate(int Y,int M,int D)
{
year=Y;month=M;day=D;
}
void show()
{
cout <<year<<"-"<<month<<"-"<<day<<endl;
}
};
/*void Tclock::settime(int H,int M,int S)
{
hour=H;minute=M;second=S;
}
void Tclock::showtime ()
{
cout<
void main()
{
Tdate d1,d2;
d1.setdate (2014,9,10);
d1.show();
d2.setdate (2014,10,10);
d2.show();
d1=d2;
d1.show();
d2.show();
}
例5-5
//内联函数示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class Tcircle
{
public:
double area(double radius)
{
return 3.1415*radius*radius;
}
};
void main()
{
Tcircle c1;
double r,s;
cout<<"please input r:"<<endl;
cin>>r;
s=c1.area (r);
cout<<"area="<<s<<endl;
}
例5-6
//内联函数示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class Tcircle
{
public:
double area(double radius);
};
inline double Tcircle::area(double radius)
{
return 3.1415*radius*radius;
}
void main()
{
Tcircle c1;
double r,s;
cout<<"please input r:"<<endl;
cin>>r;
s=c1.area (r);
cout<<"area="<<s<<endl;
}
例5-7
//构造函数示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class A
{
float x,y;
public:
A(float a,float b)
{
x=a;
y=b;
}
void set(float a,float b)
{
x=a;
y=b;
}
void print(void)
{cout<<"x="<<x<<'\t'<<"y="<<y<<endl;
}
};
void main()
{
A a1(2,3);
A a2(4,5);
a2.set(7,8);
a1.print();
a2.print();
}
例5-8
//构造函数示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class record
{
private:
char bookname[30];
int number;
public:
record();
record(char *a,int b);
void show();
};
record::record ()
{
strcpy_s(bookname,"\0");
number=0;
}
record::record(char *a,int b)
{ strcpy_s(bookname,a);
number=b;
}
void record::show ()
{
cout<<"bookname is:"<<bookname<<endl;
cout<<"booknumber is :"<<number<<endl;
}
int main()
{
record mybook("visual c++ 6.0",10020);
mybook.show();
record yourbook;
yourbook.show();
}
例5-9
//构造函数示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class record
{
private:
char bookname[30];
int number;
public:
record(char *a,int b);
void show();
};
record::record(char *a="vc++",int b=10020)
{ strcpy_s(bookname,a);
number=b;
}
void record::show ()
{
cout<<"bookname is:"<<bookname<<endl;
cout<<"booknumber is :"<<number<<endl;
}
int main()
{
record mybook;
mybook.show();
}
例5-10
//析构函数示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class teacher
{
char *name;
int age;
public:
teacher(char *i,int a)
{
name=new char[strlen(i)+1];
strcpy(name ,i);
age=a;
cout<<"\n执行构造函数teacher"<<endl;
}
~teacher()
{
delete[] name;
cout<<"执行析构函数"<<endl<<endl;
}
void show()
{
cout<<"姓名:"<<name<<" "<<"年龄:"<<age<<endl;
}
};
int main()
{
teacher obj("张立三",25);
obj.show();
}
例5-11
//析构函数示例;
#include "stdafx.h"
#include"iostream"
using namespace std;
class A
{
float x,y;
public:
A(float a,float b)
{ x=a;y=b;
cout<<"初始化自动局部对象\n";
}
A()
{ x=0;y=0;
cout<<"初始化静态局部对象\n";
}
A(float a)
{ x=a;y=0;
cout<<"初始化全局对象\n";
}
~A()
{cout<<"调用析构函数"<<endl;
}
};//
A a0(100.0);
void f(void)
{
cout<<"进入f()函数"<<endl;
A ab(10.0,20.0);
static A a3;
}
void main()
{
cout<<"进入main函数"<<endl;
f();f();
}
例5-12
//通过类名访问静态数据成员;
#include "stdafx.h"
#include"iostream"
using namespace std;
class counter
{
public:
static int num;
};
int counter::num=0;
void main()
{
counter::num=20;
cout<<counter::num<<endl;
}
例5-13
//通过对象名访问静态数据成员;
#include "stdafx.h"
#include"iostream"
using namespace std;
class counter
{
public:
static int num;
counter(){cout<<++num<<" ";}
~counter(){cout<<--num<<" ";}//main函数结束后系统自动调用析构函数
};
int counter::num=0;
void main()
{
counter a,b,c;//调用三次构造函数
cout<<endl;
cout<<a.num<<endl;
cout<<counter::num<<endl;
}
例5-14
//this指针的应用;
#include "stdafx.h"
#include"iostream"
using namespace std;
class point
{
private:
int x,y;
public:
point(int a,int b)
{
x=a;
y=b;
}
void movepoint(int a,int b)
{
x+=a;
y+=b;
}
void print()
{
cout<<"x="<<x<<"y="<<y<<endl;
}
};
int main()
{
point point1(10,10);
point1.movepoint(2,2);
point1.print();
return 0;
}
例5-15
//友元函数的应用
#include "stdafx.h"
#include"iostream"
#include"math.h"
using namespace std;
class point
{
private:
int x,y;
public:
point(int xx=0,int yy=0)
{
x=xx;
y=yy;
}
friend double distance(point &a,point &b);//distance函数可以访问point中的成员,包括私有成员;
};
double distance(point &a,point &b)
{
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
int main()
{
point p1(3.0,5.0),p2(4.0,6.0);
double d=distance(p1,p2);
cout<<"the distance is"<<d<<endl;
return 0;
}
例5-16
//友元类的应用
#include "stdafx.h"
#include"iostream"
using namespace std;
class X /
{
public:
friend class Y;
void set(int i)
{
x=i;
}
void display()
{
cout<<"x="<<x<<"y="<<y<<endl;
}
private:
int x;
static int y;
};
class Y ///
{
public:
Y(int i,int j);
void display();
private:
X a;
};
int X::y=10; ///
Y::Y(int i,int j)
{
a.x=i;
X::y=j;
}
void Y::display() ///
{
cout<<"x="<<a.x<<"y="<<a.y<<endl;
}
void main()
{
X b;
b.set(5);
b.display();
Y c(6,9);
c.display();
b.display();
}
例6-1
//成员函数重载
#include "stdafx.h"
#include"iostream"
using namespace std;
class sample
{
private:
int i;
double d;
public:
void setdata(int n){i=n;d=0;}
void setdata(int n,double x){i=n,d=x;}
void disp()
{ cout<<"i="<<i<<",d="<<d<<endl;
}
};
void main()
{
sample s;
s.setdata(15);
s.disp();
s.setdata(20,23.8);
s.disp();
}
例6-2
//双目运算符重载示例
#include "stdafx.h"
#include"iostream"
using namespace std;
class complex
{
public:
complex(){real=imag=0;}
complex(double r,double i){real=r;imag=i;}
complex operator +(const complex &c);
friend void print(const complex &c);
private:
double real,imag;
};
inline complex complex::operator+(const complex &c)
{
return complex(real +c.real,imag+c.imag);
}
void print(const complex &c)
{
cout<<c.real<<"+"<<c.imag<<"i";
}
void main()
{
complex c1(2.0,3.0),c2(4.0,-2.0),c3;
c3=c1+c2;
cout<<"c1+c2=";
print(c3);
}
例6-3
//单目运算符重载示例
#include "stdafx.h"
#include"iostream"
using namespace std;
class time
{
public:
time(){minute=0;sec=0;}
time(int m,int s):minute(m),sec(s){}
time operator++();
void display(){cout<<minute<<":"<<sec<<endl;}
private:
int minute;
int sec;
};
time time::operator++()
{
if(++sec>=60)
{
sec-=60;
++minute;
}
return *this;
}
int main()
{
time time1(34,0);
for(int i=0;i<60;i++)
{
++time1;
time1.display();
}
return 0;
}
例6-4
//修改例6-3位运算符重载位类的友元函数
#include "stdafx.h"
#include"iostream"
using namespace std;
class time
{
public:
time(){minute=0;sec=0;}
time(int m,int s):minute(m),sec(s){}
friend time operator++(time &a);
void display(){cout<<minute<<":"<<sec<<endl;}
private:
int minute;
int sec;
};
time operator++(time &a)
{
if(++a.sec>=60)
{
a.sec-=60;
++a.minute;
}
return a;
}
int main()
{
time time1(34,0);
for(int i=0;i<60;i++)
{
++time1;
time1.display();
}
return 0;
}
例6-5
//派生类示例1
#include "stdafx.h"
#include"iostream"
using namespace std;
class A
{
private:
int privA;
protected:
int protA;
public:
int pubA;
};
class C:protected A
{
public:
void fn()
{
int a;
a=privA;
a=protA;
a=pubA;
}
};
void main()
{
C obj2;
obj2.privA=1;
obj2.protA=1;
obj2.pubA=1;
}
例6-6
/派生类构造函数和析构函数的示例
#include "stdafx.h"
#include"iostream"
using namespace std;
class parent_class
{
int private1,private2;
public:
parent_class(int p1,int p2):private1(p1),private2(p2)
{}
int inc1()
{ return ++private1;
}
void display()
{
cout<<"private1="<<private1<<",private2="<<private2<<endl;
}
};
class derived_class:private parent_class
{
int private3;
parent_class obj4;
public:
derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),obj4(p3,p4),private3(p5)
{}
int inc1()
{
return parent_class::inc1();
}
void display()
{
parent_class::display();
obj4.display();
cout<<"private3="<<private3<<endl;
}
};
void main()
{
derived_class d1(18,18,1,2,-5);
d1.inc1();
d1.display();
}
例6-7
//多重继承示例
#include "stdafx.h"
#include"iostream"
using namespace std;
class A
{
int a;
public:
A(int i)
{
a=i;
cout<<"A constructor"<<endl;
}
void view()
{
cout<<"a="<<a<<endl;
}
};
class B
{
int b;
public:
B(int j)
{
b=j;
cout<<"B constructor"<<endl;
}
void view()
{
cout<<"b="<<b<<endl;
}
};
class C:public A,public B
{
int c;
public:
C(int k):A(++k),B(--k)
{
c=k;
cout<<"C constructor"<<endl;
}
void view()
{
A::view();
B::view();
cout<<"c="<<c<<endl;
}
};
void main()
{
C c1(10);
c1.view();
}
例6-8
//赋值相容示例
#include "stdafx.h"
#include"iostream"
using namespace std;
#include"string"
class ABCBase
{
private:
string ABC;
public:
ABCBase(string abc)
{
ABC=abc;
}
void showABC();
};
void ABCBase::showABC()
{
cout<<"字母ABC=>"<<ABC<<endl;
}
class X:public ABCBase
{
public:
X(string x):ABCBase(x){}
};
void function(ABCBase &base)
{
base.showABC();
}
int main()
{
ABCBase base("A");
base.showABC();
X x("B");
base=x;
base.showABC();
ABCBase &base1=x;
base1.showABC();
ABCBase *base2=&x;
base2->showABC();
function(x);
return 0;
}
例6-9
//非虚函数方式实现日期显示示例
#include "stdafx.h"
#include"iostream"
using namespace std;
class base
{
protected:
int year;
public:
base(int y)
{
year=y;
}
void display()
{
cout<<"year="<<year<<endl;
}
};
class second:public base
{
protected:
int month;
public:
second(int y,int m):base(y)
{
month=m;
}
void display()
{
cout<<"year-month"<<year<<"-"<<month<<endl;
}
};
class third:public second
{
int day;
public:
third(int y,int m,int d):second(y,m)
{
day=d;
}
void display()
{
cout<<"year-month-day"<<year<<"-"<<month<<"-"<<day<<endl;
}
};
int main()
{
base b(2015);
second s(2015,1);
third t(2015,1,20);
base *p;
p=&b;
p->display();
p=&s;
p->display();
((second*)p)->display();
p=&t;
p->display();
((third*)p)->display();
return 0;
}
例6-10
//虚函数应用
#include "stdafx.h"
#include"iostream"
using namespace std;
class base
{
protected:
int year;
public:
base(int y)
{
year=y;
}
virtual void display()
{
cout<<"year="<<year<<endl;
}
};
class second:public base
{
protected:
int month;
public:
second(int y,int m):base(y)
{
month=m;
}
void display()
{
cout<<"year-month"<<year<<"-"<<month<<endl;
}
};
class third:public second
{
int day;
public:
third(int y,int m,int d):second(y,m)
{
day=d;
}
void display()
{
cout<<"year-month-day"<<year<<"-"<<month<<"-"<<day<<endl;
}
};
int main()
{
base b(2015);
second s(2015,1);
third t(2015,1,20);
base *p;
p=&b;
p->display();
p=&s;
p->display();
p->display();
p=&t;
p->display();
p->display();
return 0;
}
例6-11
//虚函数应用
#include "stdafx.h"
#include"iostream"
using namespace std;
class base
{
public:
~base()
{
cout<<"~base()is called"<<endl;
}
};
class derived:public base
{
public:
~derived()
{
cout<<"~dervied() is called"<<endl;
}
};
int main()
{
derived *p1=new derived;
cout<<"delete p1:"<<endl;
delete p1;
base *p2=new derived;
cout<<endl<<"delete p2:"<<endl;
delete p2;
base *p3=new derived;
cout<<endl<<"delete p3:"<<endl;
delete(derived*) p3;
cout<<endl;
return 0;
}
例6-12
//虚函数应用
#include "stdafx.h"
#include"iostream"
using namespace std;
class base
{
public:
virtual ~base()
{
cout<<"~base()is called"<<endl;
}
};
class derived:public base
{
public:
~derived()
{
cout<<"~dervied() is called"<<endl;
}
};
int main()
{
base *p3=new derived;
cout<<endl<<"delete p3:"<<endl;
delete p3;
cout<<endl;
return 0;
}
例6-13
//简单异常处理示例
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int m,n;
cout<<"please input two inteders:";
cin>>m>>n;
try
{
if(n==0)
throw 0;
cout<<(m/n)<<endl;
}
catch(int)
{
cout<<"divided by 0!"<<endl;
return -1;
}
}
例6-13
//简单异常处理示例
#include "stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int m,n;
cout<<"please input two inteders:";
cin>>m>>n;
try
{
if(n==0)
throw 0;
cout<<(m/n)<<endl;
}
catch(int)
{
cout<<"divided by 0!"<<endl;
return -1;
}
return 0;
}
例6-14
/成员函数抛出异常示例
#include "stdafx.h"
#include"iostream"
using namespace std;
class complex
{
public:
complex(){real=0;imag=0;}
complex(double r,double i){real=r;imag=i;}
complex divide(const complex x);
void display();
private:
double real;
double imag;
};
void complex::display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;}
complex complex::divide(const complex x)
{
float c=x.real*x.real+x.imag*x.imag;
if(c==0)
throw 0;
return
complex((real*x.real+imag*x.imag)/c,(x.real*imag-real*x.imag)/c);
}
int main()
{
complex x(1,1), y(0,0),z;
try
{
z=x.divide(y);
z.display();
}
catch(int)
{
cout<<"error:divided by zeros!"<<endl;
}
return 1;
}
例6-15
//标准异常类示例
#include "stdafx.h"
#include"iostream"
#include"string"
using namespace std;
void main()
{
char *buf;
try
{
buf=new char[100];
if(buf==0)throw bad_alloc();
}
catch(bad_alloc)
{
cout<<"内存分配失败"<<endl;
}
getchar();
}
例6-15
// p2_1.cpp : 定义控制台应用程序的入口点。
//
//自定义异常类示例
#include "stdafx.h"
#include"iostream"
#include"string"
using namespace std;
class zerodivide
{
public:
zerodivide(string desc)
{
this->desc=desc;
}
string getmessage()
{return desc;}
private:string desc;
};
int divide(int x,int y)
{
if(y==0) throw zerodivide("除数为");
return x/y;
}
int main()
{
int x,y;
cout<<"输入两个数:";
while(cin>>x>>y)
{
try
{
cout<<divide(x,y)<<endl;
}
catch(zerodivide e){
cout<<e.getmessage()<<endl;
}
cout<<"继续输入数值<退出>";
}
return 0;
}