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;

}

#

Dani AI

Generated

Short answer: the string problems are compiler errors caused by a mix of missing includes, mismatched declarations/definitions, identifier collisions and a few syntax errors. was correct to call out the missing <string> and the naming issues — the posted header, implementation and test disagree on function names, return types and parameter lists, and some members share names with methods (which is not allowed). Default arguments were also used incorrectly in the constructor declaration.

A clean, minimal design fixes those mistakes and follows a few simple rules: include <string> in the header and use std::string there (avoid using namespace std; in headers), give data members different names (eg. weight_) to avoid collisions, make getters const, pass string parameters by const std::string& (or by value) and ensure the header prototypes exactly match the .cpp definitions. The constructor should provide defaults for all trailing parameters (if defaults are used).

Example corrected layout (header + implementation + tiny test):

// cat.h
#ifndef CAT_H
#define CAT_H
#include <string>

class Cat {
public:
  Cat(int weight = 1, int age = 1, std::string talk = "no", std::string walk = "no");
  void setWeight(int w);
  void setAge(int a);
  void setTalk(const std::string& t);
  void setWalk(const std::string& w);
  int getWeight() const;
  int getAge() const;
  const std::string& getTalk() const;
  const std::string& getWalk() const;
private:
  int weight_;
  int age_;
  std::string talk_;
  std::string walk_;
};
#endif

// cat.cpp (implementation)
#include "cat.h"
Cat::Cat(int weight,int age,std::string talk,std::string walk)
  : weight_((weight>0)?weight:1), age_((age>0)?age:1), talk_(talk), walk_(walk) {}
// ...setters/getters implemented to match prototypes...

Practical debugging tips: compile early and fix the first compiler error (many later messages cascade), make names consistent between header and source, ensure getters return std::string (not int), and avoid using namespace std; in headers. Those steps resolve the string-related failures seen in the original posts by .

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.