Class help (understanding)

This is a discussion on Class help (understanding) within the C++ Programming forums, part of the General Programming Boards category; Ok, im working on classes (since i abandoned my MUD codebase until i can actualy use C++) but im a ...

  1. #1
    ------------
    Join Date
    Jun 2005
    Posts
    79

    Class help (understanding)

    Ok, im working on classes (since i abandoned my MUD codebase until i can actualy use C++) but im a tiny bit confused on the constructor and destructor... i dont know how to add arguments yet so im not worried about that on the constructor, but does the program automatically call those functions? or do i have to put something to stop them myself? here is an example i just made using them

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Montez {
    	public:
    		Montez();
    		~Montez();
    		void talk();
    		int number;
    };
    
    Montez::Montez() {
    
    }
    
    Montez::~Montez() {
    }
    
    void talk(Montez &M) {
    	cout<<"TADA! it works! "<<endl;
    	cout<<"And, your number was "<<M.number<<". "<<endl;
    }
    
    int main() {
    	Montez Monte;
    
    	cout<<"Enter a number: ";
    	cin>>Monte.number;
    	cin.get();
    	talk(Monte);
    }
    i also did some practice on using functions to call structures and it apparently works for classes too... so is there a way i have to call the destructor or constructor? or is there anywere in that (small) code that i messed up on? thanks for the help C++ is starting to get easier...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Constructors and destructors are never called until an object is created. Adding parameters to contstructors and destructors is the same as it is for regular functions. When you create an object, the constructor is called. It's often used to initialize variables in the class. A destructor is called when an object is deleted. Try this example code:

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    class Cat{
       private:
         int itsAge;
       public:
         int GetAge(){return itsAge;}
         Cat(int age);
         ~Cat();
    };
    
    Cat::Cat(int age){
        cout << "Hey, you created a cat!\n";
        itsAge=age;
    }
    Cat::~Cat(){
        cout << "Cat object deleted.\n";
    }
    
    int main(void){
    /*Create a pointer to a Cat named Frisky; 
    pass 5 for a parameter to the constructor.*/
        Cat *Frisky = new Cat(5);
        cout << "The cat is "<<Frisky->GetAge()<< " years old.\n";
        delete Frisky; //Bye bye, kitty!
        cin.get();
        return 0;
    }
    Last edited by Krak; 07-11-2005 at 09:02 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    The constructor is called automatically when you create a new instance of an object. This line of code creates an instance of Montez on the stack and causes a call to the constructor:

    Montez Monte;

    The destructor is called automatically when an object on the stack goes out of scope. When the main function ends, the Monte variable goes out of scope and the destructor is called.

    In C++, structs and classes are the same except structs default to public access. Whatever worked for structures will work for classes and vice-versa.

  4. #4
    ------------
    Join Date
    Jun 2005
    Posts
    79
    so it would be easier to put the classes in a header file and just not use structures? or can structures be used in header files too? i havent seem any like that yet... (BTW ive never made my own header file, im just guessing from looking at other headers...)
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  5. #5
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, here is another one, but im getting errors (i know its trying to call the class in the function but im not getting it... i called it exactly like i did the last one...)

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Person {
    	public:
    		Person();
    		~Person();
    		string name;
    		int age;
    		string hobby;
    		int siblings;
    		void results();
    };
    
    Person::Person() {
    }
    
    Person::~Person() {
    }
    
    int main() {
    	Person Perso;
    	
    	cout<<"Whats your name? ";
    	cin>>Perso.name;
    	cin.get();
    	cout<<"How old are you? ";
    	cin>>Perso.age;
    	cin.get();
    	cout<<"Whats your hobby? ";
    	cin>>Perso.hobby;
    	cin.get();
    	cout<<"How many siblings do you have? ";
    	cin>>Perso.siblings;
    	cin.get();
    	results(Person);
    }
    
    void results(Person &P) {
    	cout<<"Your name is: "<<P.name<<". "<<endl;
    	cout<<"You are "<<P.age<<" years old. "<<endl;
    	cout<<"Your hobby is "<<P.hobby<<". "<<endl;
    	cout<<"You have "<<P.siblings<<" siblings. "<<endl;
    }
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Goosie
    so it would be easier to put the classes in a header file and just not use structures? or can structures be used in header files too?
    Classes and structures are identical in C++ except:
    a) The default access specifier for a class is private.
    b) The default access specifier for a struct is public.

    Which gives us:
    Code:
    class Foo
    {
        int privatevariable;
    };
    
    struct Bar
    {
        int publicvariable;
    };
    Other than that, they're identical.


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

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Goosie
    Code:
    class Person {
    	public:
    		Person();
    		~Person();
    		string name;
    		int age;
    		string hobby;
    		int siblings;
    		void results();
    };
    
    ...snip...
    
    	results(Person);
    }
    
    void results(Person &P) {
    	cout<<"Your name is: "<<P.name<<". "<<endl;
    	cout<<"You are "<<P.age<<" years old. "<<endl;
    	cout<<"Your hobby is "<<P.hobby<<". "<<endl;
    	cout<<"You have "<<P.siblings<<" siblings. "<<endl;
    }
    For starters, you're saying that your class has a member function (method if you will) called 'results'. However, you don't actually make said method for it. Next you declare a function and try to use it, but you haven't prototyped it any place.

    Assuming you actually wanted 'results' to be a method of your class, it would go something like this:
    Code:
    Person::results( )
    {
        cout<<"Your name is: "<<name<<". "<<endl;
        cout<<"You are "<<age<<" years old. "<<endl;
        cout<<"Your hobby is "<<hobby<<". "<<endl;
        cout<<"You have "<<siblings<<" siblings. "<<endl;
    }
    
    ...
    
    Perso.results(); //call this method of this class instance 'Perso'

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

  8. #8
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ^didnt work, so I tried taking it out of the class and i still get that error... hhmmm...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  9. #9
    ------------
    Join Date
    Jun 2005
    Posts
    79
    oh yea, this is the error i get... sorry, i forgot to post it...

    Code:
    [montez@localhost montez]$ c++ class.cpp -o class -O2 -Wall
    class.cpp: In function `int main()':
    class.cpp:5: error: too many arguments to function `void results()'
    class.cpp:40: error: at this point in file
    EDIT:
    oh yea, and thats after i changed the code to this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void results();
    
    class Person {
    	public:
    		Person();
    		~Person();
    		string name;
    		int age;
    		string hobby;
    		int siblings;
    };
    
    Person::Person() {
    	cout<<"Constructor started... "<<endl;
    }
    
    Person::~Person() {
    	cout<<"YOU KILLED IT!!! "<<endl;
    }
    
    int main() {
    	Person Perso;
    	
    	cout<<"Whats your name? ";
    	cin>>Perso.name;
    	cin.get();
    	cout<<"How old are you? ";
    	cin>>Perso.age;
    	cin.get();
    	cout<<"Whats your hobby? ";
    	cin>>Perso.hobby;
    	cin.get();
    	cout<<"How many siblings do you have? ";
    	cin>>Perso.siblings;
    	cin.get();
    	results(Perso);
    }
    
    void results(Person &P) {
    	cout<<"Your name is: "<<P.name<<". "<<endl;
    	cout<<"You are "<<P.age<<" years old. "<<endl;
    	cout<<"Your hobby is "<<P.hobby<<". "<<endl;
    	cout<<"You have "<<P.siblings<<" siblings. "<<endl;
    }
    Last edited by Goosie; 07-11-2005 at 09:46 PM. Reason: adding code
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    Result function

    I think that part of your problem is with the results function. The function decleration and the function implementation need to be identical. You have the decleration at the top showing no parameters, while the implementation has a Person parameter.

    Also, you may need #include <string>? not sure though
    Last edited by quizkiwi; 07-11-2005 at 09:55 PM. Reason: Additions

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include <iostream>
    
    using namespace std;
    
    void results(Person&);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    ------------
    Join Date
    Jun 2005
    Posts
    79
    I tried added Person &P in the parameters on the top and it gave a lot more errors than i started with...

    EDIT:
    yours didnt work either stoned... functions seem to really hate me...

    EDIT AGAIN:
    quizkiwi, the string header isnt needed... (i added it to check)... thanks for trying tho everyone...
    Last edited by Goosie; 07-11-2005 at 10:12 PM. Reason: adding
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  13. #13
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, i got it to work, so i saved the copy giving me problems and im going to google it and try to figure out why its not working... but on another copy i made it all 1 function and it works perfectly... thanks for the help everyone!

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Person {
    	public:
    		Person();
    		~Person();
    		string name;
    		int age;
    		string hobby;
    		int siblings;
    };
    
    Person::Person() {
    	cout<<"Constructor started... "<<endl;
    }
    
    Person::~Person() {
    	cout<<"YOU KILLED IT!!! "<<endl;
    }
    
    int main() {
    	Person Perso;
    
    	cout<<"Whats your name? ";
    	cin>>Perso.name;
    	cin.get();
    	cout<<"How old are you? ";
    	cin>>Perso.age;
    	cin.get();
    	cout<<"Whats your hobby? ";
    	cin>>Perso.hobby;
    	cin.get();
    	cout<<"How many siblings do you have? ";
    	cin>>Perso.siblings;
    	cin.get();
    	cout<<endl<<endl<<endl<<endl;
    	
    	cout<<"Your name is: "<<Perso.name<<". "<<endl;
    	cout<<"You are "<<Perso.age<<" years old. "<<endl;
    	cout<<"Your hobby is "<<Perso.hobby<<". "<<endl;
    	cout<<"You have "<<Perso.siblings<<" siblings. "<<endl;
    }
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  14. #14
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    << operator

    Sorry, i did not look at it very closely, here is the functioning code with the exception of overloading the '<<' and '>>' operators...I am assuming you have already done that?

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    
    class Person {
    	public:
    		Person();
    		~Person();
    		string name;
    		int age;
    		string hobby;
    		int siblings;
    };
    
    Person::Person() {
    	cout<<"Constructor started... "<<endl;
    }
    
    Person::~Person() {
    	cout<<"YOU KILLED IT!!! "<<endl;
    }
    
    void results(Person &P);
    
    int main() {
    	Person Perso;
    	
    	cout<<"Whats your name? ";
    	cin>>Perso.name;
    	cin.get();
    	cout<<"How old are you? ";
    	cin>>Perso.age;
    	cin.get();
    	cout<<"Whats your hobby? ";
    	cin>>Perso.hobby;
    	cin.get();
    	cout<<"How many siblings do you have? ";
    	cin>>Perso.siblings;
    	cin.get();
    	results(Perso);
    }
    
    void results(Person &P) {
    	cout<<"Your name is: "<<P.name<<". "<<endl;
    	cout<<"You are "<<P.age<<" years old. "<<endl;
    	cout<<"Your hobby is "<<P.hobby<<". "<<endl;
    	cout<<"You have "<<P.siblings<<" siblings. "<<endl;
    }

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So in other words, exactly what I said was wrong, that you ignored. Rather than fix the function to work correctly, you just threw it out the window.


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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21