Open VS2015, new vs Win32 Project, the previous steps are simple, no longer elaborated
Start directly below:
Create a new VC + + Win32 program,
Add a. cpp file to the source file, generally put the main function
#include <iostream>
#include <locale>
#include "human.h"
using namespace std;
int main()
{
Human Tom;
Tom.A(160);
people zhangsan;
zhangsan.B(160);
//cout<<Tom.printf();
}
Add the. h file (human.h) in the header file, typically a defined class
#include <iostream>
#include <locale>
#include "human.h"
using namespace std;
int main()
{
Human Tom;
Tom.A(160);
people zhangsan;
zhangsan.B(160);
//cout<<Tom.printf();
}
Constructor for the. cpp file drop class in the header file
#include "human.cpp"
#include <iostream>
#include "human.h"
using namespace std;
void Human::A(int w)
{
cout << w << endl;
}
void people::B(int w)
{
w -= 100;
cout << w << endl;
}
Then the main function under the source file to add the class declaration header file, the header file. CPP also add the header file for the class declaration
I note: The class defined in the header file, in the same name as the C file to make a declaration, the constructor and destructor of the class will either display the definition, but also to display the declaration, even if there is no execution of the contents of these functions, empty also to write, otherwise it will compile pass, either the class constructor and destructor are not written, Class by default produces constructors and destructors that do not execute any program
Anyway: for destructors and constructors within a class, as long as they are defined, you must declare that the function is empty and can
To add a multi-file system (i.e., multiple headers and CPP files) in VS, use VS to generate classes,
1. Project Right-click Add-on class
After entering the class name, h files and CPP files are automatically populated, and the generated class's constructors, destructors, including header files all have
Generated code:
H file
#pragma once
class Clock
{
public:
Clock();
~Clock();
};
CPP file
#include "Clock.h"
Clock::Clock()
{
}
Clock::~Clock()
{
}
It's all empty, and underneath it, fill in our function.
2. Put our functions in the class
h file after adding functions
#pragma once
#include <iostream>
using namespace std;
class Clock
{
public:
Clock ();
void SetTime (int NewH, int NewM, int NewS); // All three parameters have function prototype scope
void ShowTime ();
~ Clock ();
private:
int Hour, Minute, Second;
};
CPP file after adding a function:
#include "Clock.h"
Clock::Clock()
{
Hour = 0;
Minute = 0;
Second = 0;
}
void Clock::SetTime(int NewH, int NewM, int NewS)
{
Hour = NewH;
Minute = NewM;
Second = NewS;
}
void Clock::ShowTime()
{
cout << Hour << ":" << Minute << ":" << Second << endl;
}
Clock::~Clock()
{
}
3. Add our class containing header files in main
#include "Clock.h" // Header file
Clock g_Clock;
int main () // Main function
{
cout << "File-scoped clock-like object:" << endl;
// Refer to an object with file scope:
g_Clock.ShowTime ();
g_Clock.SetTime (10, 20, 30);
Clock myClock (g_Clock); // Declares an object myClock with block scope, and initializes myClock with g_Clock through the default copy constructor
cout << "Block-scoped clock-like object:" << endl;
myClock.ShowTime (); // Refer to an object with block scope
return 0;
}
Operation Result:
over!!!
Related articles:
New server complete WWW environment process
Zend Studio New Project related issues