A brief introduction to C++ (1): Object-oriented

The compilers are all vs2013 under windows

When in the same file: first acquaintance

Create file main.cpp

# include<iostream>
//class people   //定义一个类
//{
//public:  // 公有
//    void sayhello(){
//		std::cout << "Hello World\n";
//}
//};
int main(int argc, const char * argv[])
{
	people *p = new people();
	p->sayhello();             //通过指针p访问到成员方法
	delete p;   //释放内存空间
	system("pause");
	return 0;
}

Remove the comments from the above file and you can successfully run and output Hello World.

After ok, we continue to create a c++ class file named people.

Define a class in people.h:

#pragma once
class people
{
public:
	void sayhello();
};

Implement this class in people.cpp:

# include<iostream>
#include "people.h"
using namespace std;

void people::sayhello()
{
	cout << "Hello World\n";
}
After the creation is complete, go back to the main.cpp file and add # include "people.h" above the file; so that we can access the header file people.h; because our class is in this file, we need to access it if we want to use it , save and run, you can get the same results.

Guess you like

Origin blog.csdn.net/samxiaoguai/article/details/80320709