By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

Constructors and Destructors

P: 1
I need help understand what constructors and destructors do. I am reading my book and I just can't understand why they are in this program. Here is the code to the program the first set up code is the header file and the second is the cpp file. So what I am asking is what is Cat(int initialAge); doing. and then the constructor of cat.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //setAge
  5. //getAge
  6. //itsAge -- private
  7.  
  8.  
  9.  
  10. class Cat
  11. {
  12. public:
  13.     Cat ( int initalAge );
  14.     ~Cat();
  15.     int getAge() const {return itsAge;}
  16.     void setAge(int age) {itsAge = age;}
  17.     void Meow() const {cout << "STFU CAT\n";}
  18. private:
  19.     int itsAge;
  20. };
  21.  
HERE IS THE CPP FILE

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "Fun.h"
  3. using namespace std;
  4.  
  5. Cat::Cat(int initialAge)
  6. {
  7.     itsAge = initialAge;
  8. }
  9.  
  10. Cat::~Cat(){}
  11.  
  12. int main()
  13. {
  14.     int z;
  15.  
  16.     Cat Frisky(5);
  17.     Frisky.Meow();
  18.  
  19.     cout << "Frisky is a cat who is ";
  20.     cout << Frisky.getAge() << " years old.\n";
  21.     Frisky.Meow();
  22.     Frisky.setAge(7);
  23.     cout << "Now the dumb cat is: " << Frisky.getAge() << " years old.\n";
  24.     cin >> z;
  25.     return 0;
  26. }
  27.  
THanks for your time.
Sep 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: 22
Basically Constructors are there to instantiate an object of a defined class. in your example the class is Cat. What this mean is, when your program calls Cat Frisky(5); in the main method, a new instance of Cat is created. How this is done is pretty simple , remember the
Cat::Cat(int initialAge) part of your cpp file? This is the code that is executed when you tell the program to do Cat Frisky(5). Frisky is the name of the variable that contains the new Cat object. and the 5 is the argument passed to the constructor Cat::Cat(5).
Sep 21 '06 #2

100+
P: 144
Constructors are used to initialize an object when it is first instantiated. In this example a Cat object is created and the private variable age is given an initial value of 5. The public methods provide an interface that allow you to act on the object (ie. setting or getting the value of the cats age).

Destructors are called when an object is destroyed. Destructors are used to perform cleanup. Generally, this involves freeing any memory that was dynamically allocated by the object during its lifetime. In this example the destructor doesn't do anything.
Sep 21 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.