Some help with class member functions and pointers

This is a discussion on Some help with class member functions and pointers within the C++ Programming forums, part of the General Programming Boards category; Hello. I've been working with Sams Teach Yourself C++ in 24 hours and Accelerated C++ over the past few weeks. ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    9

    Some help with class member functions and pointers

    Hello. I've been working with Sams Teach Yourself C++ in 24 hours and Accelerated C++ over the past few weeks. I designed a simple Cat class that is almost identical to the one they use in the book. The problem comes in validating the data for the setage function. I've made an attempt at it and it works, but it seems a bit messy. I'd like to be able to simply call the setage function and have it do all of this without having to test for the return statements in the main function.

    Also, I have a question about the pointers that I'm using. I plan to make other animal classes and some type of zoo program (to practice using classes). Will the method I'm using still work if I have an array of Cat objects? I don't really understand what the syntax to call the function would be in that case. I'd appreciate any answers and suggestions you can give, even if it isn't the answer to all of them.

    Thanks,
    Matt

    main .C file. I'd like to integrate the catage function into the cat.h file, posted below, which contains the member functions so I don't have that loop in main.
    Code:
    #include<iostream>
    #include<string>
    #include "cat.h"
    
    int catage(Cat *one);
    
    int main()
    {
    std::cout << "What is the cat's name? ";
    std::string name;
    std::cin >> name;
    Cat * buck = new Cat;
    buck->setname(name);
    int test=1;
    while(test!=0)
    	{
    	test=catage(buck);
    	}
    buck->meow();
    std::cout << buck->getname() << " is " << buck->getage() << " year(s) old.\n";
    buck->meow();
    delete buck;
    buck = 0;
    return 0;
    }
    
    int catage(Cat *one)
    {
    std::cout << "How old is " << one->getname() << "? ";
    int catage=0;
    std::string badchars;
    if(!(std::cin >> catage))
    	{
    	std::cin.clear();
    	std::cin >> badchars;
    	std::cout << "\"" << badchars << "\"" << " is not a number!  Please try again\n";
    	return 1;
    	}
    else if(catage < 0 || catage > 20)
    	{
    	std::cout << catage << " is a strange age for a cat! Try again.\n";
    	return 1;
    	}
    else
    	{
    	one->setage(catage);
    	return 0;
    	}
    }
    the .h file. . .
    Code:
    #ifndef _cat_H_
    #define _cat_H
    #include<iostream>
    #include<string>
    
    class Cat
    {
    private:
    int itsage;
    std::string itsname;
    public:
    void setage(int age);
    int getage();
    void meow();
    void setname(std::string name);
    std::string getname();
    Cat();
    Cat(std::string name);
    ~Cat();
    };
    
    
    //default constructor
    Cat::Cat()
    {
    itsname="";
    }
    
    //setage sets the cat's age
    void Cat::setage(int age)
    {
    itsage = age;
    }
    
    //returns the cat's age
    int Cat::getage()
    {
    return itsage;
    }
    
    
    //meow has the cat meow
    void Cat::meow()
    {
    std::cout << "Meow\n";
    }
    
    //setname sets the cat's name
    void Cat::setname(std::string name)
    {
    itsname = name;
    }
    
    //getname returns the cat's name
    std::string Cat::getname()
    {
    return itsname;
    }
    
    //constructor
    Cat::Cat(std::string name)
    {
    itsname = name;
    }
    
    //destructor
    Cat::~Cat()
    {
    }
    
    #endif
    P.S. I'm not very good with pointers. It took me quite a while to even get this bit to work.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<iostream>
    
    class Foo
    {
        private:
            int bar;
    
        public:
            void setBar( int baz ) { bar = baz; }
            int getBar( ) { return bar; }
    
            Foo( ) { std::cout << "Constructor called." << std::endl; }
            ~Foo( ) { std::cout << "Destructor called." << std::endl; }
    };
    
    int main( )
    {
        Foo array[ 10 ];
        size_t x;
    
        for( x = 0; x < 10; x++ )
            array[ x ].setBar( x );
    
        for( x = 0; x < 10; x++ )
            std::cout << "array[ " << x << "].getBar( ) gives us " << array[ x ].getBar( ) << std::endl;
    
        return 0;
    }
    I'm not sure exactly what you're stuck on, but give that a shot.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  2. Pointers to member functions using virtual methods
    By Will B-R in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2006, 06:59 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  5. pointers to class functions?
    By btq in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2002, 04:17 PM

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