-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a204b91
commit ea3ebbb
Showing
8 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |