base class and subclass - vectors - Bugs

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.



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>


using namespace std;

class big{

	public:

	   virtual int add_up() const = 0;
	   virtual void print() const = 0;

};

class small : public big{
	private:
		vector <int> v;
		int total = 0;

	public:
		small(vector<int> temp){
			for (unsigned int i = 0; i < temp.size(); ++i)
			{
				v[i] = temp[i];	// Send all the elements to the new vector.
			}
		}

		int add_up() const {
			for (unsigned int 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;
}
Last edited on
Your type small does not have a default constructor, so you cannot create one without supplying the requisite argument.

small use(send_vect);, not small use;
@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 ?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>


using namespace std;

class big{

	public:

		virtual int add_up() const = 0;
		virtual void print() const = 0;

};

class small : public big{
	private:
		vector <int> v;
		int total;

	public:
		small(vector<int> temp){
			for (unsigned int i = 0; i < temp.size(); ++i)
			{
				v[i] = temp[i];	// Send all the elements to the new vector.
			}
			for (unsigned int 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;

}

Last edited on
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 ...

... replace your lines 23 to 26 with
1
2
total = 0;
v = temp;
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]; }
    }


or, perhaps even:
1
2
3
    small(vector<int> temp) : v(temp), total(std::accumulate(v.begin(), v.end(), 0)) 
    {
    }


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.
Last edited on
Topic archived. No new replies allowed.