Thread: constructor & destructors

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    12

    constructor & destructors

    Hi,


    Can anyone tell me what is the best way to develop classes, the book i am reading has a few ways to develop them and i can't see an obvious reason for using any of them, i am using constructors to access variables but the book had 1 or 2 other ways to do the same thing!

    Which one do i use or is it a case of using the best one suited for the job?

    While i am here, what is the purpose of the destructor?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    > i am using constructors to access variables
    What ? Do you mean initialize ?
    You have to give short examples of the 'ways' you saw, to recieve any meaningful answer.

    Destructors, as their name says, destroy.
    Suppose you have a 'human' object.
    When the human dies, a lot of work has to be done.. (funeral, death certificate ..etc !).
    It is the destructor's responsibility to do that cleanup work.

    Also get familiar with the "Rule of Three", while you're at it.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Quote Originally Posted by manasij7479 View Post
    > i am using constructors to access variables
    What ? Do you mean initialize ?
    You have to give short examples of the 'ways' you saw, to recieve any meaningful answer.

    Destructors, as their name says, destroy.
    Suppose you have a 'human' object.
    When the human dies, a lot of work has to be done.. (funeral, death certificate ..etc !).
    It is the destructor's responsibility to do that cleanup work.

    Also get familiar with the "Rule of Three", while you're at it.

    Termonology at this stage is not my strong suit, i'm not sure i can give you a clear example of what i am trying to say, also, can you please explain why i have to be firmiliar with the "rule of three"?

  4. #4

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    read it, dont get it, should i just quit trying to learn C++!

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    well, posting in this forum certainly helped!!! :/

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class parent{
    private:
        string name;
    public:
    //setter method
        void setName(string Name) { name = Name; }
    //getter method
        string getName() { return name; }
    };
    int main(){
    //declaring object
    parent father;
    //initializing variables (assigning)
    father.setName("John");
    //output to screen
    cout<< father.getName() <<endl;
    }
    or i can do it this way!


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class parent{
    private:
        string name;
    public:
    //constructor & destructor
        parent(string);
        ~parent();
    //getter method
        string getName() { return name; }
    };
    parent::parent(string name){
        this -> name = name;
    };
    parent::~parent(){
    //deconstructor
    };
    int main(){
    // declaring object
        parent john("John");
    //screen output
        cout<< john.getName();
    }

    both ways are basically doing the same thing so which one do i use?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-23-2012, 10:47 PM
  2. Replies: 5
    Last Post: 02-21-2011, 02:19 AM
  3. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  4. Replies: 6
    Last Post: 05-19-2010, 04:03 AM
  5. Replies: 10
    Last Post: 06-02-2008, 08:09 AM