Thread: Classes and objects

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    24

    Classes and objects

    Hi,

    Im really new to all this so apologies for any stupid questions but basically I have a program I am making.
    As its for a coursework piece I wont go into detail, however put simply there is a class, which I will call house.

    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
    
    }
    etc.
    What im asking is if there is anyway to avoid putting the

    Code:
    house house;
    bit at the start of each function?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, "house house;" is horrible. You don't name object instances the same thing as the class name. Other than that, what is it you're trying to do exactly? You're not being very clear.

    Do you have an actual working (or non working) example of what you're really doing?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    82
    What is the point of this "house house" thing?

    Within a class' method, such as menu(), you have access to all of the class member data and methods, so why would you need to make a new object of the type house?

    Each method acts on its own object, so if you declared a new object, you're just operating on a different, default object which will disappear as soon as the function returns.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Apologies.
    That was a fairly dire explanation by myself

    ok.
    So if I had a header file called shop. Inside it was a definition for a class shop

    Code:
    class shop  {
    public:
    
    	void menu();
            void select(int choice);
            void function();
    
    };
    Now I have a main.cpp. The program itself.
    Inside that I have a function for menu and for a menu selection.

    Code:
    void shop::select(int choice)
    {
    shop shop;
    
    
    		if (choice == 1)
    		{
    		shop.function();
    		}
    
    shop.menu();
    }
    
    void shop::menu()
    {
    
    	shop shop;
    			int choice;
    	cout << "\nMenu for shop\n";
    	cout << "1 - Option 1\n";
    	cout << "Please make a selection - ";
    	cin >> choice;
    	shop.select(choice);
    
    }
    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
    Code:
    shop shop;
    the compiler throws back errors stating that;

    error C2143: syntax error : missing ';' before '.'

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You don't need to declare an instance of the class in each function. For example:
    Code:
    void shop::select(int choice)
    {
    
    		if (choice == 1)
    		{
    		  function();
    		}
    
    menu();
    }
    Then inside main you might do something like this:
    Code:
    int main()
    {
      shop objA; //the name of the object can't be the same as the class name
      int choice;
    
      objA.menu();
    }
    Does that clear things up at all?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Thank you

    I just had it a bit mixed up in my head.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Ok, while ive solved one issue, its created another which I cant get my head around.

    Code:
    void shop::select(int choice)
    {
    
    
    
    		if (choice == 1)
    		{
    		function();
    		}
    
    menu();
    }
    
    void shop::menu()
    {
    
    			int choice;
    	cout << "\nMenu for shop\n";
    	cout << "1 - Option 1\n";
    	cout << "Please make a selection - ";
    	cin >> choice;
    	select(choice);
    
    }
    the above is an example of the modified code, now if I add a new header file and change my original header file to...

    Code:
    class shop  {
    public:
    
    	void menu();
            void select(int choice);
    
    };
    
    
    class stall {
    public:
    
        void function();
    
    };
    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

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, your functions are recursive, and I doubt you want that. Next, to allow access to something outside the class itself, you simply make it public.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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;
    }
    Last edited by 7stud; 04-04-2005 at 03:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 08:02 AM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  3. static classes vs objects
    By earnshaw in forum C# Programming
    Replies: 5
    Last Post: 02-08-2006, 03:19 PM
  4. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 11:01 PM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM