How to fix error and improve the code?

I got below compile errors, need some inputs to fix it and improve the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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<<"Code runs good";

    return 0;
}






Output errors:

main.cpp: In constructor ‘Bar::Bar()’:
main.cpp:20:27: error: expected ‘{’ before ‘(’ token
Bar(): data (new int[10]) ()
^
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():
^~~
main.cpp:35:1: error: expected declaration before ‘}’ token
}
Is it good idea to use virtual destructor in base class? Also how can I replace dynamic array with vector?

Given you previous posts I am now convinced you are a traffic generator: aka troll.

Fix & improve? Stop being an oxygen thief.
Output errors:
These errors are pretty obvious. What is it that you don't understand?

Is it good idea to use virtual destructor in base class?
Yes.

Also how can I replace dynamic array with vector?
What problem do you have with this?
I was just wondering if vector would be an alternate way to use here.
It is actually recommended to use a vector over an dynamic array. Wenn accessing the data it works very similar.
Registered users can post here. Sign in or register to post.