473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Constructors and Destructors

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
2 1805
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
tyreld
144 100+
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

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

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
4
by: François Duranleau | last post by:
Hi! I was writing some piece of code and then, after pondering on some readings in "More Effective C++" by Scott Meyers (Item 10) and the book of Stroustrup (section 14.4.1 in the hardcover...
3
by: Amit | last post by:
is there anything like static constructors or destructors in C++ ? if yes, how to implement it? Thanks, Amit.
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
4
by: rahul8143 | last post by:
hello, i want to know that is virtual constructors possible? if not why? also does virtual destructors functions are often used in C++ programming. regards, rahul.
5
by: ElanKathir | last post by:
Hi All ! I want to know about the constructors & Destructors, constructors ---> New Destructors --> What ? (Which keyword to use for Destructor) Thanks & Regards, ElanKathir.S.N, ASM...
5
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in...
21
by: Michael Hull | last post by:
Hi, I remember from somewhere reading that inlining constructors is a 'BadThing', but now can't seem to find the original source. I can't however thing of a reason why it would be for simple...
3
by: avinash6174 | last post by:
I could complie my code in one directory and the same piece of code in the other directory I couldnt complie the code.. I am working on the same machine .. Its giving an eror saying as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.