1)
Now everytime I run a function within that class, say a menu function, or a menu selection function.
I have to create an object first.
So all functions look like;
Code:
void house::menu()
{
house house;
code
}
Usually, when a simple function is defined in your class, it does not create any objects. Then, it is called in main() by an object of your class that you create in main(). Here is an example:
Code:
#include <iostream>
#include <string>
using namespace std;
class Apple
{
public:
int size;
string color;
void display()
{
cout<<size<<endl;
cout<<color<<endl;
}
};
int main()
{
Apple myApple; //creates an Apple object
myApple.size = 5;
myApple.color = "red";
myApple.display(); //calls an Apple function
return 0;
}
Hopefully that example will make it clear why the following is nonsensical:
What im asking is if there is anyway to avoid putting the
house house;
bit at the start of each function?
2)
Code:
void shop::select(int choice)
{
shop shop;
if (choice == 1)
{
shop.function();
}
shop.menu();
}
As I said my understanding of this language is limited at the moment so sorry if I am asking an oddly simple question.
If I dont have the line
shop shop;
the compiler throws back errors stating that;
error C2143: syntax error : missing ';' before '.'
The line:
shop shop;
is nonsensical, so get rid of it and try to solve the real problem: a member function of a class can call any other member function in the same class directly, but a member function of one class cannot call a member function of another class. Since select() is a member of the shop class it cannot call function(), which is a member of the stall() class.
3)
Code:
How can I get the menu inside of shop::menu to allow me to call a function inside of stall called function.
Ive tried calling it using the name of the object (say I declare, stall s1 and then using s1.function();
ive also tried stall::function();
I just cant see anyway to get to the function inside of another class
That's the whole point of creating a class--the functions in a class can only be called by objects of that class. Here is an example:
Code:
#include<iostream>
#include<string>
using namespace std;
class Apple
{
public:
int size;
string color;
void display()
{
cout<<size<<endl;
cout<<color<<endl;
}
};
class Car
{
private:
int doors;
int seats;
public:
Car(int d, int s)
{
doors = d;
seats = s;
}
Car()
{
doors = seats = 0;
}
int body_type()
{
return doors + seats;
}
};
int main()
{
Apple my_apple; //create an Apple object called my_apple
my_apple.size = 5;
my_apple.color = "red";
my_apple.display();
Car my_car(4, 6);
cout<<"my_car body type: "<<my_car.body_type()<<endl;
//my_apple.volume(); //error: volume is not a member of Apple class
return 0;
}
Hopefully, you can see why it's nonsensical to call a function from the Car class, i.e body_type() on an Apple object: an Apple object does not have doors, windows, or a body type. Class functions can only be called on objects of that class. Even if a class function just displays a generic message, so it would make sense to call that function with an object of another class, you still can't do it.
You can however create a global function that can be called by any class function:
Code:
#include <iostream>
#include <string>
using namespace std;
void general_greeting()
{
cout<<"This is Program 10"<<endl;
}
class Apple
{
public:
void display_message()
{
general_greeting();
cout<<"Hi from the apple class.\n\n";
}
};
class Car
{
public:
void show_message()
{
general_greeting();
cout<<"Hi from the Car class.\n\n";
}
};
int main()
{
Apple myApple;
myApple.display_message();
Car myCar;
myCar.show_message();
return 0;
}
You can also create classes with members that are objects of another class. Here is an example of that:
Code:
#include <iostream>
#include <string>
using namespace std;
class Apple
{
public:
void display_message()
{
cout<<"I am an Apple.\n"<<endl;
}
};
class Car
{
public:
Apple myApple;
void show_message()
{
cout<<"Hi from the Car class.\n\n";
}
};
int main()
{
Car myCar;
myCar.show_message();
myCar.myApple.display_message();
return 0;
}
Notice that you still can't call the Apple function directly with the Car object--you have to use the Apple object. That is another example of how class functions can only be called with objects from that class.
Of course there is an exception to the rule that only objects from the class can call the functions of that class. You could make function() static, which means it can be called without a stall object. Here is an example:
Code:
#include <iostream>
using namespace std;
class Apple
{
public:
static void display_message()
{
cout<<"I am an Apple.\n"<<endl;
}
};
class Car
{
public:
void show_message()
{
Apple::display_message(); //You use the generic class name to call a static function
cout<<"Hi from the Car class.\n\n";
}
};
int main()
{
Car myCar;
myCar.show_message();
return 0;
}