public to private values

Hi all:
I need to change a public value from a class to private with a get method. I´ve tried it but I don´t know exactly what Im doing wrong.
Here the standard code of the Header class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  class Token {
public:
char kind; // Die Kategorien der Eingabesymbole
double value; // Bei Zahlenkategorie: der Wert
void print() const {
if( kind == '9' ) cout << value;
else cout << kind;
}
// Konstruktor, erstelle ein Token aus einem char:
Token( const char& ch ) : kind{ch}, value{0.0} { /* mache nichts sonst */ }
// Konstruktor, erstelle ein Zahl-Token aus einem double:
Token( const double& val ) : kind{'9'}, value{val} { /* mache nichts sonst */ }
// Standardkonstruktor, erstelle "das Standardtoken" ohne Vorgabe:
Token( ) : Token{0.0} { /* mache nichts sonst */ }
};
// Anwendung: 


Here is what I have done in the header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef TOKEN_H
#define TOKEN_H
#include <iostream>
using namespace std;
#include <string>


class Token {
public:
    char getkind(char k);
    double getvalue(double d);
    void print() const;
    Token( );
    Token(const char& );
    Token(const double& );
private:
    char kind; // Die Kategorien der Eingabesymbole
    double value; // Bei Zahlenkategorie: der Wert

};

#endif // TOKEN_H 

And here the cpp part:
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
#include "token.h"
#include <iostream>

Token::Token(const char& ch ) : kind{ ch }, value{ 0.0 }
{}
Token::Token(const double& val ) : kind{ '9' }, value{ val }
{}
// Standardkonstruktor
Token::Token( ) : Token{0.0}
{}

void Token::print()const
{
    if( kind == '9' ) cout << value;
    else cout << kind;
}
char Token::getkind(char k)
{
    kind=k;
    return kind;
}
double Token::getvalue(double d)
{
    value=d;
    return value;
}


Now I need to implement that in a function. Here is how the function was when "kind" and "value" were public:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double primary( Token_stream& ts ) // Zahlen und Klammern behandeln
{
Token t3{ ts.get( ) };
switch( t3.kind )
{
case '9': return t3.value;
case '(': {
double d3{ expression( ts ) };
if( ts.get().kind != ')' ) error( "keine rechte Klammer" );
return d3;
}
default: error( "kein Primary" );
}
}

And here are the changes I did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double primary( Token_stream& ts )
{
    Token t3{ ts.get( ) };
    switch( t3.getkind())
    {
    case '9': return t3.getvalue();
    case '(': {
        double d3{ expression( ts ) };
        if( ts.get().kind != ')' ) error( "keine rechte Klammer" );
        return d3;
    }
    default: error( "kein Primary" );
    }
}

Now I receive error because in the switch part I have to put a parameter for t3 and Im confused. Ive tried t3.getkind(t3), Ive tried getkind(t3); and I dont know what else to try...

Hope you help me!
Well guys, I did it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Token {
public:
    char getkind();
    double getvalue();
    void print() const;
    Token( );
    Token(const char& );
    Token(const double& );
private:
    char kind; // Die Kategorien der Eingabesymbole
    double value; // Bei Zahlenkategorie: der Wert

};

#endif // TOKEN_H


1
2
3
4
5
6
7
8
char Token::getkind()
{
    return kind;
}
double Token::getvalue()
{
    return value;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
double primary( Token_stream& ts )
{
    Token t3{ ts.get( ) };
    switch( t3.getkind())
    {
    case '9': return t3.getvalue();
    case '(': {
        double d3{ expression( ts ) };
        if( ts.get().kind != ')' ) error( "keine rechte Klammer" );
        return d3;
    }
    default: error( "kein Primary" );
    }
}


Greetings!
Here is what I have done in the header file:
1
2
3
4
5
#ifndef TOKEN_H
#define TOKEN_H
#include <iostream>
using namespace std;
#include <string> 


First you should never use the using statement in a header file.

Second nothing in this header requires anything in <iostream> or <string> so don't use those #includes in this header file (only #include what you actually need.

Now some basic notes, "get" functions usually "return" a value held in the class without modifying any class member variables. "set" functions usually modify a class member variable and don't return any value.

You can simplify your class constructors somewhat by using modern C++ features:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Token
{
    public:
        Token( ) = default;  // The compiler generated no-argument constructor should be adequate.
        Token(char kind);   // No need for const & when dealing with a POD (Plain Old Data) type, pass by value should be adequate.
        Token(double value);  // See above.

        char getkind() const;  // This function shouldn't modify anything so make it const, no parameter needed.
        double getvalue() const; // See above
        void print() const;
    private:
        char kind = '9' ; // By default this will be the '9' character.  
        double value = 0.0; // By default this value will be zero.
};


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
#include "token.h"
#include <iostream>

Token::Token(char ch ) : kind{ ch }
{}

Token::Token(const double& val ) : value{ val }
{}


void Token::print() const
{
    if( kind == '9' ) // I recommend you use braces for all control statements until you are more familiar with the language.
        std::cout << value;  // Don't forget to properly scope items from the std namespace.
    else 
        std::cout << kind;
}

char Token::getkind() const
{
    return kind;
}

double Token::getvalue() const
{
    return value;
}



And here are the changes I did:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double primary( Token_stream& ts )
{
    Token t3{ ts.get( ) };
    switch( t3.getkind())
    {
        case '9':
            return t3.getvalue();
        case '(': 
        {
            double d3{ expression( ts ) };
            if( ts.get().kind != ')' )   // Remember you need to use the "get" function because "kind" is private.
               error( "keine rechte Klammer" );
            return d3;
    }
    default: 
        error( "kein Primary" );
    }
    // You need to return something in case the program get's here (when the "default case" executes).
}



Now I receive error because in the switch part I have to put a parameter for t3 and Im confused. Ive tried t3.getkind(t3), Ive tried getkind(t3); and I dont know what else to try...

This will be "fixed" by the changes already made to the "get" functions.

By the way posting the actual compiler errors would make finding the problems easier in the future.





Last edited on
Topic archived. No new replies allowed.