Rajasekar
Rajasekar

Reputation: 18938

How can I separate headers, classes and main functions in C++?

Please help me in separating the classes, headers and main() in the following program. I tried my best but there is problem.

#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);
}

Upvotes: 0

Views: 3358

Answers (3)

Charles
Charles

Reputation: 2661

If you want to separate your classes you should use create two files; .h & .cpp.

In the header file you place your definitions and declarations, and in the CPP file you implement your methods.

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;
}

Edit:

Class member variables should never be public; Write a set method if you have a need to modify a member variable.

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279225

A typical separation into files in C++ would be as follows:

// 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;
}

There are a few other things that could be tidied up: you might want to put stdafx.h back in, and the strings in player don't need to be public.

Note also that I've not put "using namespace std" in the header file. Many people prefer not to do that, at least not at global scope, but if your header does it then that's forced on them if they use your class. It doesn't much matter either way when the files are just for you, but it matters quite a lot in a large project if headers are unexpectedly "using" things. It could cause someone problems where they don't realise that all the functions in std are visible, and call one by accident trying to call something else.

I've deliberately used all three options in the three files: using namespace std;, using std::system;, or specifying the namespace every time.

Upvotes: 4

Goz
Goz

Reputation: 62323

Do you mean how you would seperate that into header and cpp files?

If so in Player.h you'd do the following:

#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

in player.cpp:

#include "stdafx.h"
#include "Player.h"
#include<iostream>

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

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

And then in main.cpp you'd do the following:

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

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

This would be the ideal way to split out everything into seperate header and cpp files :)

Upvotes: 4

Related Questions