Create a class Cat with its data members weight & age, each of which defaults to 1. Provide get & set member functions for each data member. Also provide data member functions speak & walk. Both set functions should verify that data members are >0 otherwise set the default value to 1. Test it with three objects of the class, with no values, with negative values and with valid values. Your test program should call all the data members for all three objects.The above is my question I was asked to write a program for. The program runs whenever I take the strings out but when the strings are in it doesnt work can you help me or explain where I messed up. This is a problem I am having but I have wrote my own code as you see below and just need pointed in the right direction?

header file-----------------------

#ifndef Cat_H
#define Cat_H


class Cat
{
public:
   Cat( int weight = 1, int age = 1, string walk, string talk  ); 

  
   void setweight( int ); 
   void setage( int ); 
  void speak( string );
	void walk( string );
 

   // get functions
    int getweight(); 
   int getage(); 
   string walk();
  string talk ();

  
private:
   int weight; 
    int age; 
   string walk;
    string talk;
  
}; 
#endif


test file ------------------------------------------------------

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


Cat cat1(0,0,"nah","no");
Cat cat2(-5,-10,"nah","no");
Cat cat3(3,0,"yeah","no");

cout << "First cats age and weight" << cat1.getweight() << "pounds and " << cat1.getage()<<" Years of age " <<endl;
		endl;
	cout << "Can First cat Purr? " << cat1.gettalk() << endl;
	cout << "Can First cat Walk? " << cat1.getwalk() << endl;

	cout << "Second cats age and weight" << cat1.getweight() << "pounds and " << cat2.getage()<<" Years of age " <<endl;
		endl;
	cout << "Can Second cat Purr? " << cat2.gettalk() << endl;
	cout << "Can Second cat Walk? " << cat2.getwalk() << endl;

	cout << "Third cats age and weight" << cat1.getweight() << "pounds and " << cat2.getage()<<" Years of age " <<endl;
		endl;
	cout << "Can Third cat Purr? " << cat3.gettalk() << endl;
	cout << "Can Third cat Walk? " << cat3.getwalk() << endl;
	system ("pause");


c++ file -------------------------------------------------

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


Cat::Cat(int weight,int age,string walk string talk)
{
	setweight(weight);
     setage(age);
    setwalk(walk);
    settalk(talk);
}
void Cat::setweight(int w)
{weight = (w > 0 ) ? w : 1 ;
}

void Cat::setage(int a)
{age= ( a > 0 ) ? a : 1 ;

void Cat::settalk( string t)
{
	talk = string t;
}

void Cat::setwalk( string w)
{
	walk = string w;
}

int Cat::getweight()
{
   return weight;
}

int Cat::getage()
{
   return age;

}
int Cat::gettalk()
{
   return talk;

}
int Cat::getwalk()
{
   return walk;

}

#

Recommended Answers

All 2 Replies

You never include <string> or <iostream> in your header file so the "string" class is undeclared (you should put "using namespace std;" in your header too). Also you are using names for your get, set, and variable declarations for your strings.

ok thank you I will fix these things and see where that gets me :) I noticed after I posted I didnt need that string stuff so I have took a few out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.