Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

OOP #1 SUPER EASY

Think you can spy out that little nasty bug? Try debugging some code in here

OOP #1 SUPER EASY

Postby Argio on Mon Dec 22, 2008 4:17 pm

Heres my second attempt at making a debug for you guys.

Code: Select all
#include <iostream>

using namespace std;

class Cow
{
       private:
            int Utters;
            bool alive;
       public:
            void KillCow()
            {
                  alive=false;
                  return;
            }
}

int main(int argc, char *argv[])
{
       Cow cow;
       cout << "CLASS COW IS ALIVE" << endl;
       cow.KillCow();
       if(cow.alive==false)
       {
              cout << "CLASS COW HAS DIED. " << endl;
       }else{ cout << "CLASS COW HAS SURVIVED." << endl; }

       char f;
       cin >> f;
       return 0;
     
}
Last edited by Argio on Fri Feb 27, 2009 2:53 pm, edited 1 time in total.
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.

Re: OOP #1 SUPER EASY

Postby C++ on Wed Feb 04, 2009 9:03 am

Code: Select all
#include <iostream>

using std::cout;
using std::cin;

class Cow
{
    const char *name;
    int Utters;
    bool alive;
public:
    Cow(const char *_name) : name(_name), Utters(4), alive(true) {}
    const char * getName() const {return name;}
    void KillCow() {alive = false;}
    bool isAlive() const {return alive;}
};


int main(int argc, char *argv[])
{
    Cow cow("Mooo");
    cout << "COW "<< cow.getName() << " IS ALIVE\n";
    cow.KillCow();
    if (!cow.isAlive()) cout << "COW "<< cow.getName() << " HAS DIED\n";
    else cout << "COW "<< cow.getName() << " HAS SURVIVED\n";
    cin.get();
}


I detect five problems

1. Class cow is undefined. It doesn't have a status. You mention it as if the class itself has a status. It's merely a blueprint.
2. Your instance of Cow does not have a status either, so you can't state it's alive, because you don't have a constructor that initializes the instance.
3. A ; was missing after } of class Cow.
4. bool alive is private and you can only interact with it through a method.
5. char f[] is undefined. The compiler doesn't know the size of f.


Some recommendations...

Use cin.get() instead of declaring a char to get input you don't need to store.
Don't bring in the entire namespace std. Doing that will bring in every symbol and library implementations into your program.
Instead tell the compiler what symbols to use (example: std::cout;) or just use the std:: before a symbol you use.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: OOP #1 SUPER EASY

Postby Argio on Fri Feb 27, 2009 2:47 pm

I found a problem with your code... *coughcstrings* *cough* *cough*

And If I say class cow is alive well too damn bad. I know classes are blueprints that can be used to intialize instances of the blueprints. I'm not that dumb. Also I agree with you on the:

Code: Select all
using std::cin;
using std::cout;



Also, this is for the very beginning of them starting to learn OOP... So it's not ot the point of knowing constructors or anything like that. And STOP USING THEM DAMNED C STYLE STRINGS!
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.

Re: OOP #1 SUPER EASY

Postby C++ on Wed Mar 11, 2009 2:03 pm

Argio wrote:I found a problem with your code... *coughcstrings* *cough* *cough*

And If I say class cow is alive well too damn bad. I know classes are blueprints that can be used to intialize instances of the blueprints. I'm not that dumb. Also I agree with you on the:

Code: Select all
using std::cin;
using std::cout;



Also, this is for the very beginning of them starting to learn OOP... So it's not ot the point of knowing constructors or anything like that. And STOP USING THEM DAMNED C STYLE STRINGS!


Argio, the problem is not with my code, but with your way of thinking.
There is NOTHING wrong with using const char * like C strings if the string is not going to be modified in any way.
It is actually preferred, speed wise, to use C like strings over C++ strings when they're constant.
For all other purposes I would use the C++ string class.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: OOP #1 SUPER EASY

Postby HackneyedPhrases on Mon Nov 16, 2009 4:28 pm

Sorry for the double post, honestly unintentional, my remark below.
Last edited by HackneyedPhrases on Mon Nov 16, 2009 4:31 pm, edited 1 time in total.
HackneyedPhrases
 
Posts: 5
Joined: Sun Sep 06, 2009 2:45 pm

Re: OOP #1 SUPER EASY

Postby HackneyedPhrases on Mon Nov 16, 2009 4:29 pm

For me, probably because I'm a nub, but the two problems I spotted were:
1) The Cow Class forgot the ending semicolon
2) bool alive is a private member, so it cannot be accessed directly.

I don't know if I can do this, hopefully, but if you want to access alive indirectly, all you would need to do is add an accessor function within the public members of the class:

public:
bool GetAliveStatus()
{
return alive;
}

Then in main,

if (cow.GetAliveStatus() == false)
...
HackneyedPhrases
 
Posts: 5
Joined: Sun Sep 06, 2009 2:45 pm


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests