program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: program

  1. #1
    peter Guest

    program


    hello and thanks for your help - see prog below
    even though thr program below compile i think i have my print object and

    my main needs to some changes cause it will not link
    can you please help

    thanks P

    define dogyrs = 7 ; // mult a # by 7 to actual dog years
    #include <iostream>
    using namespace std;


    int dage,human_yrs, amt_pups;

    class Dog //base
    {
    public:
    Dog(const string & dgname , const string & dgtype);
    Dog(const Dog & rhs);
    Dog();
    ~Dog();

    Dog & operator= (const Dog &rhs);
    Dog & operator+= (const Dog &rhs);

    int Dogage(int &human_yrs);
    //int Set_Dogage();
    bool have_litter( int & amt_pups);
    void Dog_bark();
    void DogPrint();

    private:
    string name_;
    string type_;
    int age_;
    bool pups_;

    };

    #include "hdog.h"
    #include <iostream>

    void main()
    {

    Dog obj1("rex","lab");

    int theage;
    cout << " enter dog's age" << endl;
    cin >> theage;


    obj1.Dogage(theage);

    obj1.DogPrint() ;
    }

    #include "hdog.h"
    #include <iostream>
    #include <string>
    using namespace std;

    Dog::Dog(const string & dgname , const string & dgtype )
    :name_(dgname), type_(dgtype), pups_(false)
    {
    age_ = dage;
    }

    Dog::Dog(const Dog & rhs)
    :name_(rhs.name_), type_(rhs.type_), age_(rhs.age_), pups_(rhs.pups_)
    {
    }

    Dog::Dog()
    :name_("default name"), type_("mongrel"), age_(0), pups_(false)
    {
    }

    Dog & Dog::operator = (const Dog &rhs)
    {
    // do check no same name

    name_ = rhs.name_;
    type_ = rhs.type_;

    return * this;
    }

    Dog & Dog::operator+= (const Dog & rhs)
    {
    return * this;
    }

    int Dog::Dogage( int &human_yrs)
    {
    dage = human_yrs * 7;//dogyrs;
    age_ = dage;
    return age_;
    }

    bool Dog::have_litter( int & amt_pups)
    {
    int puppies;
    puppies = amt_pups;
    if (!pups_ )
    {
    cout << " I already have " << puppies << "lil ones" << endl;
    cout << "cannot have anymore" << endl;
    pups_ = true;
    return true;
    }
    else if (pups_ && puppies > 0)
    {
    puppies = amt_pups;
    cout << " I now have " << puppies<<" lil ones" << endl;
    pups_ = true;
    return true;
    }
    else
    {
    cout << " i have no puppies " << endl;
    return false;
    }
    }

    void Dog::Dog_bark()
    {
    cout << "woof ! woof !" << endl;
    }

    void Dog::DogPrint()
    {
    cout <<" my name is "<< name_ << " i'm a "<<type_ << " i am "<< "\n"
    << Dogage(human_yrs) <<" years old "<< have_litter(amt_pups) << endl;
    Dog_bark() ;
    }



  2. #2
    vishal kochhar Guest

    Re: program


    There are two errors I can spot. But first, another important thing: your
    main() function should return int and not void:

    1) define dogyrs = 7;

    should be
    #define ....

    2) you have not defined the destructor Dog::~Dog(). Do one of these depending
    upon logic:
    a) Remove its decleration from the header file. Seems like most appropriate
    as your destructor is doing nothing.
    b) In the header file, change ~Dog(); to ~Dog() {}
    c) Add in the cpp file Dog::~Dog() {} member function definition

    Vishal

    "peter" <peter.l.ridgard@verizon.com> wrote:
    >
    >hello and thanks for your help - see prog below
    >even though thr program below compile i think i have my print object and
    >
    >my main needs to some changes cause it will not link
    >can you please help
    >
    >thanks P
    >
    >define dogyrs = 7 ; // mult a # by 7 to actual dog years
    >#include <iostream>
    > using namespace std;
    >
    >
    >int dage,human_yrs, amt_pups;
    >
    >class Dog //base
    >{
    >public:
    > Dog(const string & dgname , const string & dgtype);
    > Dog(const Dog & rhs);
    > Dog();
    > ~Dog();
    >
    > Dog & operator= (const Dog &rhs);
    > Dog & operator+= (const Dog &rhs);
    >
    > int Dogage(int &human_yrs);
    > //int Set_Dogage();
    > bool have_litter( int & amt_pups);
    > void Dog_bark();
    > void DogPrint();
    >
    >private:
    > string name_;
    > string type_;
    > int age_;
    > bool pups_;
    >
    >};
    >
    >#include "hdog.h"
    >#include <iostream>
    >
    >void main()
    >{
    >
    > Dog obj1("rex","lab");
    >
    > int theage;
    > cout << " enter dog's age" << endl;
    > cin >> theage;
    >
    >
    > obj1.Dogage(theage);
    >
    > obj1.DogPrint() ;
    >}
    >
    >#include "hdog.h"
    >#include <iostream>
    >#include <string>
    >using namespace std;
    >
    >Dog::Dog(const string & dgname , const string & dgtype )
    >:name_(dgname), type_(dgtype), pups_(false)
    >{
    > age_ = dage;
    >}
    >
    >Dog::Dog(const Dog & rhs)
    >:name_(rhs.name_), type_(rhs.type_), age_(rhs.age_), pups_(rhs.pups_)
    >{
    >}
    >
    >Dog::Dog()
    >:name_("default name"), type_("mongrel"), age_(0), pups_(false)
    >{
    >}
    >
    >Dog & Dog::operator = (const Dog &rhs)
    >{
    > // do check no same name
    >
    > name_ = rhs.name_;
    > type_ = rhs.type_;
    >
    > return * this;
    >}
    >
    >Dog & Dog::operator+= (const Dog & rhs)
    >{
    > return * this;
    >}
    >
    >int Dog::Dogage( int &human_yrs)
    >{
    > dage = human_yrs * 7;//dogyrs;
    > age_ = dage;
    > return age_;
    >}
    >
    >bool Dog::have_litter( int & amt_pups)
    >{
    > int puppies;
    > puppies = amt_pups;
    > if (!pups_ )
    > {
    > cout << " I already have " << puppies << "lil ones" << endl;
    > cout << "cannot have anymore" << endl;
    > pups_ = true;
    > return true;
    > }
    > else if (pups_ && puppies > 0)
    > {
    > puppies = amt_pups;
    > cout << " I now have " << puppies<<" lil ones" << endl;
    > pups_ = true;
    > return true;
    > }
    > else
    > {
    > cout << " i have no puppies " << endl;
    > return false;
    > }
    >}
    >
    >void Dog::Dog_bark()
    >{
    > cout << "woof ! woof !" << endl;
    >}
    >
    >void Dog::DogPrint()
    >{
    > cout <<" my name is "<< name_ << " i'm a "<<type_ << " i am "<< "\n"
    > << Dogage(human_yrs) <<" years old "<< have_litter(amt_pups) << endl;
    > Dog_bark() ;
    >}
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center