C++ OOP approach

Started by
2 comments, last by Nyllian 12 years, 1 month ago
Hello everybody,

I have a few questions regarding C++ as it is new to me.
Finally I have made the step after 7 years of Java programming to C++.
The thing is that Java made me a die hard OOP minded programmer (where I think C++ and I won't be the best friends because of that)

I am a bit confused about the C++ OOP methods (as it is 90% the same but the approach is different).
The thing I'm trying to achieve is to create a global struct array variable that I can call & adjust.
But on the return tmpSq; I get a few errors that I don't understand at all sad.png
Is the difference in OOP that big between Java and C++ that it's a profession on it's own? Or did I miss something?

Square.h

struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};
CUSTOMVERTEX *square[];



Square.cpp

void main()
{
*square = getSquareArray(p);
}

CUSTOMVERTEX* getSquareArray(POINT p)
{
CUSTOMVERTEX tmpSq[] =
{
// First triangle
{ p.X, p.Y, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ p.X + DIMENSION, p.Y, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ p.X + DIMENSION, p.Y + DIMENSION, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
// Second triangle
{ p.X -2 + DIMENSION, p.Y + DIMENSION, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ p.X -2, p.Y + DIMENSION, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ p.X -2, p.Y, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
};

return tmpSq;
}


Aswell requesting an object thru a method is not possible? Like so:

CUSTOMVERTEX square[] = getSquareArray(p);


Thanks in advanced.
Yours sincerely,

Nyllian
Advertisement
I don't see how OOP even figures into this.

Also when you can't understand the errors you should probably at least tell us what they are... my crystal ball happens to be out of order.
There are several problems with that code, none of which have anything to do with OOP. Here are three of them:
* A name that is all capitals usually denotes a macro (which you should try to avoid, by the way), so CUSTOMVERTEX might confuse people.
* You probably want square to be an array of objects, not an array of pointers, and you probably shouldn't get used to using global variables.
* You can't return an array from a function in C++.
* `void main()' is not a valid prototype for main(). If you learned that from a book, throw the book away and get one that is less than 12 years old.

Here is a complete standard program that does similar things to what you were trying to do:
#include <iostream>

typedef unsigned u32;

u32 make_color(unsigned char r, unsigned char g, unsigned char b) {
return (r << 16) + (g << 8) + b;
}

struct Point2D {
float x, y;
Point2D(float x, float y)
: x(x), y(y) {
}
};

struct CustomVertex {
float x, y, z, rhw;
u32 color;

CustomVertex() {
}

CustomVertex(float x, float y, float z, float rhw, u32 color)
: x(x), y(y), z(z), rhw(rhw), color(color) {
}
};

void getSquareArray(CustomVertex square[6], Point2D p, float side) {
u32 yellow = make_color(255, 255, 0);

// First triangle

square[0] = CustomVertex(p.x , p.y , 0.5f, 1.0f, yellow);
square[1] = CustomVertex(p.x + side, p.y , 0.5f, 1.0f, yellow);
square[2] = CustomVertex(p.x + side, p.y + side, 0.5f, 1.0f, yellow);

// Second triangle
square[3] = square[2];
square[4] = CustomVertex(p.x , p.y + side, 0.5f, 1.0f, yellow);
square[5] = square[0];
}

int main() {
Point2D p(3.0f,4.0f);
CustomVertex square[6];

getSquareArray(square, p, 10.0f);

for (int i=0; i<6; ++i) {
std::cout << '(' << square.x
<< ',' << square.y
<< ',' << square.z
<< ") " << square.rhw
<< " (" << square.color
<< ")\n";
}
}
This is just a portion of code, it's actually the basic of the whole concept.
And the book was 14 years old, wich I allready have thrown away.

I understand the logic of your code alvaro. It actually makes perfect sense now I look at it.
You give the variable to the function that will set the variable instead of calling a returning method. (If I get it right)

and the `void main()` was loosly written out of hand but you get the picture.

Thanks for correcting me and pointing me to a proper pathway.
Yours truly,

Nyllian

This topic is closed to new replies.

Advertisement