How to call functions in the main function by use class? - class

#include <iostream>
using namespace std;
class Money
{
public:
Money();
Money(int, int);
void setDollars(int d);
void setCents(int c);
int getDollars() const;
int getCents() const;
double getAmount(int, int);
private:
int dollars;
int cents;
};
Money::Money()
{
dollars = 0;
cents = 0;
}
Money::Money(int d, int c)
{
dollars = d;
cents = c;
}
void Money::setDollars(int d)
{
dollars = d;
}
void Money::setCents(int c)
{
if (c > 100)
{
c = c % 100;
dollars = dollars + (c / 100);
}
//update the dollars member if the cents input argument is 100 or larger.
cents = c;
}
int Money::getDollars() const
{
return dollars;
}
int Money::getCents() const
{
return cents;
}
double Money::getAmount(int d, int c)
{
return dollars + (cents / 100);
}
int main()
{
int dlr;
int cts;
//double amt;
cout << "Please input amount of dollars: ";
cin >> dlr;
cout << "Please input amount of cents: ";
cin >> cts;
Money money0(dlr, cts);
cout << "The money object1 has amount: " << money0.getDollars() << "." << money0.getCents() << endl;
cout << "The money object2 has amount: " << money0.getAmount() << endl;
//I try to call the functions to tell user how much is it
//use both ways to tell user how much is it(1.getDollars+getCents, 2. getAmount)
return 0;
system("pause");
}
When I try to call the get functions in the main, the errors show up. How to call those functions correctly?
Here is my assignment:
Define a class named Money that stores a monetary amount. The class should have two private integer variables, one to store the number of dollars and another to store the number of cents. Include a default constructor that initializes the amount to $0.00 and an overloaded parameterized constructor to initialize any non-zero amount(as demonstrated in the example program below).

Related

how to point at multiply struct with one struct pointer in c++

Write a assign_obj.h file so that the assign_driver file will output the
following. When implementing assign_obj.h think about how efficient your program will run. You must
implement your assign_obj class using a dynamic array.
assign_obj:: assign_obj() function will just set size to zero
assign_obj:: assign_obj(std::string s) function will go through each character of string and convert it to uppercase and push it the last of A
std::ostream& operator<<(std::ostream & out, assign_obj & obj) function will go through each data in obj.A vector and convert integer into string and
than output the data in this format [value:count value:count ....]
class assign_obj{
private:
struct item{
char value;
int count;
};
item *A;
int size;
public:
assign_obj();
assign_obj(std::string);
friend std::ostream& operator<<(std::ostream & out, assign_obj & obj);
}
int main(){
assign_obj ao1("dsdfdf");
std::cout << ao1 << std::endl;
}
This is the output that I want
[ D:1 S:1 D:1 F:1 D:1 F:1 ]
assign_obj:: assign_obj(){
size = 0;
}
assign_obj:: assign_obj(std::string s){
size = s.size();
for(int i = 0; i < s.size(); i++){
char c = std::toupper(s[i]);
A = new item{c,1};
}
}
# std::ostream& operator<<(std::ostream & out, assign_obj & obj){
std::cout<< "size: " << obj.size <<std::endl;
out << "[ ";
for(int i = 0; i < obj.size; i++){
out << obj.A[i].value << ":" << std::to_string(obj.A[i].count) << " ";
}
out << "]" << "\n";
return out;
}

When calling a member function of a class, I get Error C3867

In my displayData member function of the car class, the error says I should create a pointer to the member, do I have to create pointers to the member? If I do how so? I completely forget pointers. Do i make the object a pointer then point to the displayData member function? I get no red squiggles just an error message saying "use '&' to create a pointer to the member. I tried but had no luck.
#include<iostream>
#include<string>
using namespace std;
class Car
{
private:
int xVal, yVal, zVal;
protected:
public:
Car() { int xVal = 0; int yVal = 0; int zVal = 0; }
Car(int x,int y,int z) { xVal = x; yVal = y; zVal = z; }
~Car() {};
int getX() { return xVal; }
int getY() { return yVal; }
int getZ() { return zVal; }
void changeX(int n) { xVal = n; }
void changeY(int n) { yVal = n; }
void changez(int n) { zVal = n; }
virtual void getData();
void displayData();
};
class Sensor : public Car
{
private:
string sensorType;
protected:
public:
Sensor() { sensorType = "EMPTY"; }
Sensor(int x, int y, int z, string type) :Car(x,y,z) { sensorType = type; }
};
void Car::displayData()
{
cout << "The x values is: " << getX() << endl;
cout << "The y values is: " << getY() << endl;
cout << "The z values is: " << getZ() << endl;
}
int main()
{
Sensor n1;
Sensor n2(20,30,40, "Accelerometer");
n1.displayData;
n2.displayData;
return 0;
}
There is a syntax error in calling the function.
it should be
n1.displayData();
instead of
n1.displayData;

changing variables within loop from outside class

