I solved solved as it is written in the comment.
The problem is
Base b1(2), b2(10);
b1.print(); // 2
b2.print(); // 10
for (int i = 0; i < 5; i++) {
b1.setN(i, (i + 1) * 20);
b2.setN(i, (i + 1) * 10);
}
b1.printData(); // 20 40
b2.printData(); // 10 20 30 40 50 0 0 0 0 0
Derived d(5);
d.print(); // 5
d.printData(); // 0 0 0 0 0
for (int i = 0; i < 10; i++) {
d.setN(i, (i + 1) * 3);
}
d.printData(); // 3 6 9 12 15
d.insert(99); // "Base" class does not have "insert" method.
d.printData(); // 3 6 9 12 15 99 -> can't solve it..!
so i made class Base And Derived class.
class Base {
int x;
vector<int> v1;
public:
Base(int x) : x(x){
vector<int>v2(x, 0);
v1 = v2;
}
/* ...*/
}
};
I made it up to the base, but there's something I don't know.
class Derived : public Base {
public:
Derived(int x) : Base(x) {}
virtual void insert(int x) {
//How can I Access on Base?
}
};
I encountered a difficulty because I didn't know how to accese vector of derived/ vector of base. How do I make it?
reading about overriding, inheritance but it was confused to me.
Related
I am using C++ stable_sort to sort a vector of my class objects in ascending order using a comparator function, but the sort is not stable. A work around that worked was to reverse iterate and reversing the logic in the comparator. But cant understand why it shouldnt work normally.
Code:
using namespace std;
class Pair{
string str;
int num;
public:
Pair(string s, int n):str(s), num(n)
{}
Pair(const Pair &a)
{
str = a.str;
num = a.num;
}
int Num()
{
return num;
}
string Str() const{
return str;
}
void set(string s, int n)
{
str = s;
num=n;
}
void print() const{
cout<<"\n"<<num<<" "<<str;
}
};
bool comparator( Pair a, Pair b)
{
return a.Num()<=b.Num();
}
int main() {
int n;
cin >> n;
vector<Pair> arr;
for(int a0 = 0; a0 < n; a0++){
int x;
string s;
cin >> x >> s;
if((a0+1)<=n/2)
s="-";
Pair p(s, x);
arr.push_back(p);
}
cout<<"\n Before sort";
for(auto i:arr)
i.print();
stable_sort(arr.begin(), arr.end(), comparator);
cout<<"\n\n After sort";
for(auto i:arr)
i.print();
return 0;
}
Result:
Before sort
0 -
6 -
0 -
6 -
4 -
0 -
6 -
0 -
6 -
0 -
4 that
3 be
0 to
1 be
5 question
1 or
2 not
4 is
2 to
4 the
After sort
0 to
0 -
0 -
0 -
0 -
0 -
1 or
1 be
2 to
2 not
3 be
4 the
4 is
4 that
4 -
5 question
6 -
6 -
6 -
6 -
comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ​true if the first argument is less than (i.e. is ordered before) the second.
from stable_sort. The comparator must implement a strict weak ordering. See also here for a table of the exact requirements.
Your comparator is wrong, it also returns true for equal elements.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I encountered strange side effects that I cannot explain to myself in the slightest. Probably I am missing something very obvious, but I have looked for the bug for several hours now and the code is quite simple, so I concluded that I must have some rather fundamental misunderstanding about... something.
Consider this code, that was meant to compute the product of two 2d matrices (I have changed the set() function to add -1 to the argument cell, to make the debug output more comprehensible.
template<class T, unsigned column_count, unsigned row_count>
class Matrix
{
private:
const static unsigned row_length = column_count;
const static unsigned column_length = row_count;
using matrix_type = std::array<std::array<T, row_length>, row_count>;
matrix_type matrix;
public:
using value_type = T;
Matrix(const matrix_type& matrix) : matrix(matrix) {}
Matrix() {}
friend std::ostream& operator<<(std::ostream& o, const Matrix& rhs)
{
for (unsigned i = 0; i < column_count; ++i) {
for (unsigned j = 0; j < row_count; ++j) {
o << rhs.matrix[i][j] << ' ';
}
o << '\n';
}
return o;
}
const auto& get_rows() const { return matrix; }
const auto get_columns() const
{
std::array<std::array<T, column_length>, column_count> columns;
for (unsigned i = 0; i < row_length; ++i) {
for (unsigned j = 0; j < column_length; ++j) {
columns[i][j] = matrix[j][i];
}
}
return columns;
}
void set(unsigned i, unsigned j, T v) { matrix[i][j] = -1; }
friend Matrix operator*(const Matrix& m1, const Matrix& m2)
{
auto columns = m1.get_columns();
auto rows = m2.get_rows();
Matrix m3;
std::cout << "before:"
<< "\n";
std::cout << m1 << "\n";
std::cout << m2 << "\n";
std::cout << m3 << "\n";
unsigned i{ 0 };
for (const auto& row : rows) {
i++;
unsigned j{ 0 };
for (const auto& column : columns) {
j++;
value_type v{ 0 };
for (unsigned k = 0; k < column.size(); ++k) {
v += row[k] * column[k];
}
m3.set(i, j, v);
}
}
std::cout << "after:"
<< "\n";
std::cout << m1 << "\n";
std::cout << m2 << "\n";
std::cout << m3 << "\n";
return m3;
}
};
As you can see, the getter functions either return a copy or a constant reference. The operator* function takes constant parameters.
I now construct two matrices like so:
std::array<int, 3> c1{ { 1, 2, 3 } };
std::array<int, 3> c2{ { 4, 5, 6 } };
std::array<int, 3> c3{ { 7, 8, 9 } };
std::array<std::array<int, 3>, 3> m1{ { c1, c2, c3 } };
std::array<std::array<int, 3>, 3> m2 = m1;
Matrix<int, 3, 3> matrix1(m1);
Matrix<int, 3, 3> matrix2(m2);
Now I invoke operator* in different ways:
matrix1* matrix2;
result:
before:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
0 0 183238709
after:
-1 -1 -1
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
0 0 0
0 -1 -1
-1 -1 -1
matrix2* matrix1;
result:
before:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
0 0 -1823473620
after:
1 2 3
4 5 6
7 8 9
-1 -1 -1
4 5 6
7 8 9
0 0 0
0 -1 -1
-1 -1 -1
matrix1* matrix1;
result:
before:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
1385085408 32767 401515081
1 1385085440 32767
1385085440 32767 1385085464
after:
-1 -1 -1
4 5 6
7 8 9
-1 -1 -1
4 5 6
7 8 9
1385085408 32767 401515081
1 -1 -1
-1 -1 -1
As you can see, the matrix that gets passed as first argument will be changed. Which makes no sense to me, as it is passed as a const and set() only operates on m3. Somehow m3 gets partly "bound" to the matrix that is the first argument of operator*. Why?
You're writing out of bounds in your loops, since both i and j are incremented too early.
The code should look like this:
for (const auto& row : rows) {
// i++;
unsigned j{ 0 };
for (const auto& column : columns) {
// j++;
value_type v{ 0 };
for (unsigned k = 0; k < column.size(); ++k) {
v += row[k] * column[k];
}
m3.set(i, j, v);
j++; // <--
}
i++; // <--
}
I'm doing for my college a work in c++, my code is based on 2 classes,
NumSet and Game.
data members are private.
class NumSet
{
int arr[5]; //Cards
int Score;
}
class Game
{
NumSet P1, P2; //Player 1 , And Player 2
int OpenCard; //For The Card in The center
}
for adding a score to P1 \ P2 from a method inside of Game
I created this method:
void NumSet::Addscore()
{
++this->Score;
}
and this other method:
void NumSet::PrintScore()
{
cout << this->Score << endl;
}
Until now it all looks fine, but for some reason
when I call the method Addscore:
P2.Addscore();
it raises its value from 0 to 2..
NumSet::NumSet() //C'tor
{
for (int i = 0; i < STARTCARDS; i++)
arr[i] = NULL;
this->Sort(); //BubbleSort
Score = 0;
}
void Game::ChangeCards()
{
if (x1 > x2) //Player 1 is Stronger
P1.Addscore();
else if (x2 > x1) //Player 2 is Stronger
P2.Addscore();
else //Both Cards Are Equal
{ //Checkin For The Lower Max Num
int max1 = P1.Max(); //Max returns maximum num in arr
int max2 = P2.Max();
if (max1 < max2)
P1.Addscore();
else if (max2 < max1)
P2.Addscore();
}
}
I really would like an explanation of what's wrong in here.
Thanks!
nothing seems wrong here.. just make sure -
1) Score is initialized to 0
2) Not doing any assignment or increment by mistake
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C and C++ : Partial initialization of automatic structure
While reading Code Complete, I came across an C++ array initialization example:
float studentGrades[ MAX_STUDENTS ] = { 0.0 };
I did not know C++ could initialize the entire array, so I've tested it:
#include <iostream>
using namespace std;
int main() {
const int MAX_STUDENTS=4;
float studentGrades[ MAX_STUDENTS ] = { 0.0 };
for (int i=0; i<MAX_STUDENTS; i++) {
cout << i << " " << studentGrades[i] << '\n';
}
return 0;
}
The program gave the expected results:
0 0
1 0
2 0
3 0
But changing the initialization value from 0.0 to, say, 9.9:
float studentGrades[ MAX_STUDENTS ] = { 9.9 };
Gave the interesting result:
0 9.9
1 0
2 0
3 0
Does the initialization declaration set only the first element in the array?
You only initialize the first N positions to the values in braces and all others are initialized to 0. In this case, N is the number of arguments you passed to the initialization list, i.e.,
float arr1[10] = { }; // all elements are 0
float arr2[10] = { 0 }; // all elements are 0
float arr3[10] = { 1 }; // first element is 1, all others are 0
float arr4[10] = { 1, 2 }; // first element is 1, second is 2, all others are 0
No, it sets all members/elements that haven't been explicitly set to their default-initialisation value, which is zero for numeric types.
Here, is my code. Just trying to wrap my head around some of the basic things you can do with TMP. I'm trying to supply two numbers with which the compiler will add up that range of numbers. I'm just not sure how to write the syntax for the "constraint" template.
template < int b, int e >
struct add {
enum { sum = add< b + 1, e >::sum + b };
};
template <>
struct add< e, e > {
enum { sum = 0 };
};
int main() {
cout << add< 4, 8 >::sum << endl; //30
return 0;
}
template <int e>
struct add< e, e > { ...
And the result is 4 + 5 + 6 + 7 + 0 == 22, not 4 + 5 + 6 + 7 + 8 == 30. Once e==e in add<...>, add<...>::sum==0, not e.