如何添加公共课?

时间:2013-08-15 06:23:45

标签: c++ winforms

你好我对c ++很新(我是一个c#用户,想要学习c ++) 我有一个窗口形式,上面有一张带有标签的狗的图片和2个按钮 现在我想用ff:

创建一个公共类“C_dog”
public name
public weight

和功能:

public static sayname()
{
 //when button1 is clicked
  label1.text="Hi my name is "+name;
}

public static sayweight()
{//when button2 is clicked
  label1.text="I weight "+weight+" pounds";
}

当我点击添加类时,提示我创建一个头文件,并且它已预先编码.'cpp'上的内容 如何声明新的C_dog实例?

1 个答案:

答案 0 :(得分:2)

/* C_dog.h */

public class C_dog 
{
 public:
     C_dog(std::string name, unsigned int weight);   // example for constructor
     ~C_dog();  // destructor

     // declare all members: weight, name etc.
     std::string m_name;
     unsigned int m_weight;
     void sayname();
     void sayweight();
}

/* C_dog.cpp */

#include "C_dog.h"

C_dog::C_dog(std::string name, unsigned int weight)   
{
    m_name = name;
    m_weight = weigth;
}

C_dog::~C_dog() 
{
}

C_dog::sayname()
{
   //when button1 is clicked
   label1.text="Hi my name is "+m_name;    // label1 has to be visible globally
}

C_dog::sayweight()
{
    //when button2 is clicked
    label1.text="I weight "+m_weight+" pounds"; // label1 has to be visible globally
}

/* get a new instance */

C_dog * charlie = new C_dog("charlie", 40);  // this is your new C_dog

/* if not needed any more, don't forget to send him to heaven: */

delete charlie ;
charlie = nullptr;