Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: C++ abstract class

  1. #1
    Join Date
    Nov 2009
    Location
    Uruguay
    Posts
    742

    Default C++ abstract class

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

    Hi, can anyone help me with this:

    I have an abstract class:

    Code:
    class A {
    public:
    virtual void f1 () = 0;
    void f2 ();
    }; // A
    and the class B

    Code:
    class B: public A {
    ...
    Public:
    ...
    B * operator+ (B *);
    ...
    }; // B
    inside the implementation of the operator + I need to use a tmp variable

    Code:
    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 hlar nan hd gn
    -----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-----

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: C++ abstract class

    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.

  3. #3
    Join Date
    Nov 2009
    Location
    Uruguay
    Posts
    742

    Default Re: C++ 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 hlar nan hd gn
    -----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-----

  4. #4
    Join Date
    Nov 2010
    Location
    Ελλάδα(Αθήνα)-Россия (г. Красноярск)
    Posts
    1,711

    Default Απ: C++ abstract class

    Code:
    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.
    Code:
    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.


    Code:
    B * B::operator+ (B *b)
    Quote Originally Posted by VampirD
    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
    Could you give us the procedure of formulas who you write?
    Πάντα Φιλικά, Στάμος.
    Desktop: openSUSE Leap 15.3||x86_64||Gnome 3.14 & KDE 5||AMD Ryzen 7 8 core||Nvidia GTX 1080 Ti Gaming Edition
    Γλώσσες Προγραμματισμού: C++, Qt developing.
    http://bit.ly/fT8Hfi

  5. #5
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: C++ abstract class

    Quote Originally Posted by VampirD View Post
    but I implement it... it's the first function I wrote on the cpp file
    Yeah? So where is it? In B.cpp that is. You only showed us the operator + function.

  6. #6
    Join Date
    Nov 2010
    Location
    Ελλάδα(Αθήνα)-Россия (г. Красноярск)
    Posts
    1,711

    Default Απ: Re: C++ abstract class

    Quote Originally Posted by ken_yap View Post
    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.
    Πάντα Φιλικά, Στάμος.
    Desktop: openSUSE Leap 15.3||x86_64||Gnome 3.14 & KDE 5||AMD Ryzen 7 8 core||Nvidia GTX 1080 Ti Gaming Edition
    Γλώσσες Προγραμματισμού: C++, Qt developing.
    http://bit.ly/fT8Hfi

  7. #7
    Join Date
    Nov 2009
    Location
    Uruguay
    Posts
    742

    Default Re: C++ abstract class

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

    Here it is

    Code:
    #include "comparable.h"
    
    void comparable::ordenar(comparable ** c) {
    ...
    } // comparable::ordenar
    Code:
    #ifndef COMPARABLE_H
    #define COMPARABLE_H
    
    class comparable {
    public:
    virtual int comparar(Comparable*) = 0;  // compare
    void ordenar(Comparable**);  // sort
    }; // comparable
    
    #endif
    Code:
    #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) {
    ...
    } // >>
    Code:
    #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 hlar nan hd gn
    -----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-----

  8. #8
    Join Date
    Nov 2010
    Location
    Ελλάδα(Αθήνα)-Россия (г. Красноярск)
    Posts
    1,711

    Default Απ: Re: C++ abstract class

    Because I am programmer and use C++. As I can see you have missed the # include<iostream> and of course the function main().
    Πάντα Φιλικά, Στάμος.
    Desktop: openSUSE Leap 15.3||x86_64||Gnome 3.14 & KDE 5||AMD Ryzen 7 8 core||Nvidia GTX 1080 Ti Gaming Edition
    Γλώσσες Προγραμματισμού: C++, Qt developing.
    http://bit.ly/fT8Hfi

  9. #9
    Join Date
    Nov 2010
    Location
    Ελλάδα(Αθήνα)-Россия (г. Красноярск)
    Posts
    1,711

    Default Απ: Re: C++ abstract class

    What do you try to create?
    Give more clues.
    Πάντα Φιλικά, Στάμος.
    Desktop: openSUSE Leap 15.3||x86_64||Gnome 3.14 & KDE 5||AMD Ryzen 7 8 core||Nvidia GTX 1080 Ti Gaming Edition
    Γλώσσες Προγραμματισμού: C++, Qt developing.
    http://bit.ly/fT8Hfi

  10. #10
    Join Date
    Nov 2009
    Location
    Uruguay
    Posts
    742

    Default Re: C++ abstract class

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •