Skip to content

Commit

Permalink
chapter12 stringbad
Browse files Browse the repository at this point in the history
  • Loading branch information
hzchenxiaobin committed Apr 26, 2023
1 parent a204b91 commit ea3ebbb
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 0 deletions.
Binary file added chapter12/strngbad/a.out
Binary file not shown.
35 changes: 35 additions & 0 deletions chapter12/strngbad/strngbad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "strngbad.h"
#include <cstring>

int StringBad::num_strings = 0;

StringBad::StringBad(const char *s)
{
len = strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings++;
cout << num_strings << ":\"" << str << "\" object created\n";
}

StringBad::StringBad()
{
len = 4;
str = new char[4];
std::strcpy(str, "C++");
cout << num_strings << ":\"" << str << "\" default object created\n";
}

StringBad::~StringBad()
{
cout << "\"" << str << "\" object deleted, ";
--num_strings;
cout << num_strings << " left\n";
delete[] str;
}

std::ostream &operator<<(std::ostream &os, const StringBad &st)
{
os << st.str;
return os;
}
22 changes: 22 additions & 0 deletions chapter12/strngbad/strngbad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef STRNGBAD_H_
#define STRNGBAD_H_

#include <iostream>

using namespace std;

class StringBad
{
private:
char *str;
int len;
static int num_strings;

public:
StringBad(const char *s);
StringBad();
~StringBad();
friend std::ostream &operator<<(std::ostream &os, const StringBad &st);
};

#endif
40 changes: 40 additions & 0 deletions chapter12/strngbad/vegnews.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include "strngbad.h"

using std::cout;
void callme1(StringBad &rsb);
void callme2(StringBad rsb);

int main()
{
using std::endl;
{
cout << "Starting an inner block.\n";
StringBad headline1("Celery Stalks at Midnight");
StringBad headline2("Lettuce Prey");
StringBad sports("Spinach Leaves Bowl for Dollars");
cout << "headline1: " << headline1 << endl;
cout << "headline2: " << headline2 << endl;
cout << "sports: " << sports << endl;

callme1(headline1);
cout << "headline1: " << headline1 << endl;

callme2(headline2);
cout << "headline2: " << headline2 << endl;
}

return 0;
}

void callme1(StringBad &rsb)
{
cout << "String passed by reference:\n";
cout << " \"" << rsb << "\"\n";
}

void callme2(StringBad rsb)
{
cout << "String passed by value:\n";
cout << " \"" << rsb << "\"\n";
}
Binary file added chapter12/strngbad1/a.out
Binary file not shown.
55 changes: 55 additions & 0 deletions chapter12/strngbad1/strngbad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "strngbad.h"
#include <cstring>

int StringBad::num_strings = 0;

StringBad::StringBad(const char *s)
{
len = strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings++;
cout << num_strings << ":\"" << str << "\" object created\n";
}

StringBad::StringBad()
{
len = 4;
str = new char[4];
std::strcpy(str, "C++");
cout << num_strings << ":\"" << str << "\" default object created\n";
}

StringBad::StringBad(const StringBad &st)
{
num_strings++;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
cout << num_strings << ": \"" << str << "\" object created\n";
}

StringBad::~StringBad()
{
cout << "\"" << str << "\" object deleted, ";
--num_strings;
cout << num_strings << " left\n";
delete[] str;
}

std::ostream &operator<<(std::ostream &os, const StringBad &st)
{
os << st.str;
return os;
}

StringBad &StringBad::operator=(const StringBad &st)
{
if (this == &st)
return *this;
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
25 changes: 25 additions & 0 deletions chapter12/strngbad1/strngbad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef STRNGBAD_H_
#define STRNGBAD_H_

#include <iostream>

using namespace std;

class StringBad
{
private:
char *str;
int len;
static int num_strings;

public:
StringBad(const char *s);
StringBad();
StringBad(const StringBad & s);
~StringBad();

StringBad &operator=(const StringBad & st);
friend std::ostream &operator<<(std::ostream &os, const StringBad &st);
};

#endif
40 changes: 40 additions & 0 deletions chapter12/strngbad1/vegnews.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include "strngbad.h"

using std::cout;
void callme1(StringBad &rsb);
void callme2(StringBad rsb);

int main()
{
using std::endl;
{
cout << "Starting an inner block.\n";
StringBad headline1("Celery Stalks at Midnight");
StringBad headline2("Lettuce Prey");
StringBad sports("Spinach Leaves Bowl for Dollars");
cout << "headline1: " << headline1 << endl;
cout << "headline2: " << headline2 << endl;
cout << "sports: " << sports << endl;

callme1(headline1);
cout << "headline1: " << headline1 << endl;

callme2(headline2);
cout << "headline2: " << headline2 << endl;
}

return 0;
}

void callme1(StringBad &rsb)
{
cout << "String passed by reference:\n";
cout << " \"" << rsb << "\"\n";
}

void callme2(StringBad rsb)
{
cout << "String passed by value:\n";
cout << " \"" << rsb << "\"\n";
}

0 comments on commit ea3ebbb

Please sign in to comment.