Get and Set function c++ - c++

i just wanted to know if someone could help me about my code,i am a little confused to why it is not working like i want it to maybe i am misunderstanding something...The point of the program is to write a class with two functions to set and get a number but later on the main part of the code i have wanted it to print out a 2.52 number not just the number 2.Thank you if anyone helps :) .
#include <iostream>
#include<conio.h>
using namespace std;
class Class
{
public:
void Set(float x)
{
number = x;
}
int Get()
{
return number;
}
private:
float number;
};
int main()
{
Class object;
object.Set(2.52);
cout << "The number is: " << object.Get();
return 0;
}

First of all, you return an int from Get() so number will be converted in an int.
You should also make Get() const since it's will not change anything in the Class object when you call the function. Making it const makes it possible to pass instances of Class to functions taking a Class by const&:
#include <iostream>
class Class
{
public:
void Set(float x)
{
number = x;
}
float Get() const // returning float and added const
{
return number;
}
private:
float number;
};
void tester(const Class& obj) // a function taking a Class by const reference:
{
std::cout << "The number is: " << obj.Get() << '\n';
}
int main()
{
Class object;
object.Set(2.52);
tester(object);
}
Without the added const compilation would fail.

you can change get method type(float) like that
class Class
{
public:
void Set(float x)
{
number = x;
}
float Get()
{
return number;
}
private:
float number;
};
int main()
{
Class object;
object.Set(2.52);
cout << "The number is: " << object.Get();
return 0;
}

Related

Stored objects in a vector, when are they being destroyed?

Working on a small role-playing game battle system to practice object-oriented code. I have a Party class, which has a vector to store a variable amount of party members.
Initializing my party and adding a member seems to work great. I'm even able to call the member's take_damage() function to change the member's hp and that seems to work too.
But when I check on the same member's hp on the next line using the hp getter, it's right back where it started.
I made a destructor for the member class to see what was going on and according to the output the object is being destroyed many times. Why is this?
class Member
{
private:
int hp;
public:
Member() { hp = 1; }
~Member() { std::cout << "DESTROYED!!" << std::endl; }
int get_hp() { return hp; }
void take_damage(int amt) { hp += amt }
};
class Party {
private:
std::vector<Member> members;
public:
void add_member(Member memb) { members.push_back(memb); }
Member get_member(int num) { return members[num]; }
};
int main() {
Party p;
Member m;
p.add_member(m);
std::cout << p.get_member(0).get_hp() << std::endl;
p.get_member(0).take_damage(4);
std::cout << p.get_member(0).get_hp() << std::endl;
}
Your get_member method returns a copy of the array element, rather than a reference to it. Return a reference, and the member will be modified.
Member& get_member(int num) { return members[num]; }
see small issue in code -=amt
also changed function of get_member
other than that, the code seems fine.
class Member
{
private:
int hp;
public:
Member() { hp = 1; }
~Member() { std::cout << "DESTROYED!!" << std::endl; }
int get_hp() { return hp; }
void take_damage(int amt) { hp -= amt } // This should be -=amt, not +=amt
};
class Party {
private:
std::vector<Member> members;
public:
void add_member(Member memb) { members.push_back(memb); }
Member& get_member(int num) { return members[num]; }
};
int main() {
Party p;
Member m;
p.add_member(m);
std::cout << p.get_member(0).get_hp() << std::endl;
p.get_member(0).take_damage(4);
std::cout << p.get_member(0).get_hp() << std::endl;
}

Related to classes and why the code is giving an error

