How to create from structure a class

Hello. How to create from structure a class?
There is a ready code with class usage.Ide wxDev-C++.
headerfile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//golfobj.h
#ifndef GOOLF_H_
#define GOLF_H_
using namespace std;
const int Len = 40;
class golf
{
  private:
    char fullname[Len];
    int handicap;
  public:
   //golf();
   golf(const char* name,int hc);
   int setgolf();
   ~golf();
   void fhandicap(int hc);
   void showgolf() const;
};
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "golfobj.h"
/*golf::golf()
{
    fullname[0] = '\0';
    handicap = 0;
}*/

golf::golf(const char * name,int hc)
{
    strncpy(fullname,name,Len-1);
    fullname[Len-1] = '\0';
    handicap = hc;
}

golf::~golf(){}

int golf::setgolf()
{
    /* golf::golf(const char * name,int hc)
       {
        strncpy(fullname,name,Len-1);
        fullname[Len-1] = '\0';
        handicap = hc;
       }*/
    int io,hnp;
    char fullname[40];
    cout<<"What is your name and your handicap:\n";
    if(cin.get(fullname,Len))
     io = 1;
      else
       io = 0;
       cin.get();
       cin>>hnp;
       golf(fullname,hnp);
       cin.get();
       return io;                //I DO NOT UNDERSTAND, WHAT EXACTLY IS RETURNED BY FUNCTION?
       //return *this;  
}

void golf::fhandicap(int hc)
{
    handicap = hc;
}

void golf::showgolf() const
{  
    cout<<"Name: "<<fullname<<endl;
    cout<<"Handicap: "<<handicap<<endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <cstdlib>
#include <iostream>
#include "golfobj.h"
using namespace std;
int main(int argc, char *argv[])
{
   golf players[3];/* =  {
                        golf("Sara",15),
                        golf("Kristy",30),
                        golf("Bob",17)
                       };//massiv class golf
                    */  
    int i;
    char imia;
    int hnd;
    for(i=0;i<3;i++)
     {
       cout<<players[i].setgolf();
       players[i].showgolf();
       cout<<endl;
       cout<<"Please enter a new handicap:";
       int hnd = 0;
       cin.get();
       cin>>hnd;
       cout<<endl;
       players[i].fhandicap(hnd);
     }
     for(i=0;i<3;i++)
     {
       players[i].showgolf();
       cout<<endl;
     }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


In advance many thanks for the help!
Sorry,up is - my problem version!In Comments - the second working version.

Version "struct":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//golf.h
#ifndef GOOLF_H_
#define GOLF_H_
using namespace std;
const int Len = 40;
struct golf
{
    char fullname[Len];
    int handicap;
};
void setgolf(golf & g,const char* name,int hc);
//int setgolf(golf & g);   //interaktivnaya versia
void handicap(golf & g,int hc);
void showgolf(const golf & g);
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//golf.cpp
#include <iostream>
#include <string>
#include "golf.h"
void setgolf(golf & g,const char* name,int hc)
{
    strncpy(g.fullname,name,Len-1);
    g.fullname[Len-1] = '\0';
    g.handicap = hc;
}

/*int setgolf(golf & g) /// interaktivnaya versia
{
    int io,hnp;
    cout<<"Vvedite Vashe imya i handicap:\n";
    if(cin.get(g.fullname,Len))
     io = 1;
      else
       io = 0;
       cin.get();
       cin>>hnp;
       g.handicap = hnp;
       return io;
}*/
void handicap(golf & g,int hc)
{
    g.handicap = hc;
}
void showgolf(const golf & g)
{  
    cout<<"Name: "<<g.fullname<<endl;
    cout<<"Handicap: "<<g.handicap<<endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdlib>
#include <iostream>
#include "golf.h"
using namespace std;

int main(int argc, char *argv[])
{
    golf players[3];//massiv struct golf
    int i;
    char imya[Len];
    int hnd = 0;
    for(i=0;i<3;i++)
     {
       //cout<<"vozvrashaemoe znachenie(0 or 1): "<<setgolf(players[i])<<endl; //interaktivnaya versia
       cout<<"Vvedite Vashe imya i handicap:\n";
       cin.get(imya,Len);
       cin>>hnd;
       setgolf(players[i],imya,hnd);
       showgolf(players[i]);
       cout<<"Vvedite novoe znachenie handicap:\n";
       cin>>hnd;
       cin.get();
       handicap(players[i],hnd);
       showgolf(players[i]);
       cout<<endl;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Last edited on
I really didn't understand your question but I did notice a mistake in your include guard:

1
2
3
//golf.h
#ifndef GOOLF_H_
#define GOLF_H_ 


That won't work, I think you probably meant this:

1
2
3
//golf.h
#ifndef GOLF_H_
#define GOLF_H_ 
How can I convert the code from the "Version "struct" in the code contains and uses class and objects. Your variant.
Thank you for finding bugs "GOOLF",but anyway many bugs.
Last edited on
In C++, class and struct are the same thing anyway; the only difference is that structs default to public accessibility and classes to private. I assume you are probably meaning C-style structures though, in which case you would just need to re-create all the methods to take a pointer to the the structure and operate on that instead of "this".
Okay I think I see what you're asking. What you need to do is declare your functions in the struct (or class) instead of outside. Then you don't need to pass your struct into the functions because the functions are inside the struct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//golf.h
#ifndef GOOLF_H_
#define GOLF_H_

//using namespace std; // please don't use this in header files

const int Len = 40;

struct golf
{
    char fullname[Len];
    int handicap;

    void set(const char* name,int hc);
    void handicap(int hc);
    void show();
};
#endif 

//golf.cpp
#include <iostream>
#include <string>
#include "golf.h"
void golf::set(const char* name,int hc)
{
    strncpy(fullname,name,Len-1);
    fullname[Len-1] = '\0';
    handicap = hc;
}

void golf::handicap(int hc)
{
    handicap = hc;
}

void golf::show()
{  
    cout<<"Name: "<<fullname<<endl;
    cout<<"Handicap: "<<handicap<<endl;
}


Each member function of struct golf has access to its member variable directly.
I need a simple implementation of both methods structures. One I know as to create. Another is not present.
Prompt, how to me to make implementation?
p.s. How to me to create a method of struct on similarity of function
1
2
3
4
5
6
7
8
9
10
11
12
13
/*int setgolf(golf & g) /// interaktiv version
{
    int io,hnp;
    cout<<"What is your name and your handicap:\n";
    if(cin.get(g.fullname,Len))
     io = 1;
      else
       io = 0;
       cin.get();
       cin>>hnp;
       g.handicap = hnp;
       return io;
}*/
?

Sorry,I'am very bad speak English!
English is not causing the problem... the question is not clear as to what do you want to do.

It may be better to use google translator to fame your question in Russian and then paste the translated english version.
Topic archived. No new replies allowed.