1

嗨,我对 c++ 非常陌生(我是 ac# 用户,想学习 c++)我有一个 Windows 窗体,上面有一张狗的图片,上面有标签和 2 个按钮现在我想创建一个公共类“C_dog " 与 ff:

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 实例?

4

1 回答 1

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;
于 2013-08-15T06:35:47.643 回答