I want to add two numbers using OOP way. I am a complete newbie in C++ and hence need your help.
#include <iostream>
#include <string>
using namespace std;
class RunwalsClass{
public: // public function
void setName(string x){
name = x;
}
string getName(){
return name;
};
private: // good programming practice to make it private
string name;
};
class MyClass{
public:
void setSaying(string y){
CoolSaying = y;
}
string getSaying(){
return CoolSaying;
}
private:
string CoolSaying;
};
class FavSitcom{
public:
void setSitcom(string z){
BreakingBad = z;
}
string getSitcom(){
return BreakingBad;
}
private:
string BreakingBad;
};
class AddClass{
public:
void setNumbers(int a, int b){
int answer = a + b;
}
int getAddition(){
return answer;
}
private:
int answer;
};
int main(){
RunwalsClass RunwalsObject;
RunwalsObject.setName("Sir Buckey Wallace");
cout << RunwalsObject.getName() << endl;
MyClass MyObject;
MyObject.setSaying("Preaching to the choir! \n");
cout << MyObject.getSaying();
FavSitcom MyNewObject;
MyNewObject.setSitcom("My favorite Sitcom is: Breaking Bad \n");
cout << MyNewObject.getSitcom();
AddClass NewObject;
NewObject.setNumbers("answer: \n");
cout << AddObject.getAddition();
return 0;
}
error: #include <iostream>
#include <string>
using namespace std;
class RunwalsClass{
public: // public function
void setName(string x){
name = x;
}
string getName(){
return name;
};
private: // good programming practice to make it private
string name;
};
class MyClass{
public:
void setSaying(string y){
CoolSaying = y;
}
string getSaying(){
return CoolSaying;
}
private:
string CoolSaying;
};
class FavSitcom{
public:
void setSitcom(string z){
BreakingBad = z;
}
string getSitcom(){
return BreakingBad;
}
private:
string BreakingBad;
};
class AddClass{
public:
void setNumbers(int a, int b){
int answer = a + b;
}
int getAddition(){
return answer;
}
private:
int answer;
};
int main(){
RunwalsClass RunwalsObject;
RunwalsObject.setName("Sir Buckey Wallace");
cout << RunwalsObject.getName() << endl;
MyClass MyObject;
MyObject.setSaying("Preaching to the choir! \n");
cout << MyObject.getSaying();
FavSitcom MyNewObject;
MyNewObject.setSitcom("My favorite Sitcom is: Breaking Bad \n");
cout << MyNewObject.getSitcom();
AddClass NewObject;
NewObject.setNumbers("answer: \n");
cout << AddObject.getAddition();
return 0;
}
Reported error:
error: no matching function for call to 'AddClass::setNumbers(const char [10])
note: candidate: void AddClass::setNumbers(int, int)
note: candidate expects 2 arguments, 1 provided.
In main, your are passing a string as parameter to your setNumbers method. This line is wrong in main:
NewObject.setNumbers("answer: \n");
Your setNumbers function neeeds 2 integers as it is declared. Try:
int a = 10;
int b = 5;
NewObject.setNumbers(a, b);
Good luck in your OOP learning journey!
EDIT:
Also, in your setNumbers function, you must not redeclare answer because this variable is a class member. Remove int, just use answer inside setNumbers.
Yep so your function setNumbers() expects two integer arguments provided, i.e. NewObject.setNumbers(5, 10); which would set the number to 15. You have provided a string literal "answer: \n" which is not the same and therefore will not compile.

C++ pass a function in a class by reference

I'm trying to pass a reference to a function in a class but am having trouble figuring out how to do it. So say I have a class test defined as such
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int);
int getA (void);
private:
int a;
};
void test::setA (int A) { a = A; }
int test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
T.setA(5);
cout << "a = " << T.getA() << endl;
return 0;
}
That works fine but if I want to pass the values by reference
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int);
int & getA (void);
private:
int a;
};
void test::setA (int & A) { a = A; }
int & test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
T.setA(5);
cout << "a = " << T.getA() << endl;
return 0;
}
I cannot figure out how to configure setA to pass by reference.
There are two issues with the code. First, the definition of setA does not match the declaration. You must make the declaration take in a reference as a parameter.
Change this:
void setA (int);
To this:
void setA (int&);
The second issue is that you are trying to pass an r-value (5) as a reference. You must pass in an l-value. You can do that by creating an int first and then passing that by reference:
int i = 5;
T.setA(i);
Full example:
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int&);
int & getA (void);
private:
int a;
};
void test::setA (int & A) { a = A; }
int & test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
int i = 5;
T.setA(i);
cout << "a = " << T.getA() << endl;
return 0;
}
When you pass something by reference to a function in C++, the function does not keep the parameter in memory automatically. Thus, you have to declare it before so that it stays in memory throughout the entire function.
The 5 you tried to pass as a reference would go out of scope and get destroyed as soon as the function starts. The declared i variable is instead destroyed at the end of the main function.
The reason is because in order to pass by reference, you must have an lvalue, which is a fancy way of saying something that persists beyond a single use.
If you created an int variable, you would be able to pass it in by reference. In the code above, you attempted to pass in a raw integer value (5), which fails, since the compiler is expecting a reference to an int, not a raw integer value.
The following code would work:
int main ()
{
test T;
int myVariable = 4; // Need an actual variable to pass by reference.
T.setA(myVariable);
cout << "a = " << T.getA() << endl;
return 0;
}
However, if you want your function to take raw integer values like you showed in your second example, you must have a function definition like your first example, where all the function takes is an integer. Hope this helps!
Maybe you could try this:
#include <iostream>
class test {
public:
test() {};
~test() {};
void setA(int&&); // requires at least C++11
void setA(int&);
int & getA(void);
private:
int a;
};
void test::setA(int && A) { a = A; }
void test::setA(int&A) { a = A; }
int & test::getA(void) { return a; }
using namespace std;
int main()
{
test T;
int i = 5;
T.setA(i);
cout << "a = " << T.getA() << endl;
T.setA(8);
cout << "a = " << T.getA() << endl;
return 0;
}
In the example, int& passes a l-value while int&& passes a r-value as a reference.

