Total members 11895 |It is currently Mon Dec 30, 2024 5:46 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





here , A copy constructor to allow StringClass objects to be passed to functions
cpp code
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

class StringClass {
char *p;
public:
StringClass(char *s); // constructor
StringClass(const StringClass &o); // copy constructor
~StringClass() { // destructor
delete [] p;
}
char *get() {
return p;
}
};


StringClass::StringClass(char *s) // "Normal" constructor
{
int l;

l = strlen(s)+1;

p = new char [l];
if(!p) {
cout << "Allocation error\n";
exit(1);
}

strcpy(p, s);
}


StringClass::StringClass(const StringClass &o) // Copy constructor
{
int l;

l = strlen(o.p)+1;

p = new char [l]; // allocate memory for new copy
if(!p) {
cout << "Allocation error\n";
exit(1);
}

strcpy(p, o.p); // copy string into copy
}

void show(StringClass x)
{
char *s;

s = x.get();
cout << s << endl;
}

int main()
{
StringClass a("www.java2s.com"), b("www.java2s.com");

show(a);
show(b);

return 0;
}




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Copy Constructor
 Using a Constructor in jsp     -  
 What Are Constructor Methods?     -  
 using of String constructor     -  
 Class with a Constructor in php     -  
 class with constructor parameter     -  
 cannot find symbol constructor     -  
 copy folder     -  
 copy file     -  
 I'd like to copy this Skymiles_red site     -  
 SQL COPY TABLE Command     -  



Topic Tags

C++ OOP
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com