C++ abstract class

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi, can anyone help me with this:

I have an abstract class:


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/

iEYEARECAAYFAk2VKNoACgkQJQ+0ABWtaVmKOgCfYoPzpK41MJ/UuO5hlOFVuTRo
VJQAoLBrjGMOpwSmFeAm+9EhgvNZWKrE
=ViHR
-----END PGP SIGNATURE-----

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.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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/

iEYEARECAAYFAk2VOCIACgkQJQ+0ABWtaVlPwgCgoNcM8k0mTzI3nxUrTf7ps4oV
UMAAn2Sqb8CgBYqPaWWpwgp2j3osmmyb
=IMtC
-----END PGP SIGNATURE-----

class B: public A {
...
Public:
...
B * operator+ (B *);
...
}; // B

Here is your inheritance for class B.
As I can see in this code there are faults.


class B::class A

About f1 () = 0;
and void f2 (); are fuctions but you do not give us how you have created. Only that you declare inside class.

B * B::operator+ (B *b) 

Could you give us the procedure of formulas who you write?

Yeah? So where is it? In B.cpp that is. You only showed us the operator + function.

I want all the code who you write, So I will can help you.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Here it is


#include "comparable.h"

void comparable::ordenar(comparable ** c) {
...
} // comparable::ordenar


#ifndef COMPARABLE_H
#define COMPARABLE_H

class comparable {
public:
virtual int comparar(Comparable*) = 0;  // compare
void ordenar(Comparable**);  // sort
}; // comparable

#endif


#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/

iEYEARECAAYFAk2VofYACgkQJQ+0ABWtaVlOKACfStZohIA+ysEetcDs/ADDsbgH
TxsAn3CL7wWqcqFQ3UlJQGv42Rkj/W7b
=eseS
-----END PGP SIGNATURE-----

Because I am programmer and use C++. As I can see you have missed the # include<iostream> and of course the function main().

What do you try to create?
Give more clues.

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.

Write here the vocalization of your homework. I will try to solve it.

Try also to install in your system dev c/c++ through wine.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Fixed!!! thanks

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/

iEYEARECAAYFAk2V+/wACgkQJQ+0ABWtaVnV9QCgrN4Vth8MNeUk+Wb8+cbIcvEF
itEAnA6ls+0+uXQUlBiKDOjjHGWUQdaP
=auXR
-----END PGP SIGNATURE-----

Send me the vocalization. I will try to solve it differently.