Can I override a string return type functions in c++

I cannot understand why this does not compile:
#include<iostream>
#include<string>
using namespace std;
class Product {
public:
virtual void print() = 0;
virtual void slog() = 0;
virtual void loft() = 0;
};
class Bike: public Product {
private:
string s;
public:
Bike(string x){
s = x;
}
void print() {
std::cout << "Bike";
}
int slog() {
return 4;
}
string loft() {
return s;
}
};
int main() {
string s("Hello");
Product *p = new Bike(s);
p->print(); //works fine
cout << p->slog();//works fine
cout << p->loft(); //error
return 0;
}
The above code results in error. Why can't I override string class.
I want to call loft() using the pointer p.
Is there any way to achieve this using pointer object to abstract class Product
Firstly, you need to include string #include <string>.
There's no problem with loft method, you have a problem with print method. Child class has a return type of string and base class has a return type of void, thus you're not really overriding the function. Compiler sees the declaration of void print() in base class and you can't do a cout on that.
Here's your code with few fixes and comments on them, it should work fine.
#include <iostream>
#include <string>
using namespace std;
class Product {
public:
virtual void print() = 0;
virtual int slog() = 0;
virtual string loft() = 0;
//added virtual destructor so you can use pointer to base class for child classes correctly
virtual ~Product() {};
};
class Bike: public Product {
string s;
public:
Bike(string x) {
s = x;
}
void print() {
cout << "Bike";
}
int slog() {
return 4;
}
string loft() {
return s;
}
};
int main() {
string s("Hello");
Product *p = new Bike(s);
p->print();
cout << p->slog();
cout << p->loft();
return 0;
}
Also, please try to format your code better next time, it makes it easier to read

Passing class reference to a void function gives error

I'm trying to pass a reference of a class through a void function, but it throws an error.
Here is the code (it has to be a void function and not return anything). If I change the function to return int or string it works fine but I don't want to do that.
#include <iostream>
using namespace std;
class car
{
public:
car()
: wheels(4)
{
}
int wheels;
};
void getwheels( car& i_car )
{
//do something here
}
int main()
{
car mycar;
mycar.wheels = 6;
cout << getwheels( mycar )<< endl;
}
The void is the problem.
getwheels returns void, but you're printing it out as if it has a return value. If a function returns nothing, you can't print the result of calling it.
To solve, just call the function without printing:
getwheels( my_car );
Or if what you meant to do was print out the wheels value, print the value inside the function:
void getwheels(car& i_car)
{
cout << i_car.wheels << endl;
}
Try to return wheels from getwheels instead of void
int getwheels(const car& i_car)
{
return i_car.wheels;
}
Or pass std::ostream into getwheels:
std::ostream& getwheels(std::ostream& out, const car& i_car)
{
//do something here
out << i_car.wheels << std::endl;;
return out;
}
int main()
{
car mycar;
mycar.wheels = 6;
getwheels(std::cout, mycar);
}