Using a reference parameter for class type
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class StringClass {
char *p;
public:
StringClass(char *s);
~StringClass() {
delete [] p;
}
char *get() {
return p;
}
};
StringClass::StringClass(char *s)
{
int l;
l = strlen(s)+1;
p = new char [l];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
strcpy(p, s);
}
// Using a reference parameter.
void show(StringClass &x)
{
char *s;
s = x.get();
cout << s << endl;
}
int main()
{
StringClass stringObject1("Hello"), stringObject2("There");
show(stringObject1);
show(stringObject2);
return 0;
}
Related examples in the same category