}
/*
//输出结果
Starting1...
Defaultconstructorcalled.
Defaultconstructorcalled.
Defaultconstructorcalled.
Ending1...
Starting2...
Constructora=7,b=8
Constructora=9,b=10
Constructora=11,b=12
Ending2...
Destructorcalled.a=11,b=12
Destructorcalled.a=9,b=10
Destructorcalled.a=7,b=28
Destructorcalled.a=5,b=6
Destructorcalled.a=3,b=4
Destructorcalled.a=1,b=2
*/
//8.17创建一个Triangle类,这个类将直角三角形的底边长度和高作为私有成员。
类中包含设置
//这些值的构造函数。
两个成员函数:
hypot()返回直角三角形斜边的长度,area()返回三角形的面积。
#include"stdafx.h"
#include
#include
usingnamespacestd;
classTriangle
{
public:
Triangle(floatL,floatH)
{
Length=L;
Hight=H;
}
floathypot()
{returnsqrt(Length*Length+Hight*Hight);
}
floatarea()
{returnLength*Hight/2;
}
private:
floatLength;
floatHight;
};
voidmain()
{
Trianglet(3,4);
cout<cout<}
//8.18定义一个Circle类,包含数据成员Radius(半径)和计算周长和面积的成员函数,并构
//造Circle的对象进行测试。
#include"stdafx.h"
#include
#include
usingnamespacestd;
constfloatPI=3.14159;
classCircle
{
public:
Circle(floatr)
{
radius=r;
}
floatperimeter()
{
return2*PI*radius;
}
floatarea()
{
returnPI*radius*radius;
}
private:
floatradius;
};
voidmain()
{
Circlec1(3.);
cout<cout<};
自己编的:
#include
usingnamespacestd;
#include"stdafx.h"
classcircle
{
public:
circle(intr)
{
rad=r;
}
doublearea()
{
return3.14*rad*rad;
}
private:
intrad;
};
voidmain()
{
circlec
(1);
cout<}
//8.20设计一个用于人事管理的Person(人员)组合类。
人员属性为:
number(编号)、sex(性别),
//birthday(出生日期)、id(身份证号)等。
其中“出生日期”定义为一个“日期”类内嵌对象。
//用成员函数实现对人员信息的录入和显示。
要求具有构造函数、析构函数、拷贝构造函数、内
//联成员函数、带缺省形式参数值的成员函数。
#include"stdafx.h"
#include
#include
usingnamespacestd;
classDate
{
public:
Date(inty=1900,intm=1,intd=1)
{
year=y;
month=m;
day=d;
}
voidOutput()
{
cout<}
private:
intyear;
intmonth;
intday;
};
classPerson
{
public:
Person(Dated=Date(1993,1,1),intn=0,char*s="NoName",boolb=0):
birthday(d)
{
number=n;
name=newchar[strlen(s)+1];
if(name!
=NULL)
strcpy(name,s);
sex=b;
};
Person(Person&p)
{
number=p.number;
name=newchar[strlen(p.name)+1];
if(name!
=NULL)
strcpy(name,p.name);
sex=p.sex;
birthday=p.birthday;
};
~Person()
{
delete[]name;
name=NULL;
}
voidInput();
voidOutput()
{
cout<<"number="<if(name!
=NULL)
cout<<",name="<cout<<",sex="<cout<<",birthday=";
birthday.Output();
};
private:
intnumber;
char*name;
boolsex;
Datebirthday;
};
voidPerson:
:
Input()
{
cout<<"Pleaseinputthenumber:
";
cin>>number;
cout<<"Pleaseinputthename:
";
cin>>name;
cout<<"Pleaseinputthesex:
(0or1)";
cin>>sex;
cout<<"Pleaseinputthebirthday(year/month/day):
";
inty,m,d;
cin>>y>>m>>d;
birthday=Date(y,m,d);
}
voidmain()
{
Personp1;
Personp2(p1);
p1.Input();
p1.Output();
p2.Output();
Personp3(Date(1998,12,12),1,"zhangsan",1);
p3.Output();
}
数组和指针
1.已知一个数组a[10],并在其中保存了下列数据:
6、12、34、56、78、89、90、409、800、2300
要求:
(1)从键盘上任意输入一个数,并将该数插入到数组的合适位置。
(要求:
用指针完成)。
(2)打印出插入前和插入后的数组内容。
#include
usingnamespacestd;
voidmain()
{
inta[10]={6,12,34,56,78,89,90,409,800,2300};
intb,c;
cout<<"请输入一个数:
"<cin>>b;
cout<<"请输入想插入的位置(数组的哪个元素-9)"<cin>>c;
intd[11];
int*pa=a;
int*pb=d;
for(intj=0;j{
*pb++=*pa++;
}
pb=d;
*(pb+c)=b;
for(intk=c+1;k<11;k++)
*(pb+k)=a[k-1];
cout<<"插入前的数组是:
"<for(intm=0;m<10;m++)
cout<cout<cout<<"插入后的数组是:
"<for(inti=0;i<11;i++)
cout<<*(pb+i)<<"";
cout<}
用指针实现字符串顺序交换
#include
usingnamespacestd;
voidmain()
{
chars[]="abcdefghijklmnopqrstuvwxyz";
charch,*t=&s[strlen(s)-1];
for(inti=0;i<(strlen(s)-1)/2;i++)
{
ch=s[i];
s[i]=*(t-i);
*(t-i)=ch;
}
for(intk=0;kcout<
cout<
cout<}
普通转换,调换字符串顺序
#include
usingnamespacestd;
voidmain()
{
chars[10]="student",t;
inti=0,j=strlen(s)-1;
while(i{
t=s[i];s[i]=s[j];s[j]=t;i++;j--;
}
cout<
}
将十进制转换成其他进制(小于十进制)
#include
usingnamespacestd;
voidmain()
{
intn,b,a[20];
cin>>n>>b;
for(inti=0;n>0;i++)
{
a[i]=n%b;
n/=b;
}
for(intj=i-1;j>=0;j--)
cout<cout<}
运算符?
?
?
#include
classA{
public:
A():
a(0){}
A&operator++()
{
a++;
return*this;
}
Aoperator++(int)
{
Atemp(*this);
a++;
returntemp;
}
voiddisplay(){cout<protected:
inta;
};
voidmain(){
Aa;
(++a).display();
a++.display();
}
二维数组转换为一维数组
#include
usingnamespacestd;
longsum_array(intarray[],intelements)
{