如何在C++中分离页眉、类和主函数?

如何在C++中分离页眉、类和主函数?,c++,C++,请帮助我在下面的程序中分离类、头和主。我尽力了,但还是有问题 #include "stdafx.h" #include<iostream> #include<string> using namespace std; class player { public: string name; string type; void getdata() { cout<<"Enter the name of the Play

请帮助我在下面的程序中分离类、头和主。我尽力了,但还是有问题

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

class player
{
public:
    string name;
    string type;
    void getdata()
    {
        cout<<"Enter the name of the Player : "<<endl;
        cin>>name;
        cout<<"Enter the Game he play : "<<endl;
        cin>>type;
    }  
    void display()
    {
        cout<<"The name of the Player is : "<<name<<endl;
        cout<<"The game he will play is : "<<type<<endl;
    }
};

int main()
{
    player sachin;
    sachin.getdata();
    sachin.display();
    system("pause");
    return(0);
}

您的意思是如何将其分为头文件和cpp文件

如果在Player.h中是这样,您将执行以下操作:

#ifndef __PLAYER_H_
#define __PLAYER_H_

#include<string>
using namespace std;

class player
{
protected: // could be private
    string name;
    string type;
public:
    void getdata();
    void display();
};

#endif
#include "stdafx.h"
#include "player.h"

int main()
{
    player sachin;
    sachin.getdata();
    sachin.display();
    system("pause");
    return(0);
}

这是将所有事物分割成单独的头文件和CPP文件的理想方法:

< P> C++中典型的文件分离方式如下:

// myapp.cpp
// ---------

#include "player.h"
#include <cstdlib>
using std::system;

int main()
{
    player sachin;
    sachin.getdata();
    sachin.display();
    system("pause");
    return(0);
}

// player.h
// --------

// or #pragma once, since you're on MS.
#ifndef player_h
#define player_h

#include<string>

class player
{
public:
    std::string name;
    std::string type;
    void getdata();
    void display();
};
#endif

// player.cpp
// ----------

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

void player::display()
{
    cout<<"The name of the Player is : "<<name<<endl;
    cout<<"The game he will play is : "<<type<<endl;
}
void player::getdata()
{
    cout<<"Enter the name of the Player : "<<endl;
    cin>>name;
    cout<<"Enter the Game he play : "<<endl;
    cin>>type;
}
还有其他一些事情可以整理:您可能希望将stdafx.h放回,并且播放器中的字符串不需要公开

还要注意,我没有在头文件中使用名称空间std。许多人不喜欢这样做,至少在全局范围内是这样,但如果你的头这样做了,那么如果他们使用你的类,他们就会被迫这样做。当文件只适合您时,这两种方式都没有多大关系,但在大型项目中,如果头文件意外地使用了某些东西,这将非常重要。它可能会导致一些人出现问题,他们没有意识到std中的所有函数都是可见的,并且在尝试调用其他函数时意外地调用了一个函数


我特意在这三个文件中使用了所有三个选项:使用名称空间std;,使用std::system;,或者每次都指定名称空间。

如果要分隔类,应使用“创建两个文件”。h&cpp

在头文件中放置定义和声明,在CPP文件中实现方法

Player.h

#ifndef __PLAYER_H_
#define __PLAYER_H_

#include <string>

class Player
{
public:
    Player();
    ~Player();

    // Methods
    void GetData();
    void Display();

private:
    std::string Name;
    std::string Type;
}

#endif
Player.cpp

#include "Player.h"

Player::Player(): Name(""), 
                  Type("")
{
}

Player::~Player(){}

void Player::GetData()
{
    std::cout << "Enter the name of the Player : " << std::endl;
    std::cin >> name;
    std::cout << "Enter the Game he play : " << std::endl;
    std::cin >> type;
}  

void Player::Display()
{
    std::cout <<"The name of the Player is : " << name << std::endl;
    std::cout <<"The game he will play is : " << type << std::endl;
}
编辑:


类成员变量永远不应该是公共的;如果需要修改成员变量,请编写一个set方法。

能否提供有关该问题的详细信息。@chrisF,先生,我需要将程序分别划分为类和main。在cpp文件中,方法声明应该是独立的,定义应该是独立的。帮我,我只是想learn@Goz,如何初始化受保护的成员?在标头中使用命名空间std是一种不好的做法。通常的方法是使用构造函数。玩家有两个问题。h:1。永远不要在头文件中导入命名空间。这可能与头的某些用户遵循的编码样式冲突。2.在头文件中包含头文件是不好的。这可能需要更长的编译时间。吹毛求疵,但在include-guard中,您不应该以两个下划线开始标记。以两个下划线开头的标识符是为编译器和标准库保留的。我不相信严格的私人思想流派。如果您必须或多或少地提供对成员变量的直接访问,那么将其隐藏起来只会浪费编码时间并使事情复杂化,然后编写set_uu和get_uuu例程以使其再次公开
#include "Player.h"

Player::Player(): Name(""), 
                  Type("")
{
}

Player::~Player(){}

void Player::GetData()
{
    std::cout << "Enter the name of the Player : " << std::endl;
    std::cin >> name;
    std::cout << "Enter the Game he play : " << std::endl;
    std::cin >> type;
}  

void Player::Display()
{
    std::cout <<"The name of the Player is : " << name << std::endl;
    std::cout <<"The game he will play is : " << type << std::endl;
}