Evo primera:
Projektovati na jeziku C++ klasu vektora u prostoru predstavljenih pomoću komponenata u pravcu x, y i z osa. Koristiti operatorske funkcije kad je primereno. Predvideti:
* stvaranje vektora sa zadatim komponentama (podrazumevano (1,0,0)),
* dohvatanje komponenata vektora,
* izračunavanje intenziteta vektora (!v),
* izračunavanje zbira dva vektora (v1+v2, v1+=v2),
* izračunavanje proizvoda vektora i realnog broja (v*r, r*v, v*=r),
* čitanje vektora iz datoteke (dat>>v), i
* pisanje vektora u datoteku (dat<<v).
Sastaviti na jeziku C++ glavni program koji pročita dinamički niz vektora sa glavnog ulaza, izračuna aritmetičku srednju vrednost vektora u nizu, ispiše dobijeni rezultat na glavnom izlazu i ponavlja prethodne korake sve dok za dužinu niza ne pročita nedozvoljenu vrednost.
vektor.h
Code:
#ifndef VEKTOR_H
#define VEKTOR_H
#include <iostream.h> // Moras sa .h jer ne sme namespace u hederu !!!
class Vektor {
public:
double x, y, z;
Vektor(double a=1, double b=0, double c=0);
void dohvati(char ch);
double operator!();
Vektor operator+(Vektor v_desni);
void operator+=(Vektor v_desni);
Vektor operator*(double r);
void operator*=(double r);
friend ostream& operator<<(ostream& os, const Vektor& v);
friend istream& operator>>(istream& is, Vektor& v);
};
#endif // VEKTOR_H
vektor.cpp
Code:
#include <iostream>
#include <cmath>
#include "vektor.h"
using namespace std;
Vektor::Vektor(double a, double b, double c) { x=a; y=b; z=c; }
void Vektor::dohvati(char ch) {
if (ch == 'x')
cout << "x = " << x << endl;
else if (ch == 'y')
cout << "y = " << y << endl;
else if (ch == 'z')
cout << "z = " << z << endl;
else
cout << "Greska, unesite x, y ili z !";
}
double Vektor::operator!() {
return sqrt(pow(x, 2) + pow(y, 2) + pow(y, 2));
}
Vektor Vektor::operator+(Vektor v_desni) {
return Vektor(x+v_desni.x, y+v_desni.y, z+v_desni.z);
}
void Vektor::operator+=(Vektor v_desni) {
x = x + v_desni.x;
y = y + v_desni.y;
z = z + v_desni.z;
}
Vektor Vektor::operator*(double r) {
return Vektor(r*x, r*y, r*z);
}
void Vektor::operator*=(double r) {
x = x * r;
y = y * r;
z = z * r;
}
ostream& operator<<(ostream& os, const Vektor& v) {
return os << v.x << " " << v.y << " " << v.z << endl;
}
istream& operator>>(istream &is, Vektor& v) {
return is >> v.x >> v.y >> v.z;
}
main.cpp
Code:
#include <iostream>
#include "vektor.h"
using namespace std;
const int N=50;
int main() {
while (1) {
cout << "Unesite broj elemenata niza: ";
int n;
cin >> n;
if (n<=0 || n>N)
break;
Vektor* a = new Vektor [n];
Vektor srvr(0,0,0);
for (int i=0; i<n; i++) {
cout << "Unesite x, y i z kordinatu za " << i << ". vektor: ";
cin >> a[i].x >> a[i].y >> a[i].z;
srvr += a[i];
}
srvr = srvr * (1 / (double)/*!!!*/n);
cout << "Srednja vrednost ima kordinate: " << srvr << endl;
delete [] a;
}
}
/* TESTIRANJE KLASE
#include <iostream>
#include <fstream>
#include "vektor.h"
using namespace std;
int main() {
Vektor v1, v2(5, 7, 16.2), v3, v4, v5, v6, v7;
ofstream izlaz("izlaz.txt");
ifstream ulaz("ulaz.txt");
v2.dohvati('z');
cout << "Intenzitet vektora v2 je " << !v2 << endl;
v3 = v1+v2;
v3.dohvati('x');
v3.dohvati('y');
v3.dohvati('z');
v4+=v2;
v4.dohvati('x');
v4.dohvati('y');
v4.dohvati('z');
v5 = v4 * 10;
v5.dohvati('x');
v5.dohvati('y');
v5.dohvati('z');
v5 *= 10;
v5.dohvati('x');
v5.dohvati('y');
v5.dohvati('z');
izlaz << v5;
izlaz << v4;
ulaz >> v6;
v6.dohvati('x');
v6.dohvati('y');
v6.dohvati('z');
ulaz >> v7;
v7.dohvati('x');
v7.dohvati('y');
v7.dohvati('z');
}
*/
Program odlično radi, jedini "problem" je što pri kompajliranju gcc-om izbacuje sledeća upozorenja:
In file included from /usr/include/c++/3.3.4/backward/iostream.h:31,
from vektor.h:3,
from vektor.cpp:3:
/usr/include/c++/3.3.4/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
In file included from /usr/include/c++/3.3.4/backward/iostream.h:31,
from vektor.h:3,
from main.cpp:2:
/usr/include/c++/3.3.4/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.