首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

高分!语法及多文件有关问题

2012-02-08 
高分求助!!!!语法及多文件问题~C++中:1.对象是否支持 的直接赋值?Objectobject2object12.如何将整型

高分求助!!!!语法及多文件问题~
C++中:

1.对象是否支持 "= "的直接赋值?
        Object   object2   =   object1;
2.如何将整型转为string?string   是否可以+?
      int   n   =   7;
      string   s   =   n   +   "abcd ";     //不行?
3.同理,如何将整形转为char[]?

4.   按照国际习惯(虽然C++注释没有标准格式,但我想应该有个普遍承认的一些原则?),在定义类的.h文件的开头(或者其他什么地方?),是否该注释出该类的所有接口?    
    我是用readme.txt单独写出的

5.   有一个Date类,在其构造函数Date(){...}里,调用   bool   setDate()函数,以从 "命令行 "输入年\月\日,并判断是否符合格式(比如非闰年的2月是否超过了28天...)
      如果bool   setDate()返回false,那就要释放掉刚才的空间了,这里是不是应该在构造函数里 "显式 "调用析构函数~Date(){};?如何调用?
      因为我在构造函数里 "显式 "调用析构函数的时候报错.
      或者应该怎么写构造     析构     setDate()函数?
      (该问题50分!!!)

****************************************************
我对多文件中的#include机制不太明白,以计算一句话中单词数的程序为例:
 
    1.包含前两个函数声明的头文件:   words_counter.h   :
代码:
          1   /**********************************
          2     *                 words_counter.h                   *
          3     **********************************/
          4   #ifdef   _WORDS_COUNTER_H_
          5   #define   _WORDS_COUNTER_H_   1
          6
          7   #include   <iostream>
          8   #include   <cstring>
          9   #include   <string>
        10
        11   using   namespace   std;
        12
        13   bool   isLetter(const   char);
        14
        15   int   countWords(const   char*);
        16
        17   #endif
~


    2.判断一个字符是否字母的函数bool   isLetter(char)的实现文件   isLetter.cc:
代码:
          1   /************************************
          2     *                   isLetter.cc                             *
          3     ************************************/
          4   /**
          5     *   Judge   that   if   a   char   is   a   letter
          6     *   @author   Du   Chun
          7     *   @version   1.0
          8     *   @param   <character>
          9     *   @return   <bool>
        10     */
        11   #include   "words_counter.h "
        12
        13   bool   isLetter(char   character){


        14           return   ((character <= 'z ')&&(character> = 'a '))||((character <= 'Z ')&&(character> = 'a '));
        15   }


    3.   计算一个句子中的单词数的函数int   countWords(char*)的实现文件   countWords.cc:
代码:
          1   /*********************************
          2     *             countWords.cc                         *
          3     *********************************/
          4   /**
          5     *   Count   numbers   of   words   in   a   string
          6     *   @author   Du   Chun
          7     *   @version   1.0
          8     *   @param   <string   sentence>
          9     *   @return   <int   count>
        10     */
        11   #include   <cstring>
        12   #include   "words_counter.h "
        13   bool   isLetter(char);
        14
        15   int   countWords   (const   char*   sentence){
        16           int   count   =   0;
        17           int   size   =   strlen(sentence);//size   of   sentence
        18
        19           for(int   i   =   0;i <size;++i){
        20                   if(isLetter(sentence[i])&&!(isLetter(sentence[i+1])))
        21                           ++count;
        22           }
        23           return   count;
        24   }


      4.main()所在文件main.cc:
代码:
          1   /**
          2     *   TODO   count   numbers   of   words   in   a   string
          3     *   @author   Du   Chun
          4     *   @version   1.0
          5     */
          6   #include   <iostream>
          7   #include   "words_counter.h "
          8   using   namespace   std;
          9   int   countWords(const   char*);
        10
        11   /**
        12     *   main
        13     */


        14   int   main(){
        15           char   sentence[100];
        16           int   wordsNum;//number   of   words
        17
        18           /*Menu   interface*/
        19           cout < < "********************A   Words   Counter   Demo****************** "
        20                   < <endl
        21                   < < "Please   input   a   sentence: " < <endl;
        22
        23           /*Get   a   sentence   from   console*/
        24           cin.getline(sentence,100);
        25
        26           /*Count   number   of   words   in   the   sentence*/
        27           wordsNum   =   countWords(sentence);
        28
        29           /*output*/
        30           cout < < "This   sentence   has   " < <wordsNum;
        31           if(wordsNum==1)   cout < < "   word. " < <endl;
        32           else   cout < < "   words. " < <endl;
        33
        34           return   0;
        35   }


