Vector<double> as class member variable - c++

Just like I'm creating member variables for x, y, and theta, how would I create member variables for the type vector<double> in the constructor? What I'm doing below doesn't seem to work.
Positioning::Positioning(double x, double y, double theta) {
this->globalX = x;
this->globalY = y;
this->globalTheta = theta;
vector<double> this->xs;
}
I have done #include <vector> and have std::vector<double> xs; in the private section of Positioning.hpp
EDIT
Header file:
namespace Position {
class Positioning {
private:
double globalX;
double globalY;
double globalTheta;
std::vector<double> xs;
public:
Positioning(double x, double y, double theta);
~Positioning();
void updateGlobalPosition(double left_dist, double right_dist, double rear_dist);
double getX();
double getY();
double getTheta();
};
}

This works
Positioning::Positioning(double x, double y, double theta) {
this->globalX = x;
this->globalY = y;
this->globalTheta = theta;
}
All your class variables are created automatically, you don't have to do anything special to create them.
The code in the body of the constructor above is not creating anything, it's assigning values to your variables. Of course if you want to give a member variable a particular value, then you have to do something, but they get created automatically.

Related

C++ How to call a constructor parameter inside a class method

So I'm a total noob at C++, I decided to learn C++ and skipped directly to the Object-oriented programming. I'm coding a class called KineticEnergy that has a constructor with the parameters x and y which is assigned to the variables mass and velocity.
I have a class method called result() which calculates the Kinetic Energy using its formula. I want to call the parameters from my constructor within the formula but I have no idea what I'm exactly doing here (bad english, don't know how to explain). I am getting errors like "[Error] x was not declared in this scope". Here is the code I written:
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return (1/2) * (x * (pow(y, 2)));
} // What am I gonna do here for this to work?
};
int main()
{
double a = 12.1;
double b = 6.4;
KineticEnergy ke(a, b);
cout << ke.result();
return 0;
}
It is not necessary. your constructor parameters is saved in "mass" and "velocity" as class members.
double result()
{
return (1./2.) * (mass * (pow(velocity , 2.)));
}
Parameters of the parameterized constructor are not member variables. That's why you are storing param values in member variables inside of the parameterized constructor. So that, you should use member variables inside of the result() function.
try this
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return 0.5 * (mass * pow(velocity, 2));
}
};
int main()
{
double a = 12.1;
double b = 6.4;
double Result;
KineticEnergy ke(a, b);
Result = ke.result();
cout << Result;
}
x and y were declared in your constructor, therefore only known by your constructor. you cannot use them outside of it. however, mass and velocity are known variables of your class and can be used anywhere as long as they are public.
in your main you give mass and velocity of your ke object values, that's why you can call any method of your class that uses these variables after(again, as long as they're public)

How to add another Initialize function to base class but not edit base file?

I am having trouble initializing the class, I want to extend it a bit. This is the baseclass:
basefile.cpp
Class Point3d
{
public:
    Point3d ();
    Point3d (double x, double y, double z);
Point3d& set (double x, double y, double z);
    double x, y, z;
}
Point3d :: Point3d (): x (0.0), y (0.0), z (0.0)
{
}
inline Point3d &
Point3d :: set (double xx, double yy, double zz)
{
     x = xx;
     y = yy;
     z = zz;
     return * this;
}
.....
Edit
I want when declaring Point3D pt {1,1}, it means z = 0, so if I didn't edit the original file, how to add a default initialization function z = 0 on base class but from another file, it looks like this:
Point3d::Point3d(double xx, double yy, double zz=0) { x = xx; y = yy; z = zz; }
//or
Point3d :: Point3d (double xx, double yy)
{
     x = xx;
     y = yy;
     z = 0;
     return * this;
}
I thought of using a derived class, but what I wanted was to extend the base class because I wanted to use the base class directly, I searched the partial class as well as many different ways but still failed.
You can't.
You must declare your member function, member method or constructor inside your class header to define it in another place.
You can only add members to a class in its definition and there can be only one definition of a class.
Extending classes at a later point is done via inheritance as you note in your question.
If you want to have a function set in your class, you need to declare it in the class definition:
class Point3d
{
public:
Point3d ();
Point3d (double x, double y, double z);
Point3d& set (double xx, double yy, double zz = 0);
double x, y, z;
};
Also note that you don't need the second definition of set, since you are already using a default argument for z.
You also have a few syntax errors in your code. In particular set lacks return values in its function head.

Trouble with passing const reference object as parameter, VS debugger reports 'left of '.getXCoord' must have calls/struct/union'

In my header file, I've defined the Point class, constructs a (x, y) coordinate point, contains a few getter and setter member functions, and contains a toDistance member function, which returns the distance between two points.
class Point
{
private:
double x_coord;
double y_coord;
public:
Point(double x, double y);
Point();
void setXCoord(double);
void setYCoord(double);
double getXCoord();
double getYCoord();
double toDistance(const Point*);
};
Here are the member functions (of interest) in the implementation file:
double Point::getXCoord() {
return x_coord;
}
double Point::getYCoord() {
return y_coord;
}
double Point::toDistance(const Point *P2) {
double p1_x = getXCoord();
double p1_y = getYCoord();
double p2_x = P2.getXCoord();
double p2_y = P2.getYCoord();
double x_distance = p2_x - p1_x;
double y_distance = p2_y - p1_y;
double distance = sqrt(pow(x_distance, 2) + pow(y_distance, 2));
return distance;
}
When running with test data, VS returns the error left of '.getXCoord' must have calls/struct/union' for the lines double p2_x = P2.getXCoord(); and double p2_y = P2.getYCoord();
What am I doing wrong here?
If I understand your question correctly, P2 is const reference. In that case you're not allowed to call non-const functions. You need to make your getter functions const (maybe also the toDistance function). You'd change your functions like this:
double getXCoord() const;
etc. Also make sure to change that in the implementation.
If P2 is a const pointer (not a reference) then in addition you need to modify your calls to be like P2->getXCoord() etc.

