for(i=0;i<8;i++)
cout<
}
例:
2.1使用成员函数的实例
#include
usingnamespacestd;
structPoint{
voidSetxy(doublea,doubleb)//成员函数,用来重新设置数据成员
{x=a;y=b;}
voidDisplay()//成员函数,按指定格式输出数据成员的值
{cout<doublex,y;//数据成员
}
;
voidmain(){
Pointa;//定义对象a
a.Setxy(10.6,18.5);//设置对象a的数据成员
a.Display();//显示对象a的数据成员
cout<}
例2.2使结构具有封装性的实例
#include
usingnamespacestd;
structPoint{
private:
doublex,y;//数据成员
public:
voidSetxy(doublea,doubleb)//成员函数,用来重新设置数据成员
{x=a;y=b;}
voidDisplay()//成员函数,按指定格式输出数据成员的值
{cout<};
例2.3使用构造函数初始化对象的实例
#include
usingnamespacestd;
structPoint{
private:
doublex,y;//数据成员
public:
Point(){};
Point(doublea,doubleb)
{x=a;y=b;}
voidSetxy(doublea,doubleb)
{x=a;y=b;}
voidDisplay()
{cout<};
voidmain(){
Pointa;
Pointb(18.5,10.6);
a.Setxy(10.6,18.5);
a.Display();
b.Display();
}
例:
2.4定义类的实例
#include
usingnamespacestd;
classpoint{
private:
doublex,y;//类point的数据成员
public:
point(){};//类point的无参数构造函数
point(doublea,doubleb)//具有两个参数的构造函数
{x=a;y=b;}
voidsetxy(doublea,doubleb)//成员函数,用来重新设置数据成员
{x=a;y=b;}
voiddiaplay()//成员函数,按指定格式输出数据成员的值
{cout<};
例2.8演示使用string对象及初始化的例子
#include
#include
usingnamespacestd;
voidmain(){
stringstr1("wearehere!
");
stringstr2="whereareyou?
";
cout<cout<cout<<"pleaseinputaword:
";
cin>>str1;
cout<<"lengthofthe"<}
例:
2.10演示使用complex和string对象及初始化的例子
#include
#include
#include
usingnamespacestd;
voidmain(){
complexnum1(2,3);
complexnum2(3.5,4.5);
stringstr1("realis");
stringstr2="iamgeis";
cout<cout<}
例2.11演示string对象的例子
#include
#include
#include
usingnamespacestd;
voidmain(){
stringstr1="wearehere!
",str2=str1;
reverse(&str1[0],&str1[0]+12);//str字符串的元素逆向
cout<copy(&str1[0],&str1[0]+12,&str2[0]);//原样复制到str2
cout<reverse_copy(&str2[0],&str2[0]+12,ostream_iterator(cout));//逆向输出str2
cout<}
例:
演示string对象使用成员函数表示存储区间的例子
#include
#include
#include
#include
usingnamespacestd;
voidmain(){
stringstr1="wearehere!
",str2(str1);//使用str1初始化str2
reverse(str1.begin(),str1.end());//字符串的元素逆向
cout<copy(str1.begin(),str1.end(),str2.begin());//原样复制到str2,str2应能容纳下str1
sort(str1.begin(),str1.end());//默认升幂排序
cout<cout<reverse_copy(str1.begin(),str1.end(),str2.begin());//逆向复制到str2
cout<reverse(str2.begin()+2,str2.begin()+8);
copy(str2.begin()+2,str2.begin()+8,ostream_iterator(cout));//输出逆向后的部分内容
cout<sort(str1.begin(),str1.end(),greater());//降幂排序
cout<str1.swap(str2);//互相交换内容
cout<cout<<(*find(str1.begin(),str1.end(),'e')=='e')<<""//注意不是成员函数find
<<(*find(str1.begin(),str1.end(),'0')=='0')<}
例2.13演示string对象数组的例子
#include
#include
#include
usingnamespacestd;
voidmain(){
stringstr[]={"wearehere!
","whereareyou?
","welcome!
"};
for(inti=0;i<3;i++){
copy(str[i].begin(),str[i].end(),ostream_iterator(cout));
cout<}
str[0].swap(str[2]);
str[0].swap(str[1]);
for(i=0;i<3;i++)
cout<}
例:
3.1传对象不会改变原来对象数据成员值的例子
#include
#include
usingnamespacestd;
voidswap(string,string);//使用string类的对象作为函数参数
voidmain(){
stringstr1("现在"),str2("过去");//定义对象str1和str2
swap(str1,str2);//使用传值方式传递str1和str2的数据成员值
cout<<"返回后:
str1="<}
voidswap(strings1,strings2)///string类的对象s1,s2作为函数参数
{
stringtemp=s1;s1=s2;s2=temp;
cout<<"交换为:
str1="<}
例3.2使用对象指针作为函数参数的例子
#include
#include
usingnamespacestd;
voidswap(string*,string*);//使用string类的指针作为函数参数
voidmain(){
stringstr1("现在"),str2("过去");//定义对象str1和str2
swap(&str1,&str2);//使用传地址值方式传递str1和str2的地址值
cout<<"返回后:
str1="<}
voidswap(string*s1,string*s2)//string类的对象指针s1和s2作为函数参数
{
stringtemp=*s1;*s1=*s2;*s2=temp;
cout<<"交换为:
str1="<<*s1<<"*str2="<<*s2<}
例3.3传递数组名实例
#