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

初学者有关问题 关于一个类是另外一个类的元素的构造函数如何写

2013-02-17 
菜鸟问题 关于一个类是另外一个类的元素的构造函数怎么写1 #includeiostream2 #includestdio.h3 using

菜鸟问题 关于一个类是另外一个类的元素的构造函数怎么写
  1 #include<iostream>
  2 #include<stdio.h>
  3 using namespace std;
  4 
  5 typedef enum {A=1,B,C,D} SchoolName;
  6 typedef enum {male=1,female} SexType;
  7 typedef enum {run=1,jump} MatchType;
  8 class Component {   //school name  sex match score
  9     private:
 10         SchoolName school;
 11         string name;
 12         SexType sex;
 13         MatchType match;
 14         int score;
 15     public:
 16         Component(SchoolName s_school=D , SexTypes_sex=male ,MatchTypes_match=run, int s_score=0 , char * s_name="zs" )   //②还有这个地方本来想把默认的全搞成0值的不过编译的时候报错了,如 SchoolName s_school=0 ;①号问题在下面初学者有关问题 关于一个类是另外一个类的元素的构造函数如何写
 17         {
 18             school=s_school;
 19             sex=s_sex;
 20             name=s_name;
 21             match=s_match;
 22             score=s_score;
 23         }
 24 };


 29 class ComponentList {
 30     private:
 31         Component clist[128];
 32         int top; //下标
 33 /*      void init()
 34         {
 35             top=0;
 36             int i;
 37             for(i=0;i<128;i++){
 38                 clist[i].school=0;
 39                 clist[i].sex=0;
 40                 clist[i].match=0;
 41                 clist[i].score=0;
 42                 clist[i].name="NULL";
 43             }
 44         }
 45 */
 46     public:
 47 /*      ComponentList()    //我写的构造函数 编译的时候错了也知道为什么错了(component的成员是私有的),可是正确的 还是不会写

初学者有关问题 关于一个类是另外一个类的元素的构造函数如何写 
 48         {
 49             
 50             
 51             init();     正缺的构造函数怎么写啊ComponentList(){}
 52         }       
 53 */
 54         ComponentList & insert( const Component & s_component)
 55         {
 56             clist[top]=s_component;
 57             top++;
 58             return this;
 59         }
    
[解决办法]
②因为你的参数类型是SchoolName,它是一个枚举类型,那么你需要明确地指定该类型的其中一个值。从0转换到SchoolName虽然是可行的,但编译器不会自动进行这种隐式转换,这必须由你自己进行。虽然不被推荐,但你可以这么写:


void Foo(SchoolName s=(SchoolName)0){
   //......
}


①因为你的Component的默认构造函数(不带参数的那个)被屏蔽了,原因是你为Component定义了带参构造函数(具体见《深入探索C++对象模型》)。而你的List中定义了Component数组,这意味着你必须为Component提供默认构造函数,因为数组必须用它来初始化。
    所以,你只需要为Component提供一个不带参数的默认构造函数即可,如果你不想让外界访问该构造函数,则把它定义为私有的,并声明ComponentList为它的友元类。

class Component {
    friend class ComponentList;
private:
    Component(){ }

 // other codes
}

class ComponentList {
private:
    Component components[128];

    // ......
}

[解决办法]

#include<iostream>
#include<stdio.h>
using namespace std;

typedef enum {A=1,B,C,D} SchoolName;
typedef enum {male=1,female} SexType;
typedef enum {run=1,jump} MatchType;

class Component 
{
private:
SchoolName school;
string name;
SexType sex;
MatchType match;
int score;
public:
Component(SchoolName s_school=D , SexType s_sex=male ,MatchType s_match=run, int s_score=0 , char * s_name="zs");
~Component();
};

Component::Component( SchoolName s_school/*=D */, SexType s_sex/*=male */,MatchType s_match/*=run*/, int s_score/*=0 */, char * s_name/*="zs"*/ )
{
school=s_school;
sex=s_sex;
name=s_name;
match=s_match;
score=s_score;
}
//////
class ComponentList 
{

public:
ComponentList():top(0)
{
init();  
}       
ComponentList & insert( const Component & s_component)
{
clist[top]=s_component;
top++;
return *this;
}
private:
void init()
{
for(int i=0;i<128;i++)
{
clist[i]();


}
}
private:
Component clist[128];
int top; //下标
};


[解决办法]
 #include<iostream>
 #include<stdio.h>
 using namespace std;

 typedef enum {A=1,B,C,D} SchoolName;
 typedef enum {male=1,female} SexType;
 typedef enum {run=1,jump} MatchType;
 class Component {   //school name  sex match score
     private:
         SchoolName school;
         string name;
         SexType sex;
         MatchType match;
         int score;
     public:
         Component(SchoolName s_school=D , SexType s_sex=male ,MatchType s_match=run, int s_score=0 , char * s_name="zs" )   //②还有这个地方本来想把默认的全搞成0值的不过编译的时候报错了,如 SchoolName s_school=0 ;①号问题在下面
         {
             school=s_school;
             sex=s_sex;
             name=s_name;
             match=s_match;
             score=s_score;
         }
 };


 class ComponentList {
     private:
         Component clist[128];
         int top; //下标
/*       void init()
         {
             top=0;
             int i;
             for(i=0;i<128;i++){
                 clist[i].school=0;
                 clist[i].sex=0;
                 clist[i].match=0;
                 clist[i].score=0;
                 clist[i].name="NULL";
             }
         }
 */
        void init ();
     public:
        ComponentList(){init();};
 /*      ComponentList()    //我写的构造函数 编译的时候错了也知道为什么错了(component的成员是私有的),可是正确的 还是不会写
         {


             init();     正缺的构造函数怎么写啊ComponentList(){}


         }
 */
         ComponentList & insert( const Component & s_component)
         {
             clist[top]=s_component;
             top++;
             return *this;
         }
};

void ComponentList::init()
{
    top = 0;
    for(int i=0;i<128;++i)
        clist[i]=Component();
}
}


[解决办法]
噢,对了,如果你将Component的带参构造函数的参数全都赋予默认值,则那个空的构造函数可省略

热点排行