Please forgive me if I asked too big a question. I tried to work it into an example I found in the forum. Though this is for a class, I'm just trying to figure out my homework and my question is centered around this forum question - I'm not asking for magic code to get an A! (actually am taking this class p/fail for personal enrichment.) .... I have been using the answer to the following question to try to buildup to a better understanding of a homework problem in my C++ class. But I'm getting stuck because there is some basic idea (just one?:-) I think I just don't get. The code I attached to the bottom of this question is me working with this forum sample, but I haven't gotten a sense yet, of how to setup an array that is inside a class. That is part of the assignment.
C++: syntax for accessing member struct from pointer to class
I'm fighting this problem somewhat, because my background is as a structured programmer - the reason I'm taking this class is to work my brain into an oop mode. So sometimes I go off on a track that leads away from oop, without realizing it.
MY QUESTION
I need to be able to instantiate an object of class 'Foo'. That object needs to have an array of 3 kinds of things associated with it. Uh.. I think in this context those things would be called 'Bar'. Those things have attributes 'otherdata' and 'andMoreData'. There is just one piece of data in the object called 'somedata' (Sorry I am mostly using the names of variables from the code I found here).
So I see it like this.
I have an object I instantiated and it is named 'foo' it is of class 'Foo'. It has one ivar called 'somedata' I can assign a value to. Then I want to setup 3 occurances of 'Bar' data. Each of those occurrances will consist of the ivars otherdata and andMoreData. Maybe with default data for each ivar like this otherdata = 1, andMoreData = 2
I can't figure out how I write a constructor to get the defualt data into my object.
I can't figure out how I access the data within the object for display or changing.
...
I hope I didn't ask too big a question here
....
HERE IS THE CODE I HAVE BEEN TRYING TO REWORK from this forum , hoping that I could build up from this explanation to a good understanding, but I get stuck when I try to create a constructor. ....
/* MY VARIATION ON FOOBAR
Original EXamples found at
https://stackoverflow.com/questions/915106/c-syntax-for-accessing-member-struct-from-pointer-to-class
*/
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_num = 3;
class Foo{
// I think I have this right, that Bar is the name of the struct
// and 'mybar' is an ivar within the class, with a data type of 'Bar'
//
public:
struct Bar{
int otherdata;
int andMoreData;
}
mybar; // I think I am saying that mybar is an ivar of type Bar
/* Here I tried to move forward with what I wanted to do, but the
compiler thought it was a bad idea
*/
// Here I'm attempting to create a constructor with default data
{ // set default data for mybar
otherdata = 1;
andMoreData = 2;
}
// Here I'm attempting to fill an array 'nmybar' with 3 occurances of my struct
// but I don't know how to say that it's associated with that struct/variable
// actually this seems like the WRONG place to do this, but my teachers example
// does something similar in the .h file (and of course his example works fine, but I
// don't understand it
nmybar[MAX_num]; // this calls mybar() MAX_num times
int somedata;
};
int main(){
// I instantiate class Foo and name my object 'foo'
Foo foo;
// Then I want to refer to the mybar portion of my foo object
// mybar could contain more than one variable itself, in this case
// it doesn't. so I am just referring to the one variable that
// a 'Bar' variable can contain, which is 'otherdata'
// HERE I added a second variable to the struct to make sure I understood how
// to use it , ok that works
foo.mybar.otherdata = 5;
foo.mybar.andMoreData = 6;
cout << foo.mybar.otherdata << endl ;
cout << foo.mybar.andMoreData << endl ;
return 0;
}
/*
Program loaded.
run
[Switching to process 1297]
Running…
5
6
Debugger stopped.
Program exited with status value:0. */