【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( );
}
 指針

更多詳細內容:::::去學習對象

相關文章
相關標籤/搜索