How to change value of public data fields in C++? [closed] - c++

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 am trying to learn C++ .I want to to change the value of public member data2. In the second cout statement why I am not getting 10.5 value?
class Test
{
private:
int data1;
public:
float data2;
void function1()
{
data1=2;
}
float function2()
{
data2=3.5;
return data2;
}
};
int main()
{
Test obj2;
Test obj1;
cout<<"value of public member data2 is "<< obj2.function2()<<endl;
obj2.data2=10.5;
cout<<"new value of data2 is " <<obj2.data2<<where as its previous value
was obj2.function2()<<endl;
}

When you run cout << x << b() The compiler can break this down into something looking more logically like:
auto t = b()
cout << x << t
If your x is changed by b() then the modified one is what will be used

Inside funtion2, You are assigning value again:
float function2()
{
data2=3.5;
return data2;
}
Remove data2=3.5; from function2

Related

c++ using bool in a class with set and get functions [closed]

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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am trying to create a class with private attributes and change values with set and get functions. However i cant seem to change the bool value
class VideoGames {
private:
bool buy;
public:
bool get_buy(){return buy;}
void set_buy(bool b){b = buy;}
};
main(){
VideoGames g1;
double price;
cin >> price;
if(price <=100){
g1.set_buy(true);
}
else {
g1.set_buy(false);
}
cout<<"Buy= " << g1.get_buy()<<endl;
return 0;
}
this always prints out 0 no matter what the price is. What do i need to change?
You flip-flopped the assignment; b = buy; reassigns the parameter (which is then thrown away so nothing changes), you wanted buy = b; to reassign the attribute from the parameter.
when using the "=" sign, the right side gets put in the left side, so instead of:
void set_buy(bool b){b = buy;}
write:
void set_buy(bool b){buy = b;}

C++ unexpected output [closed]

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 1 year ago.
Improve this question
I am a C++ beginner
I wonder if there is any chance you might be able to tell me why my code (below) produces unexpected output? It compiles fine.
./a.out produces 4.94066e-324 rather than 1000
Thanks very much. Esther
#include <iostream>
#include <string>
#include <vector>
enum class OrderBookType{bid, ask};
class OrderBookEntry
{
public:
OrderBookEntry(double price, double amount, std::string timestamp, std::string product, OrderBookType orderType)
{}
double price;
double amount;
std::string timestamp;
std::string product;
OrderBookType orderType;
};
int main()
{
while(true)
{
OrderBookEntry order1{1000,0.02,"2020/03/17 17:01:24.884492","BTC/USDT",OrderBookType::bid};
std::cout << order1.price << std::endl;
return 0;`
Your constructor is not initializing the values.
use Initialiser list like this
OrderBookEntry(double price, double amount, std::string timestamp, std::string product, OrderBookType orderType)
: price(price),
amount(amount),
timestamp(timestamp),
product(product),
orderType(orderType)
{
}

non-standard syntax; use '&' to create a pointer to member. Accessing boolean variable [closed]

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've been searching for a while on why this does not work. I have not gotten a clear answer.
Can anyone explain why trying to access this boolean variable and comparing it to another boolean variable won't work?
I also tried setting the rhs of the comparison to 0, and that got rid of the boolean/int error, but I'm still getting the error.
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
setWorking(true);
}
//Mutator
void setWorking(bool x) { working = x; }
//Accessor
bool getWorking() { return working; }
private:
bool working;
};
int main() {
MyClass alpha;
if (alpha.getWorking == true) {
cout << "its working\n";
}
else {
cout << "not working\n";
}
return 0;
}
In main function
if (alpha.getWorking == true)
should be
if (alpha.getWorking())

C++ error: Reference to non-static member function must be called, Please help, don't know whats literally wrong [closed]

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 7 years ago.
Improve this question
class skClass {
public:
void setName(string x) {
name = x;
}
string getName() {
return name;
}
private:
string name;
};
int main() {
skClass sk;
sk.setName = ("Mr Bashir Sentongo");
cout << sk.getName() << endl;
return 0;
}
Methods are called by passing parameters within parenthesis (()), not with the assignment operator (=):
I.e., you should replace
sk.setName = ("Mr Bashir Sentongo");
With
sk.setName("Mr Bashir Sentongo");

C++ "?>" operator [closed]

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 7 years ago.
Improve this question
Could someone explain me what this kind of operator this?> does in C++?
Below example of code with usage of it:
#include <iostream>
#include <string>
using namespace std;
class A {
public:
int x;
};
class B : public A {
public:
B() {x=1;}
B(int x) {this?>x = x;}
};
int main()
{
B c1;
B c2(10);
cout << c1.x;
cout << c2.x;
return 0;
}
I think you try to say that -> becouse ?> it does not exist.
In the contex, im sure that you want ->.
You probably have a mistake when you´re typing.
PD: try to compile before ask
It's a typo. You might want to refer to ->.
this operator is used to access the property of the object calling that function of that class in which the functions is defined, and -> operator is used to access the properties of that object.