tags:

views:

168

answers:

3

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);
}
+4  A: 

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 :)

Goz
@Goz, how are you going to initialize protected members?
Kirill V. Lyadvinsky
And placing `using namespace std` in a header is a bad practice.
Kirill V. Lyadvinsky
With a constructor is the usual method. http://www.fredosaurus.com/notes-cpp/oop-condestructors/constructors.html
Goz
There are two problems with player.h: 1. Never import a namespace in a header file. This might conflict with the coding style followed by certain users of the header. 2. It is bad to include header files in a header file. This might lea d to a longer compilation time.
Vijay Mathew
and TBH ... using "using namespace" at all is pretty poor practice, IMO. I personally always do std::string, std::vector etc etc
Goz
I know that constructor could help, but I can't see it in your sample.
Kirill V. Lyadvinsky
it doesn't actually NEED one in the code he posted though ... I just seperated it into different files.@Vijay .. i have to wholeheartedly disagree with what you say. It is far more problematic to force everybody to know what #includes must be in the CPP file before they include your header. Afterall if you want to include that header you HAVE to include <string> somewhere ... so why not just put it in the header and save the nightmare.
Goz
Nitpick, but in your include guards, you should not start your token with two underscores. Identifiers beginning with two underscores are reserved for the compiler and the standard library.
Tyler McHenry
@Vijay (2): I strongly disagree, and I'm not the only one. Header files should stand alone, and include all their dependencies, rather than requiring the user to first include a wish list of "stuff needed to make this header work". If this leads to longer compile times, that's worth paying. It probably won't, since serious compilers will use an equivalent of `#pragma once` on system headers, and in any case the OP's compiler supports precompiled headers. Even if it might, it's a premature optimisation at this stage of the project.
Steve Jessop
@onebyone: Its probably worth noting that if the header doesn't include the headers that it needs then people will just cut and paste headers into the top of their CPP files to make sure it compiles and THAT will lead to slower compile times. I have seen it many times before, alas :(
Goz
@onebyone: I agree. However, I'd like to point out that in my experience precompiled headers don't actually speed things up much, if any. I think the main issue with them is that they optimize the wrong thing. The time it takes to load the files from disk is far greater than the time it takes to compile them, so it doesn't really help much to be eliminating that much smaller compile time and leaving the much larger file loading time untouched.
T.E.D.
+4  A: 

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.

Steve Jessop
A: 

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.

Chaoz
I'm not a believer in the strict private school of thought. If you have to provide more or less direct access to a member variable, it just wastes coding time and complicates things to hide it away, and then write set_ and get_ routines to make it public again
T.E.D.