【C++】对象做为函数参数【原创技术】


 

题目:php

对象做为函数参数ios

l 对象自己作参数(传值),传对象副本ide

l 对象引用作参数(传地址),传对象自己函数

l 对象指针作参数(传地址),传对象自己学习


源代码:
//科目:C++实验4-2
//题目:对象的调用
//做者:武叶
//语言:C++
//创做时间:2012年4月16日
#include < iostream.h>
#include <string.h>
#include < stdlib.h>
class CStrSub
{
char *str;
public:
CStrSub(char *s);
CStrSub(CStrSub &);
~ CStrSub();
void set(char *s);
void show()
{
cout<<str<<endl;
}
};
CStrSub:: CStrSub(char *s)
{
str=new char[strlen(s)+1];
if(!str)
{
cout<<"申请空间失败!"<<endl;
exit(-1);
}
strcpy(str,s);
}
CStrSub:: CStrSub(CStrSub & temp)
{
str=new char[strlen(temp.str)+1];
if(!str)
{
cout<<"申请空间失败!"<<endl;
exit(-1);
}
strcpy(str,temp.str);
}
CStrSub:: ~ CStrSub( )
{
if(str!=NULL) delete [ ]str;
}
void CStrSub::set(char *s)
{
delete [ ]str;
str=new char[strlen(s)+1];
if(!str)
{
cout<<"申请空间失败!"<<endl;
exit(-1);
}
strcpy(str,s);
}
CStrSub input(CStrSub *temp)
{
char s[20];
cout<<"输入字符串:"<<endl;
cin>>s;
temp->set(s);
return *temp;
}
void main()
{
CStrSub a("hello");
a.show( );
CStrSub b=input(&a);
a.show( );
b.show( );
}
 指针

更多详细内容:::::去学习对象