I have a time class and a Build class. The harvest function increases the minerals count for the entire time, but the nunmber of minerals depends on the number of available workers. As soon as a build starts, the number of workers decreases.
So, how can i decrease the number of workers, within the loop, and not after the loop has finished.
I want minerals to keep increasing, but the number of workers should increase and decrease based on the Build call.
class Time {
private:
int minerals = 50;
int workers = 6;
int vespene = 0;
int supply = 6;
int time = 0;
public:
Time ();
int Time_counter();
void harvest();
int mineral_count();
int worker_count();
int supply_count();
void update(int m, int w, int s, int t);
void Display();
};
Time::Time ()
{
for (int i =0; i<9; i++)
{
update(minerals, workers, supply, time);
harvest();
time = i;
// Time_counter();
}
}
int Time::Time_counter() {
return time;
}
void Time::harvest() {
minerals+= workers * (0.7);
}
int Time:: mineral_count() {
return minerals;
}
int Time::worker_count() {
//return workers.size();
return workers;
}
int Time::supply_count() {
return supply;
}
void Time:: update(int m, int w, int s, int t) {
minerals = m;
workers = w;
supply = s;
time = t;
cout << "No. of Minerals: " <<minerals <<"\n" <<"No.of Workers: " <<workers <<"\n"
<< "No. of Supply: " <<supply <<"\n" <<"Current Time: " <<time << endl;
}
void Time:: Display ()
{
cout << "No. of Minerals: " <<minerals <<"\n" <<"No.of Workers: " <<workers <<"\n"
<< "No. of Supply: " <<supply <<"\n" <<"Current Time: " <<time << endl;
}
class Build: public Time {
public:
void Build_create();
};
void Build::Build_create() {
int build_count=0;
SCV obj;
int m = obj.mineral_count();
int w = obj.worker_count();
int s = obj.supply_count();
int t; // = obj.Time_counter();
if (m >= 50 && biuld_count<2)
{
cout << "Build start....";
m-= 50;
obj.update(m,w,s,t);
obj.Display ();
w-=1;
for (t = obj.Time_counter(); t <obj.Time_counter()+ 10; t++)
{cout<< "Building...";}
cout << " Build complete" << endl;
scv_count++;
w += 1;
s += 1;
t = obj.Time_counter();
obj.update(m, w, s, t);
obj.Display ();
}
else if (m<50)
{
cout << "Cannot build " << endl;
}
else if (build_count==2)
{
cout<<"two already built.." <<endl;
}

C++ Cant Change Object's Variables

class ship {
public:
int location;
int length;
};
void createship(ship ship1, int gridsize) { //function to set ship1's length and location
srand(time(NULL));
ship1.length = (int)((rand()) % gridsize) / 4 + 1;
ship1.location = (int)(rand()) % gridsize;
}
void insertship(ship ship1, vector <char> grid) { //insert ship into grid, change grid's elements from 'e' to 'f'
for (int temp2= 0; temp2 < ship1.length; temp2++) {
grid[ship1.location + temp2] = 'f';
}
}
int main()
{
int gridsize;
cout << "Gridsize: ";
cin >> gridsize;
cout << "\n";
vector <char> grid(gridsize, 'e');
for (int temp3 = 0; temp3 < grid.size(); temp3++) { //cout vector grid
cout << grid[temp3];
}
cout << "\n";
ship* ship1 = new ship(); //create ship1
createship(*ship1, gridsize);
insertship(*ship1, grid);
cout << (*ship1).length << "\n";
cout << (*ship1).location << "\n";
for (int temp4 = 0; temp4 < grid.size(); temp4++) { //cout vector grid again (with ship)
cout << grid[temp4];
}
return 0;
}
My ship1.length and ship1.location always remain as zero, even though the createship() function is supposed to change it to a random figure? Is there any mistake I made?
Just to add more words below because stackexchange doesn't allow me to add mostly code in my question
You were passing a parameter by value which will create and change a local variable in the function instead of the source variable.
You should pass the parameter by reference.
void createship(ship &ship1, int gridsize) {
srand(time(NULL));
ship1.length = (int)((rand()) % gridsize) / 4 + 1;
ship1.location = (int)(rand()) % gridsize;
}
Anyway,in you case, using a member function maybe a better solution.
class ship {
public:
int location;
int length;
void createship(int gridsize) {
srand(time(NULL));
this->length = (int)((rand()) % gridsize) / 4 + 1;
this->location = (int)(rand()) % gridsize;
}
};
call:
ship1->createship(100);
You pass the ship by value, so the parameter createship gets is a copy of the original object and changing it won't change the original.
Pass a reference/pointer to ship and then changes to the parameter will change the original object.
Or much better, use a constructor.

how to print to screen a vector?

I'm implementing the show Deal Or No Deal, there's a class 'box' which in the main file i used to store the random values of the boxes and than, i saved each box in the vector. im trying now to print in the screen the boxes saved in the vector whit the iterator,without succeeding, any help???
//random assignation of pound value to the 22 boxes
for (int e = 1; e < 23; e++)
{
int pos;
bool op = true;
while (op)
{
pos = rand();
if (pos > 0 && pos < 23)
{
if (myArray[pos][1] == 0)
{
myArray[pos][1] = 1;
op = false;
}
}
}
box b(e, myArray[pos][0]); //creating the class box
game_box.push_back(b); //function of the vector to insert a data in it
}
//show boxes
for (auto a = game_box.begin(); a!= game_box.end(); a++)
{
cout << *a << endl;
}
You first need to remove the dereference operator (*) from the a.
Next you need to add an output operator for box. Assuming that box is a class with member data member1 and member2 this would look something like:
friend std::ostream& operator<< (std::ostream &out, const box& b)
{
out << box.memeber1 << " " << box.member2;
}
The key point is that you need to define this operator for every class.
Once you've done that and got it working you might also like to look at this library which defines << operators for all stl containers. This lets you replace
for (auto a = game_box.begin(); a!= game_box.end(); a++)
{
cout << *a << endl;
}
by simply
cout << game_box << endl;
this works..
class Box
{
float pound_contained;
int box_number;
public:
Box(int box_number, float pound_contained);
int getbox_number();
float getpound_contained();
};
int Box::getbox_number()
{
return this->box_number;
}
float Box::getpound_contained()
{
return this->pound_contained;
}
main()
{
vector<Box> game_box;
Box* boxes = &game_box[i];
cout <<boxes->getbox_number()<<endl;
}