Tego maina nie mozna zmieniac, po prostu trzeba do niego dopisac reszte, tak aby bylo dobrze ;)
#include "Vector.h"
int main()
{
Vector a, b;
const Vector c(3.0, 5.0, 4.0);
a.set(1, 1, 1);
b.set_x(2);
b.set_y(3);
b.set_z(4);
//Format wypisania w nawiasach
std::cout
<< "a = " << a.get() << std::endl // (1, 1, 1)
<< "b = " << b.get() << std::endl // (2, 3, 4)
//i bez nawiasow
<< "c = " << c.get_x() << " " << c.get_y() << " " << c.get_z() << std::endl; // 3 5 4
Vector d = a.add(c);
const Vector e = d.substract(a);
std::cout
<< "d = " << d.get() << std::endl
<< "e = " << e.get() << std::endl
<< "tmp = " << d.multiply(1.1).get() << std::endl
// << "tmfsdfsdp = " << d.multiply(1.1).get_x() << std::endl
<< "tmp = " << d.scalar_multiply(c) << std::endl
<< "tmp = " << d.vector_multiply(c).get() << std::endl;
//<< "tmfsdfsdp = " << d.vector_multiply(c).get_x() << std::endl;
}
moj kod:
deklaracja klasy:
/*
* File: Vector.h
* Author: owen
*
* Created on 30 październik 2010, 22:07
*/
#ifndef _VECTOR_H
#define _VECTOR_H
#include <iostream>
class Vector
{
public:
Vector(double a = 0, double b = 0, double c = 0) : _a(a), _b(b), _c(c)
{
}
void set(double a, double b, double c);
void set_x(double a);
void set_y(double a);
void set_z(double a);
const char *get() const;
const char* get(); //tu chcialbym wyrzucic ta metode, ale bez niej sie sypie, dlaczego ?
const double get_x() const;
const double get_y()const;
const double get_z()const;
const Vector add(const Vector &a) const;
const Vector substract(const Vector &a) const;
Vector multiply(double a) const;
const int scalar_multiply(const Vector &a);
Vector vector_multiply(const Vector &a);
private:
double _a;
double _b;
double _c;
static char bufor_c[20]; //tego chcialbym sie tak docelowo pozbyc (albo tylko jedno z dwoch zostawic)
char bufor[20];
};
#endif /* _VECTOR_H */
definicje:
/*
* File: Vector.cpp
* Author: owen
*
* Created on 30 październik 2010, 22:07
*/
#include "Vector.h"
#include <cstdio>
char Vector::bufor_c[20];
void Vector::set(double a, double b, double c)
{
_a = a;
_b = b;
_c = c;
}
void Vector::set_x(double a)
{
_a = a;
}
void Vector::set_y(double a)
{
_b = a;
}
void Vector::set_z(double a)
{
_c = a;
}
const char* Vector::get() const
{
sprintf(Vector::bufor_c, "(%.1f,%.1f,%.1f)", _a, _b, _c);
return Vector::bufor_c;
}
const char* Vector::get()
{
sprintf(bufor, "(%.1f,%.1f,%.1f)", _a, _b, _c);
return bufor;
}
const double Vector::get_x() const
{
return _a;
}
const double Vector::get_y() const
{
return _b;
}
const double Vector::get_z() const
{
return _c;
}
const Vector Vector::add(const Vector& a) const
{
return Vector(_a + a._a, _b + a._b, _c + a._c);
}
const Vector Vector::substract(const Vector& a) const
{
return Vector(_a - a._a, _b - a._b, _c - a._c);
}
Vector Vector::multiply(double a) const
{
return Vector(_a*a, _b*a, _c * a);
}
Vector Vector::vector_multiply(const Vector& a)
{
return Vector((_b * a._c - _c * a._b), (_a * a._c - _c * a._a), (_a * a._b * _b * a._a));
}
const int Vector::scalar_multiply(const Vector& a)
{
return (_a * a._a + _b * a._b + _c * a._c);
}
Wszystko jest niby ładnie, pieknie i wszystko niby proste, ale jednej rzeczy nie rozumiem.
Dlaczego koniecznie potrzebuje, tuch dwoch metod get(), przeciez na moje oko wersje const char* get() - mozna spokojnie wyrzucic (wystarczy samo get, ktore traktuje obiekt przekazywany niejawnie jako staly) i pozbyc sie jednej tablicy ze skaldowych klasy, ale bez tego robilem to wszystkie mozliwe sposoby i sie po prostu sypie (metoda zwraca bledny c-string, ze zlymi wartosciami) :D
Jak cos takiego zrobic ?