如果我问的话太大,请原谅我。我试着把它变成我在论坛中找到的一个例子。虽然这是一堂课,但我只想弄清楚我的作业,我的问题是围绕这个论坛问题 - 我不是要求魔法代码来获得A! (实际上我正在接受这个课程/不能进行个人致富。) .... 我一直在使用以下问题的答案来尝试构建以更好地理解我的C ++类中的作业问题。但是我被困了,因为有一些基本的想法(只有一个?:-)我想我只是没有得到。我在这个问题的底部附上的代码是我使用这个论坛样本,但我还没有意识到如何设置一个类内的数组。这是任务的一部分。
C++: syntax for accessing member struct from pointer to class
我正在解决这个问题,因为我的背景是一个结构化的程序员 - 我参加这个课程的原因是让我的大脑进入oop模式。因此,有时候我会走出一条远离oop的轨道而没有意识到它。
我的问题
我需要能够实例化类'Foo'的对象。该对象需要具有与其相关联的3种事物的数组。呃..我认为在这种情况下,这些东西会被称为“酒吧”。那些东西有'otherdata'和'andMoreData'属性。对象中只有一个数据称为“somedata”(抱歉,我主要使用的是我在此处找到的代码中的变量名称)。
所以我觉得这样。
我有一个实例化的对象,它被命名为'foo',它是'Foo'类。它有一个名为'somedata'的ivar我可以赋值。然后我想设置3个'Bar'数据的出现。每个出现的事件都包括ivars otherdata和andMoreData。也许有这样的每个ivar的默认数据 otherdata = 1,andMoreData = 2
我无法弄清楚如何编写构造函数以将defualt数据导入到我的对象中。 我无法弄清楚如何访问对象内的数据以进行显示或更改 ... 我希望我在这里没有提出太大的问题 ....
这是我试图从这个论坛重新编写的代码,希望我可以从这个解释中积累到一个很好的理解,但是当我尝试创建一个构造函数时,我会陷入困境。 ....
/* 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. */
答案 0 :(得分:1)
我清理了一些代码并生成了一个工作示例。
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_num = 3;
class Foo
{
public:
struct Bar {
int otherdata;
int andMoreData;
Bar () : otherdata(1), andMoreData(2) { };
};
Bar mybar[MAX_num]; // Array of MAX_num Bar's
int somedata;
};
int main ()
{
Foo foo;
foo.mybar[0].otherdata = 5; // [0] for first element, [1] for second, [2] for third
foo.mybar[0].andMoreData = 6; // don't try to access higher positions, as the array has
// only three elements
cout << foo.mybar[0].otherdata << endl; // 5
cout << foo.mybar[0].andMoreData << endl; // 6
cout << foo.mybar[1].otherdata << endl; // 1
cout << foo.mybar[1].andMoreData << endl; // 2
return 0;
}
但看起来有点像你真的不知道该怎么做。我建议你从一开始就学习c ++来学习c ++教程或书(在stackoverflow上有一些关于优秀初学者书籍的条目)。虽然开始时看起来可能更耗时,但您将从中学到更多东西。
答案 1 :(得分:1)
你为Bar编写一个构造函数,就像你对任何构造函数一样。如果Bar不在Foo里面那么它就是这样的:
struct Bar {
Bar();
int otherdata;
int andMoreData;
};
Bar::Bar(): otherdata(1), andMoreData(2) { }
当你把Bar放在Foo里面时,它变成了:
class Foo {
struct Bar {
Bar();
int otherdata;
int andMoreData;
};
};
Foo::Bar::Bar(): otherdata(1), andMoreData(2) { }
然后,给每个Foo 3实例的Bar对象,你会做类似的事情:
class Foo {
struct Bar {
Bar();
int otherdata;
int andMoreData;
};
Bar bars[3];
};
Foo::Bar::Bar(): otherdata(1), andMoreData(2) { }
还有其他方法可以格式化,但以上是最惯用的。您会注意到bars
变量和Bar
类在上面都是私有的。人们普遍认为成员变量应该在一个类中是私有的(我告诉你这是因为你说你的目的是更好地理解OO。)有时可以公开结构(Bar
)。可用,但如果结构只是为了方便,那么最好将其设为私有。
答案 2 :(得分:0)
const int MAX_num = 3;
class Foo{
public:
struct Bar{
Bar() { // Constructor - initializes a Bar instance
otherdata = 1;
andMoreData = 2;
}
int otherdata;
int andMoreData;
};
Bar mybar; // Separating the Bar type from the mybar variable. Your original syntax is valid, but this is more common
Bar nmybar[MAX_num]; // nmybar is an array of MAX_num Bar's
int somedata;
};
答案 3 :(得分:0)
好的,非常感谢发布的人我能够处理我理解的代码示例。
然后我能够将我学到的想法应用到课堂上。
我现在能够使用我需要的结构和数组创建类的类!现在我可以重新完成其余的作业解决方案(我得到了所有的运行机器工作但只喝一杯的零碎)以适应所需的课程。谢谢!
这是我的新样本类,有真实姓名等。
/ * C++ syntax/how to declare, fill and access an array within a class 从MacGucky的解决方案开始 * /
使用namespace std;
const int UNIQUE_DRINKS = 5;
/ * class DrinkMachine { 私人的: 串饮料; 双倍成本; int drinksLeft; 双钱收集;
* /
类DrinkMachine { 上市: / *所以'DrinkMachine'课程中有两个成员 一个是'moneyCollected' 另一个是'瓶' '瓶子'有3个属性: a)'喝' - 饮料名称 b)'成本' - 一杯饮料多少钱 c)'drinkLeft' - 机器中放入了多少独特的饮料 * / struct Bottles { 串饮料; 双倍成本; int drinksLeft;
// this is a constructor for 'Bottles' It puts default data into an instance OF 'Bar'
Bottles () : drink("cola"), cost(0.75), drinksLeft(20) { };
};
Bottles myBottles[UNIQUE_DRINKS]; // myBottles is an Array of UNIQUE_DRINKS Bottles
// refer to 'myBottles' like a member, that has children (two dots for it's children)
// versus one dot for moneyCollected
double moneyCollected;
};
int main() { //实例化DrinkMachine,创建一个名为'Building1'的对象
cout << "Lets's fill up the drink machine in Building 1 ! " << endl ;
DrinkMachine Building1;
Building1.moneyCollected = 20.00;
// objectname.ARRAYNAME[position].ivarWithinStruct
// KEEP ALL the defaults for first Bottles item (Cola)
// most of the rest matches the defaults except name and price on water
Building1.myBottles[1].drink = "grape soda";
Building1.myBottles[2].drink = "root bear";
Building1.myBottles[3].drink = "orange soda";
Building1.myBottles[4].drink = "bottled water";
Building1.myBottles[4].cost = 1.00;
for (int i=1; i<=4;i++) {
cout << Building1.myBottles[i].drink ;
cout << " " ;
cout << Building1.myBottles[i].cost ;
cout << " " ;
cout << Building1.myBottles[i].drinksLeft ; //
cout << endl;
}
cout << "\t * * * " << endl ;
cout << "\t Total Bucks Collected Today " << Building1.moneyCollected << endl ;
return 0;
}
/ * 将程序加载到调试器中...... 程序已加载。 跑 [切换到流程3332] 正在运行... 让我们填满1号楼的饮料机! 葡萄苏打0.75 20 根熊0.75 20 橙子汽水0.75 20 瓶装水1 20
今天收集的总奖金20
调试器已停止。 程序退出,状态值为:0。 * /