| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 469 人关注过本帖
标题:新手!!求大神帮忙看下C++ class的问题
取消只看楼主 加入收藏
mistakexx
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-12-7
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
新手!!求大神帮忙看下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。。。

请大神解答,并帮忙改下程序,谢谢!!!
搜索更多相关主题的帖子: character belonging following maintain contain 
2013-12-07 18:40
快速回复:新手!!求大神帮忙看下C++ class的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016936 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved