Skip to content

Instantly share code, notes, and snippets.

@isagallerani
Created January 4, 2017 23:54
Show Gist options
  • Save isagallerani/9659767f7e15a67601a72b033161d69d to your computer and use it in GitHub Desktop.
Save isagallerani/9659767f7e15a67601a72b033161d69d to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
/**
USANDO HERANÇA E A CLASSE VETOR2D COMO BASE, CRIE UMA NOVA CLASSE CHAMADA VETOR3D QUE ALÉM DAS COORDENADAS X E Y,
TAMBÉM TERÁ A COORDENADA Z. IMPLEMENTE O QUE FOR NECESSÁRIO PARA QUE A SAÍDA ABAIXO SEJA GERADA.
CASO SEJA NECESSÁRIO INCLUIR NOVOS MÉTODOS NA CLASSE VETOR2D, DEMONSTRE CLARAMENTE.
**/
class Vetor2d {
Vetor2d(int Vx, int Vy):x(Vx), y(Vy) {
//vazio
}
Vetor2d():x(0), y(0) {
//vazio
}
int getX() const;
int getY() const;
friend ostream &operator<<(ostream &, const Vetor2d&);
private:
int x, y;
};
class Vetor3d : public Vetor2d {
public:
Vetor3d(int Vx, int Vy, int Vz);
Vetor3d();
int getZ() const;
friend ostream &operator<<(ostream &, const Vetor2d&);
private:
int z;
};
Vetor3d::Vetor3d(int Vx, int Vy, int Vz):Vetor2d(Vx, Vy) {
z = Vz;
}
Vetor3d::Vetor3d():Vetor2d() {
z = 0;
}
ostream& operator<<(ostream &saida, const Vetor2d& v) //agora não preciso mais fazer o imprime()!
{
saida << "(" << v.x << ", " << v.y << ", " << v.z << ")" << endl;
return saida;
}
ostream& operator<<(ostream &saida, const Vetor3d& v) //agora não preciso mais fazer o imprime()!
{
saida << "(" << v.x << ", " << v.y << ", " << v.z << ")" << endl;
return saida;
}
int Vetor2d::getX() const{
return x;
}
int Vetor2d::getY() const{
return y;
}
int Vetor3d::getZ() const{
return z;
}
int main() {
Vetor2d v1(5, 6);
Vetor3d v2(7, 8, 9);
cout <<"V1 = " << v1 << endl;
cout << "V2 = " << v2 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment