Changing values in arrays of a different class - c++

(c++ problem) I have two classes A and B, and the main class(int main). I have an array in class A. In the main class, I want to make a change to the array in class A. When class B reads the array from class A, it should read the changed values. However, changes to the array I make in class A through int main take effect for all things in int main only, NOT class B. In other words, I am unable to permanently change the values in class A.
I created a dummy program (c++) to showcase my problem. If I input 3 for x (first cin) and 9 for y(second cin), the output is
00090
0
when it should be
00090
9
#include <math.h>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;
class A {
public:
int array[5] = { 0,0,0,0,0 };
int getNum(int index)
{
return array[index];
}
void changeNum(int index, int change)
{
array[index] = change;
}
};
class B {
public:
A obj1;
int getNum(int index)
{
return obj1.getNum(index);
}
};
int main()
{
A obj2;
B obj3;
int x,y;
cout << "Original Array: " << endl;
for (int i = 0; i < 5; ++i)
cout << obj2.getNum(i);
cout << endl << endl << "Enter index number:" << endl;
cin >> x;
cout << "Enter new number" << endl;
cin >> y;
obj2.changeNum(x, y);
for (int i = 0; i < 5; ++i)
cout << obj2.getNum(i);
cout << endl << obj3.getNum(x) << endl;
system("pause");
return 0;
}

If you want changes to the array in A to be visible globally, then you can make array a static variable like this:
class A {
public:
static int array[5];
// ...
};
int A::array[5] = { 0,0,0,0,0 };
From c++17, you could also define array inside the class:
class A {
public:
inline static int array[5] = { 0,0,0,0,0 };
// ...
}
Here's a demo.
Also, avoid doing using namespace std;. e.g. there is already an std::array, so variable names like array can cause serious problems.

Related

CPP program displays shadows the perimeter error [duplicate]

I am trying to make a function that returns double the integer number that I will pass to it. I am getting the following error message with my code:
declaration of 'int x' shadows a parameter int x; "
Here is my code:
#include <iostream>
int doublenumber();
using namespace std;
int doublenumber(int x)// <-- this is the function which returns double the value .
{
int x;
return 2 * x;
cout << endl;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin >> a;
doublenumber(a);
return 0;
}
You have x as a parameter and then try to declare it also as a local variable, which is what the complaint about "shadowing" refers to.
I did it because your advice was so helpful, and this is the final result :
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int n= doublenumber(a);
cout << "the double value is : " << n << endl;
return 0;
}
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int d = doublenumber(a);
cout << "Double : " << d << endl;
return 0;
}
There are some problem with your code. Your declaration and definition of function dies not match. So remove declaration as no necessity of it.
You are declaring local x variable inside function which will shadow your function arguments.

c++ reference to a class member but not changing value

Please help me to review the following code.
I am wondering why the variable "b" is not the modified value.
I can not change the value using reference ?
Thanks!
#include <iostream>
using namespace std;
class Foo{
public:
int a = 1;
int& check(){
return a;
};
};
int main()
{
int b;
Foo foo;
b = foo.check();
cout << b << endl;
foo.check() = 2;
cout << foo.a << endl;
cout << b << endl;
return 0;
}
The output is
1
2
1
As #Igor Tandetnik indicated, foo.check returns a reference, but b is an int, not a reference to int, so it keeps the original value.
What you want can be achieved by ...
#include <iostream>
using namespace std;
class Foo
{
public:
int a = 1;
int &check()
{
return a;
};
};
int main()
{
Foo foo;
int &b { foo.check() };
cout << b << endl;
foo.check() = 2;
cout << foo.a << endl;
cout << b << endl;
return 0;
}

How to call a setter function from one class in another class?

I'm trying to call the setter function from Class A in class B but I'm unable to do this and I don't know why. (I'm a beginner in C++ and I need to do this as it's related to my assignment.)
Here is my code:
#include <iostream>
using namespace std;
class A{
private:
int i = 2;
public:
int getInt(){
return i;
};
void setI(int a){
i = a;
}
};
class B{
private:
int c = 3;
public:
void setAI(A a){
a.setI(c);
}
};
int main() {
A a;
B b;
cout << a.getInt() << endl;
b.setAI(a);
cout << a.getInt() << endl;
a.setI(5);
cout << a.getInt() << endl;
}
The output I'm getting is:
2
2
5
When I want to get:
2
3
5
I just made some changes in the setAI() function. I have passed the object by address rather than value. I'm not sure of what was causing the problem (as I'm also a beginner) but the pointer has solved the problem.
#include <iostream>
using namespace std;
class A{
private:
int i = 2;
public:
int getInt(){
return i;
};
void setI(int a){
i = a;
}
};
class B{
private:
int c = 3;
public:
void setAI(A *a){
a->setI(c);
}
};
int main() {
A a;
B b;
cout << a.getInt() << endl;
b.setAI(&a);
cout << a.getInt() << endl;
a.setI(5);
cout << a.getInt() << endl;
}

