Hello guys, I wrote this code just for fun, but I get some bugs that I don't know how to fix. Any idea how to fix them ?
Please run the code in an IDE like c++ shell.
Purpose: I want to write a program that has a base class including virtual const functions (), and another subclass that is derived from the main class, and also uses its public to develop the virtual const functions. The main sends in a vector of values that another vector needs to receive and add up the total, and then print it.
#include <iostream>
#include <vector>
usingnamespace std;
class big{
public:
virtualint add_up() const = 0;
virtualvoid print() const = 0;
};
class small : public big{
private:
vector <int> v;
int total = 0;
public:
small(vector<int> temp){
for (unsignedint i = 0; i < temp.size(); ++i)
{
v[i] = temp[i]; // Send all the elements to the new vector.
}
}
int add_up() const {
for (unsignedint i = 0; i < v.size(); ++i)
{
total += v[i];
}
return total;
}
void print() const {
int print_total = add_up();
cout << print_total << endl;
}
};
int main(){
vector<int> send_vect = {1, 2, 3, 4, 5};
small use; // small is the name of class, and use is the object
use(send_vect);
return 0;
}
@cire, I already have a default constructor in my class 'small'. The default constructor can act as parametrized constructor as well, so it is not necessary to have two separate constructor in my opinion.
Also could you please explain why small use(send_Vect); is possible and not small use ? I remember I used my previous format somewhere and it worked.
Here is the fixed code that does not have bugs, but it also does not print anything. I don't understand why ?
#include <iostream>
#include <vector>
usingnamespace std;
class big{
public:
virtualint add_up() const = 0;
virtualvoid print() const = 0;
};
class small : public big{
private:
vector <int> v;
int total;
public:
small(vector<int> temp){
for (unsignedint i = 0; i < temp.size(); ++i)
{
v[i] = temp[i]; // Send all the elements to the new vector.
}
for (unsignedint i = 0; i < v.size(); ++i)
{
total += v[i];
}
}
int add_up() const{
return total;
}
void print() const {
int print_total = add_up();
cout << "total is: " << print_total << endl;
}
};
int main(){
vector<int> send_vect = {1, 2, 3, 4, 5};
small use (send_vect);
use.print();
return 0;
}
total hasn't been initialised, so you shouldn't be adding to it.
Vector v starts life empty, so v[i] = temp[i];
is meaningless. Use v.push_back( temp[i] ) if your really have to do it like this, but probably better to just ...
The default constructor can act as parametrized constructor as well, so it is not necessary to have two separate constructor in my opinion.
A default constructor does not take parameters so you can create an object like this: Classname var(), or var or var{}
does not have bugs, but it also does not print anything. I don't understand why ?
You code doesn't do what it is supposed to do. If not a bug what else do you call it?
v[i] = temp[i]; // Send all the elements to the new vector.
Accessing elements in an empty vector is undefined behaviour. In Visual Studio your code crashes.
@cire, I already have a default constructor in my class 'small'. The default constructor can act as parametrized constructor as well, so it is not necessary to have two separate constructor in my opinion.
You do not have a default constructor. A default constructor is defined as one which can be used with no arguments. The only constructor you define requires an argument, thus it is not a default constructor.
Certainly it is not necessary to have two constructors, but is impossible to create an object of type small as you tried to do in the original code supplied without a default constructor.
As Tomas1965 points out, you cannot access elements of a vector that do not exist.
1 2 3 4 5 6 7 8 9 10
small(vector<int> temp) : total(0) {
for (size_t i=0; i<temp.size(); ++i) {
v.push_back(temp[i]);
total += temp[i];
}
}
[code] small(vector<int> temp) : v(temp), total(0) {
for (size_t i=0; i<v.size(); ++i) { total += v[i]; }
}
Perhaps more important than correcting the syntax and logic errors is realizing the current design benefits nothing from the use of inheritance or virtual functions.