方法:
1.打开VS2019建立一个空项目
2.里面都是空的,在对应目录下新建自己的头文件、源文件
3.编辑自己新建的文件
4.运行
代码(6-3对象数组应用举例的代码):
//main.cpp
#include<iostream>
#include"Point.h"
using namespace std;
int main()
{
cout << "Entering main..." << endl;
Point a[2];
for (int i = 0; i < 2; i++)
a[i].move(i + 10, i + 20);
cout << "Exiting main..." << endl;
return 0;
}
//Point.h
#ifndef _POINT_H
#define _POINT_H
class Point {
public:
Point();
Point(int x, int y);
~Point();
void move(int newX, int newY);
int getX() const { return x; }
int getY() const { return y; }
static void showCount(); //静态成员函数
private:
int x, y; //私有成员
};
#endif//_POINT_H
//Point.cpp
#include<iostream>
#include"Point.h"
using namespace std;
Point::Point() :x(0), y(0) {
cout << "Default Constructor called." << endl;
}
Point::Point(int x, int y) : x(x), y(y) {
cout << "Constructor called." << endl;
}
Point::~Point() {
cout << "Destructor called." << endl;
}