大侠们帮帮忙!!实在找不出啥错?
//////#include <iostream>
#ifndef STRNGBAD_H
#define STRNGBAD_H
#include "stringbad1.h"
using namespace std;
class stringbad
{
private:
char *str;
int len;
static int num_strings;
public:
stringbad(const char *s);
stringbad();
~stringbad();
//friend function
friend ostream & operator<<(ostream &os,const stringbad &st);
};
#endif
////////////////////////////////////////////////////
#include <iostream>
#include <cstring>
#include "stringbad1.h"
using namespace std;
int stringbad::num_strings=0;
stringbad::stringbad(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
cout<<num_strings<<":\""<<str<<"\"object created\n";
}
stringbad::stringbad()
{
len=4;
str=new char[4];
strcpy(str,"c++");
num_strings++;
cout<<num_strings<<":\""<<str<<"\"default object created\n";
}
stringbad::~stringbad()
{
cout<<num_strings<<":\""<<str<<"\"object deleted\n";
num_strings--;
cout<<num_strings<<"left\n";
delete [] str;
}
ostream & operator<<(ostream &os,const stringbad &st)
{
os<<st.str;
return os;
}
/////////////////////////////////////////////////////////////
#include <iostream>
#include "stringbad1.h"
using namespace std;
void callme1(stringbad &);
void callme2(stringbad);
int main()
{
stringbad headline1("welcome to here!");
stringbad headline2("shan dong jinan");
stringbad sports("here is my home");
cout<<"headline1:"<<headline1<<endl;
cout<<"headline2:"<<headline2<<endl;
cout<<"sports:"<<sports<<endl;
callme1(headline1);
cout<<"headline1:"<<headline1<<endl;
callme2(headline2);
cout<<"headline2:"<<headline2<<endl;
cout<<"Initialize one object to another:\n";
stringbad sailor=sports;
cout<<"sailor:"<<sailor<<endl;
cout<<"Assign one object to another:\n";
stringbad knot;
knot=headline1;
cout<<"knot:"<<knot<<endl;
cout<<"end of main()\n";
return 0;
}
void callme1(stringbad &rsb)
{
cout<<"string passed by reference:\n";
cout<<"\""<<rsb<<"\"\n";
}
void callme2(stringbad sb)
{
cout<<"string passed by value:\n";
cout<<"\""<<sb<<"\"\n";
}