Home > Back-end >  Please help me to look at the add function and display function in the what's the problem
Please help me to look at the add function and display function in the what's the problem

Time:11-07

# define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;

The class MyWord
{
Public:
//define myword members of the class data
Static int num.
Char * pdata;
Int length;
Const int a;
Friend ostream& Operator<(ostream& , MyWord&);//declare friend yuan overloaded operators "& lt; & lt;" The function
Friend istream& Operator> (istream& , MyWord&);//declare friend yuan overloaded operators & gt; "" & gt;" The function



//constructor initializes a
MyWord (char * pd="qwertyuiopasdfghjkl, int n=0) : a (n)
{
Length=strlen (pd);
Pdata=https://bbs.csdn.net/topics/new char [length + 1];
Strcpy (pdata, pd);//copies string pd to pdata
cout <"To start the calls the constructor... "& lt; }
//class of deep copy constructor
MyWord (const MyWord& S) : a (s.a)
{
Length=s.l ength;
Pdata=https://bbs.csdn.net/topics/new char [length + 1];
Strcpy (pdata, supachai panitchpakdi data);
cout <"The deep copy constructor" & lt; Num++;
}
MyWord operator=(const MyWord& S)//overloaded=
{
Length=s.l ength;
Pdata=https://bbs.csdn.net/topics/new char [length + 1];
Strcpy (pdata, supachai panitchpakdi data);
cout <"Call the overloading of the equal sign" & lt; Return * this;
}

~ MyWord ();//
};
Ostream& Operator<(ostream& OS, MyWord& MW)
{
OS & lt; Return the OS;
}
Istream& Operator> (istream& Is, MyWord& MW)
{
Is & gt;> MW. Pdata;
The return is;
}

Int MyWord: : num=0;

The class Sentence: MyWord
{
Public:
Vector a;//inheritance myword
Void Length ();
Void the Add ();
Void the Show ();
Void Menu ();


};

//destructors
MyWord: : ~ MyWord ()
{
cout <"Start the destructor call... "& lt; Pdata [0]='\ 0';
The delete pdata;//data release
}

Void Sentence: : Length ()
{
cout <& lt;" Sentence length is: "& lt; }

Void Sentence: : Add ()
{
MyWord word;
While (cin & gt;> Word)
{
Amy polumbo ush_back (word);
}
}

Void Sentence: : Show ()//for
{

Vector : : iterator m=a. do v.begin ();
for (; m !=a.e nd (); + + m)
{
cout <* m & lt; }

}


Void Sentence: : Menu ()
{
cout cout <"* * * * * * * * * * * * * * * * * * * * * *" & lt; cout <"* * * * * * end of 0 process" & lt; cout <"* * * * * * 1 add word" & lt; cout <"* * * * * * 2 show sentence" & lt; cout <"Number * * * * * * 3 words" & lt; cout <"* * * * * * * * * * * * * * * * * * * * * *" & lt; cout cout <"Please enter the serial number of your choice:";
}
MyWord f (MyWord A)
{
MyWord D (" abs ", 3);
Return D;
}

Int main ()
{
//test for
cout <"* * * * * * * * * * * * * * * * * * * * * * * *" & lt; cout <"Beginning of the test & lt;" cout <"* * * * * * * * * * * * * * * * * * * * * * * *" & lt; Acsd MyWord A (" ");
MyWord C=A;//test deep copy function
C=f (A);
cout <"* * * * * * * * * * * * * * * * * * * * * * * *" & lt; cout <"End of the test & lt;" cout <"* * * * * * * * * * * * * * * * * * * * * * * *" & lt;
Char flag='1';
What S;
While (flag!='0')
{
S.M emu ();
cin> Flag;
If (flag=='1')
S.A dd ();
Else if (flag=='2')
S.S how ();
Else if (flag=='3')
S.L ength ();
Else if (flag=='0');
The else
{
cout <"Input error" & lt; flag=1;
}

}

}

CodePudding user response:

Have a memory leak, the operator=, followed by the operator should return references
  • Related