Reputation: 817
I'm still learning c++ and have a lot more experience with Java. In Java, creating a class can be as simple as this:
public class vertex
{
public double x, y, z;
public boolean eliminated = true;
public vertex(double x, double y, double z)
{
//vertex constructor
}
}
I'm trying to do something pretty much identical in C++, save for built in "get" functions for returning the values that should be set when creating an instance of the class, and want to know if my syntax is correct. Here is the class:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Vertex // Standard way of defining the class
{
public:
// This means that all of the functions below this(and any variables) are accessible to the rest of the program.
Vertex(double x, double y, double z);
//constructor
double getX();
double getY();
double getZ();
double x;
double y;
double z;
};
double Vertex:: getX()
{
return x;
}
I would also like advice on creating a class that has instances of custom classes like the one aboce within them.
Upvotes: 0
Views: 7947
Reputation: 3731
public class vertex
{
public double x, y, z;
public boolean eliminated = true;
public vertex(double x, double y, double z)
{
//vertex constructor
}
}
To mimic this Java class as simply and as exactly as possible in C++ you would do this:
struct vertex{
double x, y, z;
bool eliminated = true;
vertex(double x, double y, double z){
//vertex constructor
}
};
Remarkably similar right? Note that the eliminated
member is default initiated, which is new to C++11, so make sure you have an up to date compiler.
struct
is used instead of class because all of your members are public anyway. You could also use class
and the public:
access specifier.
You do not need access methods (getters) because everything is public already, as in your Java class.
You don't need any includes in for your vertex class definitions and you definitely should NOT put a using
declaration any header files.
To mimic Java method parameter passing you pass copies for primitives and non-const pointers (not references) for object instance parameters.
edit - fixed the silly issues that I missed and that the human compilers below spotted :)
Upvotes: 2
Reputation: 7905
This might help you out:
Vector3.h
#ifndef VECTOR3_H
#define VECTOR3_H
namespace myNamespace { // Add This Because Many Libraries may define a Vector3 Object
class Vector3 {
union {
float m_f3[3];
struct {
float m_fx;
float m_fy;
float m_fz;
};
};
inline Vector3();
inline Vector3( float x, float y, float z );
inline Vector3( float *pfv );
~Vector3();
// Add Your inline overloaded operators & inline function declarations here.
};
#include "Vector3.inl"
} // namespace myNameSpace
#endif // VECTOR3_H
Vector3.inl
// ------------------------------------------------------
// Vector3() - Default Constructor Sets All Values To 0
inline Vector3::Vector3() :
m_fx( 0 ),
m_fy( 0 ),
m_fz( 0 ) {
} // Vector3
// ------------------------------------------------------
// Vector3() - Takes (x,y,z) Parameters
inline Vector3::Vector3( float x, float y, float z ) :
m_fx( x ),
m_fy( y ),
m_fz( z ) {
} // Vector3
// ------------------------------------------------------
// Vector3() - Takes A Pointer To Type
inline Vector3::Vector3( float *pfv ) :
m_fx( pfv[0] ),
m_fy( pfv[1] ),
m_fz( pfv[2] ) {
} // Vector3
Vector3.cpp
#include "Vector3.h"
namespace myNamespace {
// ----------------------------------------------
// ~Vector3() - Default Destructor - Does Nothing
// Only Defined Here So That There Is Some Type Of Translation
// Unit In The Object File Generated When Compiling
Vector3::~Vector3() {
} // ~Vector3
} // namespace myNamespace
To use this:
main.cpp
#include <iostream>
#include "Vector3.h"
int main() {
using namespace myNamespace; // Forgot to add this line here.
Vector3 v1;
std::cout << "Default Constructor Used: " << std::endl;
std::cout << "v1 = ( " << v1.m_fx << ", "
<< v1.m_fy << ", "
<< v1.m_fz << " )" << std::endl;
// Or This Way Works Too
std::cout << "v1 = ( " << v1.m_f3[0] << ", "
<< v1.m_f3[1] << ", "
<< v1.m_f3[2] << " )" << std::endl;
Vector3 v2( 2.2f, 3.3f. 4.4f );
std::cout << "2nd Constructor Used: " << std::endl;
std::cout << "v2 = ( " << v2.m_f3[0] << ", "
<< v2.m_f3[1] << ", "
<< v2.m_f3[2] << " )" << std::endl;
float f3[3] = { 4.5f, 6.7f, 8.9f };
Vector3 v3( f3 );
std::cout << "3rd Constructor Used: " << std::endl;
std::cout << "v3 = ( " << v3.m_fx << ", "
<< v3.m_f3[1] << ", "
<< v3.m_fz << " )" << std::endl;
} // main
EDIT:
If you want a have a class that includes instances of another user define class then it is as simple as this:
SomeClass.h
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
namespace myNamespace {
class Vector3;
class SomeClass {
private:
Vector3 m_currentPosition;
Vector3 m_velocity;
Vector3 m_finalPosition;
public:
SomeClass( Vector3 initialPosition, Vector3 currentVelocity );
Vector3 getFinalPosition() const;
private:
void calculateFinalPosition();
};
} // namespace myNamespace
SomeClass.cpp
#include "SomeClass.h"
#include "Vector3.h"
namespace myNamespace {
SomeClass::SomeClass( Vector3 initialPosition, Vector3 currentVelocity ) :
m_currentPosition( initialPosition ),
m_velocity( currentVelocity ) {
calculateFinalPosition();
}
Vector3 SomeClass::getFinalPosition() const {
return m_finalPosition;
} // getFinalPosition
void SomeClass::calculateFinalPosition() {
// Perform The Appropriate Calculation Here To Find The Final Position
} // calculateFinalPosition
} // namespace myNamespace
However for this to work; this would require the Vector3 to have the overloaded operators, functions such as cross, dot etc., to be defined.
Upvotes: -1