Getting segmentation fault (core dumped) error when declaring a int variable

I was trying to create vector like class. Here is my code
#include <bits/stdc++.h>
using namespace std;
template<class t>
class vec{
t *start;
int size=0;
public:
void add_value(t a){
*(start+(sizeof(t)*size)) = a;
cout << (start+(sizeof(t)*size))<< " = "<< *(start+(sizeof(t)*size))<<endl;
size++;
// cout << start<< endl;
}
void operator [](int i){
cout << (start+(sizeof(t)*i))<< " = "<< *(start+(sizeof(t)*i))<<endl;
}
int length(){
return size;
}
};
int main(){
vec<int> t;
cout << t.length()<<endl;
t.add_value(8);
t.add_value(10);
cout << t.length()<<endl;
t[1];
}
This gives me correct output.
0
0x7fff0fe9b5d0 = 8
0x7fff0fe9b5e0 = 10
2
0x7fff0fe9b5e0 = 10
But when declare a int variable in main function like.
int main(){
int i=0; //newline
vec<int> t;
cout << t.length()<<endl;
t.add_value(8);
t.add_value(10);
cout << t.length()<<endl;
t[1];
}
output.
0
Segmentation fault (core dumped)
I also tried printing address of start variable and new int variable int those are different.
You probably want something like this:
#include <iostream>
using namespace std;
template<class t>
class vec {
t* start = new t[100]; // initialize the pointer (fixed size of 100, to be improved)
int size = 0;
public:
void add_value(t a) {
*(start + size) = a;
size++;
}
t operator [](int i) { // should return a t instead of a void
return *(start + i);
}
int length() {
return size;
}
};
int main() {
vec<int> t;
cout << "t.length() = " << t.length() << endl;
t.add_value(8);
t.add_value(10);
cout << "t.length() = " << t.length() << endl;
// display all values in t
for (int i = 0; i < t.length(); i++)
{
cout << "t[" << i << "] = " << t[i] << endl;
}
}
All multiplication by sizeof(t) have been removed, because pointer arithmetic already does this for you.
This very poor and minimalist example works as you expect, but the maximum number of elements you can store in the class is 100. I leave the improvement as an exercise to you.
BTW there are many other improvements you need to do, especially you need a destructor and possibly the rule of three
Note:
You should replace all instances of *(start + x) with start[x] which does exactly the same thing but which is more idiomatic and more readable.

Pointers, dynamic variables, C++

Errors
I keep receiving errors when running this program. What do these errors mean? A screenshot is attached.
This is the problem:
A class Area that has two private variable members; units of type string and area_value of type float.
1) Input from the keyboard the area_value and its units. Compute one-half and one-quarter of the area and display the results with unit.
2) Destroy the dynamic variable at the end.
#include <iostream
#include <string>
using namespace std;
class Area
{
public:
Area();
void setu(string a);
void seta(float b);
string getu();
float geta();
private:
string unit;
float area_value;
};
int main()
{
Area *p = new Area();
string a;
float f;
cout << "Enter a unit with no space: ";
getline(cin, a);
p->setu(a);
cout << "Enter a value of area: ";
cin >> f;
p->seta(f);
cout << "A half of your input = " << f / 2 << endl;
cout << "A quarter of your input = " << f / 4 << endl;
delete p;
return 0;
}
Looks like
You have not implemented the member functions, or
You have implemented the member functions in a separate file and forgot to include it in building the executable.
It seems you forgot to implement the constructor Area() and the methods of your Area class.
The first include is missing greater than sign (>). The public Area class only defines the functions, not implementing them. Variable p is defined as a pointer.
You can also add half and quarter as functions. I modified it slightly so it compiles with clang++ -Wall -std=c++11 -o area area.cpp.
#include <iostream>
#include <string>
using namespace std;
class Area {
public:
Area() {};
~Area() {};
void setu(string u) { unit = u; }
void seta(float a) { area_value = a; }
string getu() { return unit; }
float geta() { return area_value; }
float getHalf() { return area_value / 2; }
float getQuarter() { return area_value / 4; }
private:
string unit;
float area_value;
};
int main() {
Area p {};
string a;
float f;
cout << "Enter a unit with no space: ";
getline(cin, a);
p.setu(a);
cout << "Enter a value of area: ";
cin >> f;
p.seta(f);
cout << "A half of your input = " << p.getHalf() << endl;
cout << "A quarter of your input = " << p.getQuarter() << endl;
return 0;
}