Object method does not respond
I am studying the creation of classes in C + +. When executing the code below the method LEN class String2 not work. Someone can tell me why?
code ----------------------------------------------------------------------------------------------
#include <iostream>
#include <string.h>
using namespace std;
const int MAX = 80;
class string2
{
private:
char str[MAX];
public:
string2() {str[0]='\0';}
string2(char s[]) {strcpy_s(str,s);}
string2(char ch, int n);
int len() const {return strlen(str);}
void print() const {cout << str;}
};
string2::string2(char ch, int n)
{
int ii;
for(int i=0;i<n;i++)
{
str[i] = ch;
ii = i;
}
str[ii] = '\0';
}
void main()
{
string2 s1("Feliz Aniversario! "),
s2('=',18),
s3 = "Estamos na Primavera. ",
s4,s5;
cout << "\nS1 = "; s1.print();
cout << "\nS2 = "; s2.print();
cout << "\nS3 = "; s3.print();
s4 = "O Mundo gira!";
cout << "\nS4 = "; s4.print();
s5 = s4;
cout << "\nS5 = "; s5.print();
cout << "\nTamanho de S1: "; s1.len();
cout << "\nTamanho de S2: "; s2.len();
cout << "\nTamanho de S3: "; s3.len();
cout << "\nTamanho de S4: "; s4.len();
cout << "\nTamanho de S5: "; s5.len();
}
Re: Object method does not respond
For starters, you need to indent your code properly, e.g.,
Code:
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 80;
class string2
{
private:
char str[MAX];
public:
string2() {str[0]='\0';}
string2(char s[]) {strcpy_s(str,s);}
string2(char ch, int n);
int len() const {return strlen(str);}
void print() const {cout << str;}
};
string2::string2(char ch, int n)
{
int ii;
for(int i=0;i<n;i++)
{
str[i] = ch;
ii = i;
}
str[ii] = '\0';
}
int main()
{
string2 s1("Feliz Aniversario! "),
s2('=', 18),
s3 = "Estamos na Primavera. ",
s4, s5;
cout << "\nS1 = "; s1.print();
cout << "\nS2 = "; s2.print();
cout << "\nS3 = "; s3.print();
s4 = "O Mundo gira!";
cout << "\nS4 = "; s4.print();
s5 = s4;
cout << "\nS5 = "; s5.print();
cout << "\nTamanho de S1: "; s1.len();
cout << "\nTamanho de S2: "; s2.len();
cout << "\nTamanho de S3: "; s3.len();
cout << "\nTamanho de S4: "; s4.len();
cout << "\nTamanho de S5: "; s5.len();
}
I took the liberty of changing <string.h> to the C++ version: <cstring>, and void main to int main.
Next, how does it not work? Have you checked to be sure that the member array of char really is a null terminated string?
Re: Object method does not respond
This code is a bit odd:
Code:
string2::string2(char ch, int n)
{
int ii;
for(int i=0;i<n;i++)
{
str[i] = ch;
ii = i;
}
str[ii] = '\0';
}
Why do you need the ii variable? That code is equivalent to:
Code:
string2::string2(char ch, int n)
{
for(int i=0;i<n;i++)
{
str[i] = ch;
}
str[n-1] = '\0';
}
Edit: Strictly speaking, the second code sample is only equivalent to the first when the loop executes at least once, i.e. when n>0. Otherwise, if the loop does not execute at all, ii is uninitialized so the first code sample would use an uninitialized variable.
Re: Object method does not respond
Regardless of whether string2::len() does what it's supposed to, which I didn't check, this would never output the string length:
Code:
cout << "\nTamanho de S1: "; s1.len();
what you actually want probably rather is this:
Code:
cout << "\nTamanho de S1: " << s1.len();
In contrast, this does what you expect it to do:
Code:
cout << "\nS1 = "; s1.print();
This may seem paradox compared to the above, but it only "works" because string2::print() does the printing entirely on its own inside its implementation; the cout at the beginning of this line has nothing to do with that.
Re: Object method does not respond
You're right, missed the << operator. Thank you for your help.