按以上方法编译成功;运行成功.

问题:
1.按我的理解,以下行是不需要的,可是没有的话编译不通过:

    isLetter.cc文件里的第11行--因为不需要用到word_counter.h里的任何声明;
    countWords.cc文件里的第13行--因为已经包含了word_counter.h,而该头文件里已经有isLetter()的声明了;(不加会报错,说没有声明countWords())
    main.cc文件里的第9行--因为已经包含了word_counter.h,而该头文件里已经有countWords()的声明了;(不加会报错,说没有声明countWords())

2.   文件的#include机制是什么样的?看下面两个文件:
    file1:
    #include   <iostream> ( <string> ....)
    {
      ....
    }
***********************
    file2:
    #include   <file1>
    {
      //要用到 <iostream> ( <string> ....)的相关函数
    }

  按我的理解,file2   中不需要#include   <iostream> ( <string> ....)了.
  这个应该适用于所有.h     or     .cc文件.
  是不是?

[解决办法]
换本C++Primer来学C++吧。你基础知识差太多了,让我不知道怎么回答可以让你懂,还是看书吧。
[解决办法]
1.对象是否支持 "= "的直接赋值?
Object object2 = object1;
==============
你这用的是构造函数,不是operator =赋值操作符
对象支持operator =,编译器会提供默认的赋值操作符,执行按成员赋值行为,
一般含有指针数据成员的时候,重载operator =
[解决办法]
2.如何将整型转为string?string 是否可以+?


int n = 7;
string s = n + "abcd "; //不行?
====================
itoa()
sprintf()都可以
C++的string是可以支持operator +的
string s = “abc”;
s = s + “def”;
此时s的值为“abcdef”
[解决办法]
1 可以
2 int i=1234; ostringstream os; os < <i; string str(os); cout < <str;
3 int i=1234; char tmp[20]; sprintf(tmp, "%d ", i); //converting
4 h 文件中声明即可, 不要求必须在文件头部分(h文件一般仅包含声明)。
5 析构函数一般不显式调用。
[解决办法]
#include 声明,将包含的文件在 indluce 位置展开,
那么在 include 的文件中声明的 变量/类型/方法 就可以在 当前文件中使用了 ~~
[解决办法]
问题1:effective C++ item11
问题4:类的定义本身就是描述类的接口的。代码要自注释。
问题5:构造还没有完成,对象就还不存在,调用析构函数纯属胡闹。
[解决办法]
那就是说,即时调用了构造函数,但构造函数没有对成员变量进行任何赋值,那该对象就不占用空间??
=======================================================================================
当然占用了,
对象建立分成:
分配内存=》调用构造函数=》对象
[解决办法]
"taodm ":构造还没有完成,对象就还不存在,调用析构函数纯属胡闹。
===============================
taodm说的对,构造还没完成,对象还没建立,怎么去调用一个对象的析构函数,至于构造函数执行完成之前发生的异常,内存释放由系统来作,不会发生内存泄漏的
[解决办法]
1.对象是否支持 "= "的直接赋值?
Object object2 = object1;
答:首先,你写的这个是“初始化”,而非“赋值”,赋值应该是这样的:
Object object2;
Object object1;
object2 = object1;//赋值

然后,如果想让对象支持赋值,需要定义operator=,当然了,编译器一般情况下会为你自动合成一个,但是这个不一定符合你的要求。
[解决办法]
"不要在构造函数里抛异常。
如果bool setDate()返回false,那就要释放掉刚才的空间了,这里应该在构造函数里 "显式 "调用释放内存的函数,比如delete。 "
=============================
不是动态分配的也要用 Delete ?

热点排行