【C++】对象作为函数参数【原创技术】
题目:
对象作为函数参数
l 对象本身做参数(传值),传对象副本
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( );
}
更多详细内容:::::去学习
转载于:https://blog.51cto.com/daxiao51/877809
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-16478.html
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网进行投诉反馈,一经查实,立即删除!