Author Topic: Sev needs help with his first object-oriented C++ program.  (Read 1036 times)

So I thought I'd test out classes and object oriented programming to make sure I know it. I wanted to create something where I could create members of a class and have them have variables that represent their name and their value and be able to access it.

This is what I made

Code: [Select]
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class Test {
    string temporary;
    int temporary2;
    int value;
    string retain;
    string a;
    int b;
public:
    void create (string);
    void agd (int);
    int contain();
    string name();
    int returnname (string) {
        a = %enter.name;
        return(a);
    }
    int returnvalue (int) {
        b = %enter.contain;
        return(b);
    }
};

void Test::create (string input) { // Creates an object and assigns it a name
    temporary = input;
    Test %temporary; // object creation
    %temporary.name (temporary); // assigning it a name to the variable
    retain = temporary;
}

void Test::agd (int input2) {
    temporary2 = input2;
    %retain.contain (temporary2);
}

int main() {
    string tempinput;
    string enter;
    string q;
    int c;
    Test operate;
    while (1); {
    cout << "What would you like to do?";
    cout << "1. Add a database entry";
    cout << "2. Check a database entry";
    cin >> tempinput;
    if (tempinput == 1) {
        cout << "Enter a name for the entry";
        getline (cin,tempinput);
        operate.name (tempinput);
        cout << "Now assign the entry a value";
        cin >> c;
        operate.agd (c);
        cout << "Complete!";
    }
    else if (tempinput == 2) {
        cout << "What is the name of the entry?";
        getline(cin,enter);
        q = operate.returnname (enter);
        cout << "Name:" << q; << ".\n";
        c = operate.returnvalue ();
        cout << "Value:" << c; << ".\n";
    }
               }
    return(0);
}
There's probably numerous errors, but at one part.

specifically the void Test::create and the void Test::agd I get errors that it can't identify %enter.

What I wanted to do was create an object that wouldn't just be called ENTER, but rather the value of the string ENTER. I figured the ampersand would work. I could be wrong though because I didn't pay much attention when reading about pointers.

If someone would be kind enough to help me out, I'd love you forever :D.



Test %temporary; // object creation
Variables don't use a % identifier in C++

%temporary.name (temporary); // assigning it a name to the variable
retain = temporary;

Even if the above did work you're using the same variable name twice. This would mean you can't access the member variable easily and generally makes it confusing to work with.

Test::create makes and destroys a Test object with no effect. If you're trying to create a function that e.g. makes a Test object with a specific variable set then you should use a "constructor":
Code: [Select]
#include <iostream>
#include <string>

using namespace std;

class Test
{
   public:
      string name;

      //constructors have no return type
      Test();
      Test(string input);

      string getName();
};

//Default for when you put no arguments
Test::Test()
{
   name = "unnamed";
}

//Called when you put one argument in
Test::Test(string input)
{
   name = input;
}

int main(int argc, char *argv[])
{
   //An object you create with no arguments is set to "unnamed"
   Test first;
   cout << "The first one is " << first.name << endl;

   //You can set a variable when creating it
   Test second("Foo");
   cout << "The second one is named " << second.name << endl;

   string input;

   cout << endl << endl;
   cout << "Enter a name for the next one: ";
   cin >> input;
   cout << endl;

   Test custom(input);
   cout << "It's named " << custom.name;
}

Quote from: Output
The first one is unnamed
The second one is named Foo

Enter a name for the next one: Joe
It's named Joe

For yours specifically, you'd be able to do something like "Test entry(name, value)" from two inputs.

To make things easier you should put everything under 'public' access until you specifically need the variables restricted from outside - otherwise you have to make lots of useless getName/setName/getValue/... methods that don't have any function. (i.e. use it for adding checks "value is more than 0" "name can't be empty" but for these test ones don't)


This just helps with setting the initial name and value, you can't do Torque's accessing by making strings for names in C++. The closest approximation would be making some kind of data structure (key/value map) to store all your entry objects and looking them up through that.
« Last Edit: April 09, 2011, 07:58:02 AM by Space Guy »

-snip-
For one, I tried building and running this and it leaves me with an empty terminal.

I know what you're doing here, but I wanted to create something where your input will SET the name of the object. Not create an object and set the value of input to name();

For one, I tried building and running this and it leaves me with an empty terminal.

I know what you're doing here, but I wanted to create something where your input will SET the name of the object. Not create an object and set the value of input to name();
In short, I need a way to do something like

Test <value of string goes here>;


In short, I need a way to do something like

Test <value of string goes here>;


Test<String>, Test<Int>, etc? Look up templates.

Test<String>, Test<Int>, etc? Look up templates.
That's horribly unhelpful.

Let me elaborate, let's say I have string q;

now I told the user to enter in a name

cin >> q;

now I want to create an object with that name

test <some way to make an object with the value of Q yet not called Q>;

anyone?

You want to make an unique object, dynamically, based on a value inputted by the end user? Or do you want to create an instance of an object and give it the value? I don't know what you want between < ... >. In C++, those characters are used for creating a template class (in that context).

You want to make an unique object, dynamically, based on a value inputted by the end user? Or do you want to create an instance of an object and give it the value? I don't know what you want between < ... >. In C++, those characters are used for creating a template class (in that context).
Oh my loving god.

In this case I'm using it as a way to express an idea singularly in code. From now on I'll use //

I want to create an object, based on the value inputted by the end user. You said yourself. The question is how.

Oh my loving god.

In this case I'm using it as a way to express an idea singularly in code. From now on I'll use //

I want to create an object, based on the value inputted by the end user. You said yourself. The question is how.
Calm down. Mention that the first time. The way you were saying it, using Test before the <> made it look like the syntax for templates. How was I supposed to know?

Anyways, I'm not quite sure what you are asking exactly. Like, like if you are just creating an instance of that object and passing the inputted value, that's easy enough.

Test newTest = Test(q);

Calm down. Mention that the first time. The way you were saying it, using Test before the <> made it look like the syntax for templates. How was I supposed to know?

Anyways, I'm not quite sure what you are asking exactly. Like, like if you are just creating an instance of that object and passing the inputted value, that's easy enough.

Test newTest = Test(q);
No.

I want the string, like the value of the string.

Like this

THIS IS STRING Q

Q = "HELLO"

Q WAS SET TO HELLO BY THE USER TYPING HELLO WHEN PROMPTED TO BY THIS LINE:

Getline(cin,q);

SO I WANT THE VALUE OF Q, WHICH IS, "HELLO" TO BECOME A NEW OBJECT UNDER THE CLASS TEST

SO THEREFORE I WANT TO CREATE THE OBJECT HELLO.

Alright, so you want to load a string object with the value given by the console input stream?

Alright, so you want to load a string object with the value given by the console input stream?
I already explained what I want. Just do it.

Alright, I had to re-read that. Also, for future reference, don't be a richard to someone trying to help you.

From what I gathered, you want the input to become a sub-class of the object Test? As far as I know with C++, this can't be done.

Now, since I am fairly good with object-oriented design, maybe you can explain what you need it for? There could be other possible solutions.