新手!!求大神帮忙看下C++ class的问题
题目:(英文授课的,没翻译,不好意思)Requirements
You must declare a class named String. It should contain (or wrap) a pointer that points to a character array. You do not need to maintain a minimum size on this array. You may include
other member variables if you choose. You must complete the following functions belonging to the
String class that you will declare in the given file with your main:
int getLength(): Receives no input and returns the length of the contained string.
char getFirst(): Receives no input and returns the 1st
character of the contained string.
char getLast(): Receives no input and returns the last character of the contained string.
void printString(): Receives no input and returns void. It merely prints the contained
string to the cout console.
void concat(char* inString): Receives a pointer to a character array as input and
returns nothing. It appends the inputted string to the contained string.
String(char* inString): A constructor which receives a pointer to a character array as input.
输出例子:
Sample output
Note: user input is the name.
What's your full name? John Jacob Jingleheimer Schmidt
You entered:
John Jacob Jingleheimer Schmidt
Your name is 31 characters long.
Your name starts with the letter "J"
Your name ends with the letter "t"
You could name your kid:
John Jacob Jingleheimer Schmidt the Second
Press any key to continue . . .
我的代码:
#include <iostream>
#include <cstring>
class String
{
public:
char* cString;
int getLength();
char getFirst();
char getLast();
void printString();
void concat(char* inString);
String(char* inString);
};
int main()
{
const int STRING_SIZE = 1000;
char *cString = new char[STRING_SIZE];
std::cout << "What's your full name? ";
std::cin.getline(cString, STRING_SIZE);
String yourName(cString);
std::cout << "You entered:" << std::endl << "\t";
yourName.printString();
std::cout << std::endl;
std::cout << "Your name is " << yourName.getLength() <<
" characters long." << std::endl;
std::cout << "Your name starts with the letter \"" <<
yourName.getFirst() << "\"" << std::endl;
std::cout << "Your name ends with the letter \"" <<
yourName.getLast() << "\"" << std::endl;
std::cout << "You could name your kid: " << std::endl << "\t";
yourName.concat(" the Second");
yourName.printString();
std::cout << std::endl;
return 0;
}
int String::getLength()
{
return std::strlen(cString);
}
char String::getFirst()
{
return cString[0];
}
char String::getLast()
{
int index=0;
while (cString[index] != '\0') //run the loop when it dosen't come to '/0'
{
index++;
} //to get the length of inString
return cString[index-1]; //output the last letter of the string
}
void String::printString()
{
for (int n=0;cString[n]!='\0';n++)
{
std::cout << cString[n];
} //use the for loop to print the string on the screen
return;
}
void String::concat(char* inString)
{
int numberofcstring=0;
int n=0;
while (cString[numberofcstring] != '\0') //run the loop when it dosen't come to '/0'
{
numberofcstring++;
} //get the length of string a
while (inString[n]!='\0')
{
cString[n+numberofcstring]=inString[n];
n=n+1; //to append string b to the end of string a
}
cString[n+numberofcstring]='\0'; //to ensure that the string a ends properly
return;
}
请教各位大神:在Class定义里老师要我加入这个String(char* inString);但是我不知道这个到底有什么用,它需要在程序末尾写coding的内容吗?(运行程序说这个未解决,fail)
当我删除这个String(char* inString),运行就报错:在main里面String yourName(cString);的那个cString就被说没有转换为string。。。
请大神解答,并帮忙改下程序,谢谢!!!