C++ primer plus第六版课后编程练习答案:8.4

#define  _CRT_SECURE_NO_WARNINGS 
#include 
#include

using namespace std;

struct stringy{
    char* str;
    int ct;
};

void set(stringy& s1, const char* c1);
void show(const stringy& s2, int n = 1);//函数重载
void show(const char* t, int n = 1);

int main()
{
    stringy beany;
    char testing[] = "Reality isn't what it used to be.";

    set(beany, testing);
    int n = strlen(testing);
    char* p1 = new char[n + 1];
    show(beany);
    show(beany,2);
    delete[]beany.str;//原书是没有delete的,但是我不知道怎么在被调用函数里加进去,只好在这里加了。
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);
    show(testing, 3);
    show("Done!");

    return 0;
}

void set(stringy& s1, const char* c1)
{
    int n = strlen(c1);
    s1.str = new char[n + 1];
    strcpy(s1.str, c1);
    s1.ct = n;
    //delete[] s1.str;
}

void show(const stringy& s2, int n)
{
    for (int i = 0; i < n; i++)
        cout << s2.str << endl;
}

void show(const char* t, int n)
{
    for (int i = 0; i < n; i++)
        cout << t << endl;
}

你可能感兴趣的:(c语言,C++,primer,plus第六版课后编程练习)