Finally.

This is a discussion on Finally. within the C++ Programming forums, part of the General Programming Boards category; I finally learned Classes. Since i finished being comfortable with them today , i decided to make a little application. ...

  1. #1
    kevinawad
    Guest

    Finally.

    I finally learned Classes.

    Since i finished being comfortable with them today , i decided to make a little application.

    Here it is.

    main.cpp
    Code:
    #include "Library.h"
    
    using namespace std;
    
    
    
    int main()
    {
    
    	Dog dog;
    
    	string enterdogname;
    	string enterdogcolor;
    	int enterdogweight;
    
    
    	cout <<"Hello, What's your dog name? "<<endl;
    	cout <<">>";
    	cin >> enterdogname;
    	cout <<"\n\n";
     
    	dog.setName(enterdogname);
    	system("cls");
    
    	cout <<"What color is your dog?"<<endl;
    	cout <<">>";
    	cin >> enterdogcolor;
        cout <<"\n\n";
    
    	dog.setColor(enterdogcolor);
    	system("cls");
    
    	cout <<"What's is the weight of your dog?"<<endl;
    	cout <<">>";
    	cin >> enterdogweight;
    	cout <<"\n\n";
    
    	dog.setWeight(enterdogweight);
    	system("cls");
    
    	cout <<"Dog name: "<<dog.getName()<<endl;
    	cout <<"Dog color: "<<dog.getColor()<<endl;
    	cout <<"Dog weight: "<<dog.getWeight()<<endl;
    	cout <<"Press enter to reset your dog informations";
    	cin.get();
    	cin.get();
        
    	dog.reset();
    	system("cls");
    
    	cout <<"Let's check if your dog informations has been reset.Press enter again."<<endl;
    	cin.get();
    
    	cout <<"Dog name: "<<dog.getName()<<endl;
    	cout <<"Dog color: "<<dog.getColor()<<endl;
    	cout <<"Dog weight: "<<dog.getWeight()<<endl;
        
    	cout <<"BYE";
    
    	cin.get();
    
    
    	return 0;
    
    
    
    
    }
    Library.h

    Code:
    //Lib
    #include <windows.h>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    //Headers
    #include "Dog.h"

    Dog.cpp
    Code:
    #include "Library.h"
    
    
    //Dog name
    
    string Dog::getName()
    {
    	return name;
    }
    
    void Dog::setName(string dogname)
    {
    	name = dogname;
    
    }
    
    //Dog Weight
    
    int Dog::getWeight()
    {
    	return weight;
    }
    
    void Dog::setWeight(int dogweight)
    {
    
    	weight = dogweight;
    }
    
    //Dog Color
    
    string Dog::getColor()
    {
    	return color;
    }
    
    void Dog::setColor(string dogcolor)
    {
    	color = dogcolor;
    
    }
    
    //Destructor/Constructor
    
    Dog::Dog()
    {
    
    }
    
    Dog::~Dog()
    {
    
    }
    
    //reset
    
    int Dog::reset()
    {
    	name = "";
    	color = "";
    	weight = NULL;
    	return true;
    }


    Dog.h

    Code:
    //Dog class
    
    
    class Dog
    {
    
    public:
    
    	 Dog();
    	~Dog();
    
    	//Name
    	string getName();
    	void setName(string dogname);
    	//Weight
    	int getWeight();
    	void setWeight(int dogweight);
    	//Color
    	string getColor();
    	void setColor(string dogcolor);
    
            //reset data
    	int reset(); 
    
    
    private:
    	string name;
    	int weight;
    	string color;
        
    
    };

    This will create the dog, and finish by destroying it. You may compile it and check it. Now, when you checked it. There's a bug huh? It doesnt destroy the weight of the dog. Now, in the destructor, i have decided to put weight = 0;. But, it will show Dog weight: 0 on the screen. I want it to just destroy it forever and don't show it. Can anyone help me. Thank you.
    Last edited by kevinawad; 06-26-2008 at 05:42 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Code:
    	dog.~Dog();
    	system("cls");
    
    	cout <<"Let's check if your dog has been destroyed.Press enter again."<<endl;
    	cin.get();
    
    	cout <<"Dog name: "<<dog.getName()<<endl;
    	cout <<"Dog color: "<<dog.getColor()<<endl;
    	cout <<"Dog weight: "<<dog.getWeight()<<endl;
    I wish that were a syntax error, instead of just undefined behavior. But alas, it's only undefined behavior. Referring to dog after it has been destroyed can't possibly give you anything valid; since you haven't done anything to reuse the memory the old contents are still there.

  3. #3
    kevinawad
    Guest
    Well, it's supposed to destroy the weight, the name, the color and set them to null...since the dog class still exist. But, it doesn't look like it does that. it actually set the name and the color to NULL, but not the weight.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Quote Originally Posted by kevinawad View Post
    Well, it's supposed to destroy the weight, the name, the color and set them to null...since the dog class still exist. But, it doesn't look like it does that. it actually set the name and the color to NULL, but not the weight.
    It doesn't on my system -- the name and the color are still printed after destruction.

    That's the point. The compiler is allowed to do whatever it wants in this situation -- it could play the theme song to "Cheers" through the speakers if it wanted -- because it's not a legal thing to access dog after it's been destroyed.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    >> There's a bug huh? It doesnt destroy the weight of the dog. Now, in the destructor, i have decided to put weight = 0;. But, it will show Dog weight: 0 on the screen. I want it to just destroy it forever and don't show it. Can anyone help me. Thank you.

    first of all, you should *never* explicitly call a destructor (there are exceptions, but they are few and rare). it's generally the compiler's job to do that when the variable goes out of scope. furthermore, once the destructor has been called you *must never* attempt to access the variable's data members in any way (remember: it is effectively 'destroyed'/nonexistant at that point). but to answer your question, it is simply that POD's (Plain Old DataTypes) such as ints don't have a destructor...
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

  6. #6
    kevinawad
    Guest
    Oh, hmm...ill try something else to destroy all the data in dog then.

  7. #7

    Join Date
    Apr 2008
    Location
    USA
    Posts
    78
    Next, try making an abstract class Animal from which you can derive your Dog

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Quote Originally Posted by kevinawad View Post
    Oh, hmm...ill try something else to destroy all the data in dog then.
    Why would you *want* to destroy all the data in dog then? If you don't want the dog variable at all, or you want the dog variable later to have no relation to the dog variable earlier, then use the scope rules (via judicious use of curly brackets) to make sure the variable is gone.

    If your class design requires you for whatever reason to "clean out" a variable, then you should make a reset method.

  9. #9
    kevinawad
    Guest
    Here. put this in the Dog class in Dog.h.(public)

    Code:
    int reset();

    This in Dog.cpp

    Code:
    int Dog::reset()
    {
    	name = "";
    	color = "";
    	weight = NULL;
    	return true;
    }
    Replace dog.~Dog() to dog.reset(); in main.cpp


    Now, it should reset but, it will print weight as 0;
    How is it possible to set an integer to null?

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    isn't zero good enough? anyway, you can simply store a boolean flag to indicate that the data is valid if you're that concerned about it.
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Well, NULL == 0. It is defined as such in the C++ standard.

    And, let me put this in boldface this time: IF the variable dog exists, then the variable dog.weight WILL exist. All the member variables will exist. The only way to get rid of weight is to get rid of dog, and the only way to get rid of dog is to let it go out of scope.

    Let's get rid of classes, since they seem to have gotten in the way, and look at this:
    Code:
    int foo = 3;
    How do you propose to set foo to NULL?

  12. #12
    kevinawad
    Guest
    What do you mean sebastiani? could you show me a little example? Thank you.


    Hmm...isn't there any way to just set it to nothing? like delete the zero?
    Last edited by kevinawad; 06-26-2008 at 05:31 PM.

  13. #13

    Join Date
    Apr 2008
    Location
    USA
    Posts
    78
    Every variable has a value, even if it's bogus (like what you get when you try to access invalid memory).
    There's no way to explicitly delete memory allocated on the stack, and assigning "null" doesn't do much except let you check, later on, whether the memory is valid, which usually only applies to pointers.


    (In response to below):
    Quote Originally Posted by rudyman View Post
    Next, try making an abstract class Animal from which you can derive your Dog
    Last edited by rudyman; 06-26-2008 at 05:46 PM.

  14. #14
    kevinawad
    Guest
    Weight = 0 is okay, i'm not doing that to make a big program, it's just my project for classes. i done it now, now, i am comfortable with classes and know how they work...now, i can work on something else. That's what im going to do right now.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    I think you need to change how you're approaching this. Your thinking is off. What you're trying to do might work better in Java. In C++, a local object exists until the end of the block it was declared in. As was mentioned, if you want an object to not exist any more, then make sure it is in a block that ends when you don't need it.

    In some cases the lifetime of the object won't fit neatly into a block and you can use dynamic memory. But those cases are much rarer than you think.

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

Similar Threads

  1. Finally Graduated
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 06-01-2006, 01:04 AM
  2. Replies: 6
    Last Post: 12-21-2005, 02:49 AM
  3. OThello 0.9 - finally working..
    By Nutshell in forum Game Programming
    Replies: 19
    Last Post: 04-18-2003, 11:40 AM
  4. I finally got...
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 11-18-2001, 06:38 PM

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