class A {
public:
virtual void f1 () = 0;
void f2 ();
}; // A
and the class B
class B: public A {
...
Public:
...
B * operator+ (B *);
...
}; // B
inside the implementation of the operator + I need to use a tmp variable
B * B::operator+ (B *b) {
...
B *tmp;
...
} // +
but I can’t compile it because tmp is used uninitialized in this
function, if I change it to B *tmp = new B(); the error is that I cannot
allocate an object of abstract type ?B? because the following virtual
functions are pure within ?B? A::f1
so, how can I do this?
VampirD
No in elenath hîlar nan hâd gîn
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/
Don’t you have to implement f1 and f2 in B before you can instantiate it? After all that’s what an abstract class is, you promise to provide an implementation in the concrete class of the methods in the abstract class.
but I implement it… it’s the first function I wrote on the cpp file
VampirD
No in elenath hîlar nan hâd gîn
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/
#include "fecha.h"
#include <stdexcept>
int fecha::comparar(fecha *f) { // anio = year; mes = month; dia = day
int f1 = (anio * 10000) + (mes * 100) + dia;
int f2 = (f->anio * 10000) + (f->mes * 100) + f->dia;
return (f1 - f2);
} // comparar
fecha::fecha() {
...
} // fecha
fecha::fecha (fecha &f) {
...
} // fecha
fecha::fecha (int d, int m, int a) {
...
} // fecha
int fecha::getDia() {
...
} // getday
int fecha::getMes() {
...
} // getmonth
int fecha::getAnio() {
...
} // getyear
void fecha::setDia(int d) {
...
} // setday
void fecha::setMes(int m) {
...
} // setmonth
void fecha::setAnio(int a) {
...
} // setyear
bool fecha::operator ==(fecha *f) {
...
} // ==
bool fecha::operator <=(fecha *f) {
...
}
bool fecha::operator >=(fecha *f) {
...
} // =>
const fecha * fecha::operator +(int d) {
fecha *tmp = new fecha();
int aux = dia + d;
tmp->dia = aux % 30;
aux = mes + (aux / 30);
tmp->mes = aux % 12;
tmp->anio = anio + (aux / 12);
return tmp;
} // +
const fecha * fecha::operator -(int d) {
...
} // -
std::ostream& operator <<(ostream &s, fecha *f) {
...
} // <<
std::istream& operator >>(istream &s, fecha &f) {
...
} // >>
#ifndef FECHA_H
#define FECHA_H
#include "comparable.h"
#include <iostream>
using namespace std;
class fecha : public Comparable {
private:
int dia; // day
int mes; // month
int anio; // year
public:
fecha();
fecha(fecha&);
fecha(int, int, int);
int getDia();
int getMes();
int getAnio();
void setDia(int);
void setMes(int);
void setAnio(int);
bool operator ==(fecha *);
bool operator <=(fecha *);
bool operator >=(fecha *);
const fecha * operator +(int);
const fecha * operator -(int);
int comparar(fecha*);
~fecha();
}; // fecha
std::ostream& operator <<(std::ostream &o,fecha *f);
std::istream& operator >>(std::istream &i,fecha *c);
#endif
VampirD
No in elenath hîlar nan hâd gîn
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/
It’s a homework about a datatype date, but I don’t understand well the ABC,
if I have a pure virtual function and I define a derived class that
implement it, I would be able to instantiate a variable of that class don’t?
I have the base class comparable and I have implemented the virtual function
on the derived class fecha, so I believe that I can use the derived class as
a normal one, is that correct?
–
VampirD
Microsoft Windows is like air conditioning
Stops working when you open a window.
it was a type error on the virtual function
on the ABS class I have virtual int comparar(comparable *) = 0; and I
have implemented it as int fecha::comparar(fecha *f);
VampirD
Microsoft Windows is like air conditioning
Stops working when you open a window.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/