Thread: improve this cpp code?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up improve this cpp code?

    I need to improve this code.
    any suggestions?

    Code:
    class Foo
    {
    public:
    virtual void func() const = 0;
    };
    class Bar: public Foo
    {
    public:
    Bar(): data (new int[10]) ()
    ~Bar() {}
    void func() const{
    data++;
    }
    int get()
    {
    return data;
    }
    private:
    int *data;
    };
    Foo *foo = makeBar();
    foo->func():
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Indentation is a good start

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Can someone help me as I am new to C++ and still learning it. I see the below compile errors. how to fix it? any suggestions please.

    main.cpp: In member function ‘virtual void Bar::func() const’:
    main.cpp:23:5: error: increment of member ‘Bar::data’ in read-only object
    data++;
    ^~
    main.cpp: In member function ‘int Bar::get()’:
    main.cpp:27:8: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
    return data;
    ^~~~
    main.cpp: At global scope:
    main.cpp:32:20: error: ‘makeBar’ was not declared in this scope
    Foo *foo = makeBar();
    ^
    main.cpp:33:1: error: ‘foo’ does not name a type
    foo->func():

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    this is full code...

    Code:
    #include <iostream>
    
    using namespace std;
    class Foo
    {
    public:
    virtual void func() const = 0;
    };
    class Bar: public Foo
    {
    public:
    Bar(): data (new int[10]) ()
    ~Bar() {}
    void func() const{
    data++;
    }
    int get()
    {
    return data;
    }
    private:
    int *data;
    };
    Foo *foo = makeBar();
    foo->func():
    return 0;
    }
    int main()
    {
        cout<<"Hello World";
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    The code doesn't really make sense.
    What do you want it to do?
    Last edited by john.c; 03-09-2021 at 02:25 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    I need to improve the code as it gives some compile errors...Need to fix them.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Need to improve the below code.

    Code:
    class Foo
    {
    public:
    virtual void func() const = 0;
    };
    class Bar: public Foo
    {
    public:
    Bar(): data (new int[10]) ()
    ~Bar() {}
    void func() const{
    data++;
    }
    int get()
    {
    return data;
    }
    private:
    int *data;
    };
    Foo *foo = makeBar();
    foo->func():
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Any suggestions here?

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by leo2008 View Post
    Any suggestions here?
    Write code that makes sense.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    If you have an abstract function in the base class you need a virtual constructor.
    c++ - When to use virtual destructors? - Stack Overflow

    I also would replace the dynamic array with a std::vector.
    std::array - Dynamic Memory, no Thanks - ModernesCpp.com

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Can someone help me as I am new to C++ and still learning it.
    Yeah, since 2008 apparently - when you first joined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Improve my code
    By hariharaan in forum C Programming
    Replies: 0
    Last Post: 05-20-2015, 02:05 AM
  2. Can I improve my code?
    By advancedk in forum C Programming
    Replies: 1
    Last Post: 07-27-2008, 07:47 AM
  3. Help me improve my code!
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-19-2006, 10:04 AM
  4. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM

Tags for this Thread