Accessing variables in nested classes

I've nested a class for use within another class and need to try accessing individual parts of it but can't. How would I go about doing this?
class Point
{
public:
Point() { float x = 0, y = 0; }
void Input(int &count); //input values
Rectangle myRec;
private:
float x, y;
};
class Rectangle
{
public:
Rectangle(); //side1 - horizontal, side2 - vertical
void SetPoint(const Point point1, const Point point2, const Point point3, const Point point4) { LLPoint = point1; LRPoint = point2; URPoint = point3; ULPoint = point4; }
float CalcSides(Point LL, Point LR, Point UL, Point UR);
private:
Point LLPoint, LRPoint, ULPoint, URPoint;
float side1, side2, length, width, area, perimeter; //side1 - horizontal, side2 - vertical
};
float Rectangle::CalcSides(Point LL, Point LR, Point UL, Point UR)
{
side1 = (LR.x - LL.x);
}
How can I access the x and y values for the points I've created in the Rectangle class?
If you really want to do this, then you can make the classes friends.
class Rectangle;
class Point
{
friend class Rectangle;
public:
Point() { x = 0; y = 0; }
void Input(int &count); //input values
private:
float x, y;
};
More likely though, you simply want to add accessors to the Point class as it is fairly useless as is.
class Point
{
public:
Point() { x = 0; y = 0; }
void Input(int &count); //input values
float getX() const { return x; }
float getY() const { return y; }
private:
float x, y;
};
Or, if Point is really going to be so simple and not need to maintain any invariants at all, just expose x and y as public members.
Also, you probably don't want to have Point contain a Rectangle but rather refer to one either through a pointer or a reference, if it refers to one at all. After all, a Point can be useful without reference to a Rectangle (e.g. - maybe it's used for Triangles too).

c++: initialising an object of a class in another class

Could not find a clear solution for this problem.
I have two classes Point and Vector. Vector is a child of Point In one of the methods of class Point I want to use an object of class Vector. I do it like this:
class Point
{
double x, y, z;
public:
// constructor from 3 values
Point(double x, double y, double z)
: x(x), y(y), z(z)
{}
// method move point
Point move(Vector vect, double dist)
{
Vector vectU = vect.unit();
return sum(vectU.multiplyScalar(dist));
}
};
class Vector: public Point
{
double x, y, z;
public:
// constructor from 3 values
Vector(double x, double y, double z)
: Point(x, y, z), x(x), y(y), z(z)
{}
// create unit vector
Vector unit()
{
double len = length();
return Vector(x / len, y / len, z / len);
}
};
When I compile this it gives me an error in line Point move(Vector vect, double dist) "Vector" has not been declared. I cannot find any useful answer for this error. How do I do this initialisation?
In C++ a class needs to be declared before it is defined. In your example with everything in one file, it has no idea what a Vector is when you define your Point::move function.
Typically, we'd have a header file per class (MyClass.h etc) and put the function definitions in a cpp file per class (MyClass.cpp)
So you need to restructure to something like:
Point.h:
#ifndef _POINT_H
#define _POINT_H
class Vector; // Forward declaration so you don't need to include Vector.h here
class Point
{
double x, y, z;
public:
// constructor from 3 values
Point(double x, double y, double z);
// method move point
Point move(Vector vect, double dist);
}
#endif // _POINT_H
Point.cpp
#include "Point.h"
#include "Vector.h"
// constructor from 3 values
Point::Point(double x, double y, double z)
: x(x), y(y), z(z)
{}
// method move point
Point Point::move(Vector vect, double dist)
{
Vector vectU = vect.unit();
return sum(vectU.multiplyScalar(dist));
}
Vector.h
#ifndef _VECTOR_H
#define _VECTOR_H
#include "Point.h"
class Vector: public Point
{
double x, y, z;
public:
// constructor from 3 values
Vector(double x, double y, double z)
: Point(x, y, z), x(x), y(y), z(z);
// create unit vector
Vector unit();
}
#endif // _VECTOR_H
Vector.cpp
#include "Vector.h"
// constructor from 3 values
Vector::Vector(double x, double y, double z)
: Point(x, y, z), x(x), y(y), z(z)
{}
// create unit vector
Vector Vector::unit()
{
double len = length();
return Vector(x / len, y / len, z / len);
}
(disclaimer, No guarantees that this will compile and work straight away, this is just to demonstrate how the code should be split up!)
Put a forward declaration:
class Vector;
at the beginning of the file.
Also, put a ; after the definition of each class.
If your class Vector
class Vector: public Point
inherits from Point, then you should not be using Vector in the base class Point (the base class shouldn't know anything about the derived class).
Also you are redefining x, y, z in your derived class Vector, which defeats the point of inheritance and can lead to very nasty behaviour when using polymorphism.
A virtual function may do the trick for you.
ie
move() Stub in the base
move() declaration in the derived.
Use pointers for dynamic binding.
eg point *x = new vector(...)
x.move()
etc etc.