View Code

代码就是这样的简单。

无参时的创建。

 

View Code
#include "iostream"
#include "string"
using namespace std;

class Point
{
public:
    Point(int x, int y)
    {
        cout<<"Default constructor called."<<endl;
        cout<<x<<" "<<y<<endl;
    }
private:
    int x, y;

};
int main()
{
    Point *ptr1=new Point(1, 2);
    delete ptr1;
}

代码就是这样的简单。

有参时的创建。

 

View Code
#include "iostream"
#include "string"
using namespace std;

class Point
{
public:
    Point():x(0), y(0)
    {
        cout<<"Default constructor called."<<endl;
        cout<<x<<" "<<y<<endl;
    }
private:
    int x, y;

};
int main()
{
    Point *ptr1=new Point[10];                                                                ;
    delete[] ptr1;
}

这个例子是用来展示用new来创建

对象数组的。

相关文章:

  • 2021-05-29
  • 2021-08-27
  • 2022-12-23
  • 2021-10-31
  • 2021-06-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
相关资源
相似解决方案