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

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;
}

Related

C++ Member Initializer Lists Problem: Declaration Order on Private Member Variables

This is my first ever question posted on Stack Overflow, hope someone could explain it.
So basically the two classes are the same except the order of the private member variables. The output is 10 20 10 0 I cannot understand why the order of declaration affects the output.
#include <iostream>
using namespace std;
class MyClass{
public:
MyClass(int value): b(value), a(b * 2){
cout << b << " " << a << " ";
}
private:
int b;
int a;
};
class YourClass{
public:
YourClass(int value): d(value), c(d * 2){
cout << d << " " << c << " ";
}
private:
int c;
int d;
};
int main(){
MyClass obj(10);
YourClass OBJ(10);
}
Class members are initialized in the order of their declaration.
Initialization Order of Class Data Members
#include <iostream>
class MyClass{
public:
MyClass(int value): b(value++), a(value++){
std::cout << b << " " << a << " " << std::endl;
}
private:
int b;
int a;
};
class YourClass{
public:
YourClass(int value): b(value++), a(value++){
std::cout << b << " " << a << " " << std::endl;
}
private:
int a;
int b;
};
int main(){
MyClass obj(10);
YourClass OBJ(10);
}
Output:
10 11
11 10

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;
}

Derived class hiding base class same name method

I was reading this article and it states
This calls Derived::f( complex ). Why? Well, remember that
Derived doesn't declare "using Base:f;", and so clearly Base::f( int )
and Base::f( double ) can't be called.
I decided to try this out and used this code
class Base {
public:
virtual void f( int ) {
cout << "Base::f(int)" << endl;
}
virtual void f( double ) {
cout << "Base::f(double)" << endl;
}
virtual void g( int i = 10 ) {
cout << i << endl;
}
};
class Derived: public Base {
using Base::f;
public:
void f( complex<double> ) {
cout << "Derived::f(complex)" << endl;
}
void g( int i = 20 ) {
cout << "Derived::g() " << i << endl;
}
};
int main() {
Derived d;
d.f(1.0);
}
I get the error
main.cpp: In function 'int main()':
main.cpp:43:16: error: 'virtual void Base::f(double)' is inaccessible within this context
43 | d.f(1.0);
My question is how do I use using Base::f; and how do I fix this issue ?
Simple. You should write the 'using' declaration under public section of class as below. If you put the declaration in private section, Base functions are available to Derived class but they will become private members of Derived class. That is why your compiler is throwing 'inaccessible' error.
#include <iostream>
#include <complex>
using namespace std;
class Base {
public:
virtual void f( int ) {
cout << "Base::f(int)" << endl;
}
virtual void f( double ) {
cout << "Base::f(double)" << endl;
}
virtual void g( int i = 10 ) {
cout << i << endl;
}
};
class Derived: public Base {
public:
using Base::f;
void f( complex<double> ) {
cout << "Derived::f(complex)" << endl;
}
void g( int i = 20 ) {
cout << "Derived::g() " << i << endl;
}
};
int main() {
Derived d;
d.f(1.0);
}

Friend of the class is inaccesible

i am trying to write a very simple code as a practice. The problem is when i make friend a member function of one class to another, it says inaccesible but when i declare the whole class as friend of another class it works fine.
#include <iostream>
using namespace std;
class gpa2;
class gpa1 {
private:
int no1;
int no2;
public:
void setnum1(int n1, gpa2&xp) {
cout << " the friend member function is : " << xp.no4;
}
void setnum2(int n2) {
no2 = n2;
cout << "num2 is : " << no2 << endl;
};
};
class gpa2 {
private:
int no3;
int no4;
friend void gpa1::setnum1(int, gpa2&);
public:
void setnum3(int n3) {
no3 = n3;
cout << "num3 is : " << no3 << endl;
}
void getnum4(int n4) {
cout << "num4 is : " << n4 << endl;
}
};
int main() {
gpa1 g1;
gpa2 g2;
g1.setnum1(15, g2);
g1.setnum2(30);
g2.setnum3(45);
g2.getnum4(50);
return 0;
}
xp.no4 is not accessible, as setnum1 is a member function of gpa1, not gpa2
Implementing the function setnum1(int, gpa2&) is not necessary when defining it, and causing problems here: At the time of defining class gpa1 setnum1 can't be implemented, as class gpa2 is not yet defined. BUT implementing it after defining gpa2 is no problem at all.
Therefore, with some small changes: Godbolt example
#include <iostream>
using namespace std;
// forward declaration
class gpa2;
class gpa1 {
private:
int no1;
int no2;
public:
void setnum1(int n1, gpa2& xp);
void setnum2(int n2) {
no2 = n2;
cout << "num2 is : " << no2 << endl;
};
};
class gpa2 {
private:
int no3;
int no4;
friend void setnum1(int, gpa2&);
public:
void setnum3(int n3) {
no3 = n3;
cout << "num3 is : " << no3 << endl;
}
void getnum4(int n4) {
cout << "num4 is : " << n4 << endl;
}
int num4() { return no4; } // added accesibility to no4
};
// implementation of setnum1
void gpa1::setnum1(int n1, gpa2& xp) {
cout << " the friend member function is : " << xp.num4();
}
int main() {
gpa1 g1;
gpa2 g2;
g1.setnum1(15, g2);
g1.setnum2(30);
g2.setnum3(45);
g2.getnum4(50);
return 0;
}

Changing values in arrays of a different class

(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.