In: Computer Science
Debug the pet.h file and pet.cpp file (I would like to check my work)
#ifnotdef PET_H
#then_define PET_H
#include <string>
using name space std
namespace fhsuzeng
{
Class Pet
{
public
Pet();
void setName(string name);
string getName() const;
void setAge(double age);
int getAge() const;
virtual void talk();
private:
string _name; int _age;
};
class Dog::public Pet
{
public:
Dog();
void setBreed(string breed);
string getBreed() const;
private: string _breed;
}
class Cat:Pet
{
Public:
Cat();
void setColor(string color);
string getColor() const;
private:
string _color;
};
}; #endif
The above is pet.h file and the following is the file pet.cpp
#include <string>
#include <pet.h>
using name space std
namespace fhsuzeng
{
void Pet::Pet():: _name("no name yet"), _age(0){}
int Pet::getAge() const { return age; }
void Pet::setAge(int age) { age = age; }
string Pet::getName() const { return _name }
void Pet::setName(string name) { _name = name; }
Pet::Dog(): Pet(), _breed("Any breed") {}
string Dog::getBreed() { return _breed; }
void Dog::setBreed(string breed) { _breed = breed; }
Cat::Cat(): Pet(), _color("Red")
Cat::string getColor() const { return _color; }
void Cat:: setColor(string color) { _color = color; } }
pet.h:
Raw_code:
#ifndef PET_H
#define PET_H
#include <string>
using namespace std;
namespace fhsuzeng{
class Pet{
public:
Pet();
void setName(string name);
string getName() const;
void setAge(int age);
int getAge() const;
private:
string _name; int _age;
};
class Dog:public Pet{
public:
Dog();
void setBreed(string breed);
string getBreed() const;
private: string _breed;
};
class Cat:public Pet{
public:
Cat();
void setColor(string color);
string getColor() const;
private:
string _color;
};
}
#endif
pet.cpp:
Raw_code:
#include <string>
#include "pet.h"
using namespace std;
namespace fhsuzeng{
Pet::Pet(): _name("no name yet"), _age(0){}
int Pet::getAge() const { return _age; }
void Pet::setAge(int age) { _age = age; }
string Pet::getName() const { return _name; }
void Pet::setName(string name) { _name = name; }
Dog::Dog(): Pet(), _breed("Any breed") {}
string Dog::getBreed() const{ return _breed; }
void Dog::setBreed(string breed) { _breed = breed; }
Cat::Cat(): Pet(), _color("Red"){}
string Cat::getColor() const { return _color; }
void Cat::setColor(string color) { _color = color; }
}
main.cpp:
Raw_code:
#include<iostream>
#include"pet.cpp"
using namespace std;
using namespace fhsuzeng;
int main(){
Pet pet = Pet();
cout<<"Pet: "<<pet.getName()<<endl;
pet.setName("Jimmy");
pet.setAge(9);
cout<<"pet name: "<<pet.getName()<<"\tage:
"<<pet.getAge()<<endl<<endl;
Dog dog = Dog();
cout<<"Dog: "<<dog.getBreed()<<endl;
dog.setName("Browny");
dog.setBreed("Pamorian");
dog.setAge(5);
cout<<"Dog name: "<<dog.getName()<<"\tbreed:
"<<dog.getBreed()<<"\tage:
"<<dog.getAge()<<endl<<endl;
Cat cat = Cat();
cout<<"Cat: "<<cat.getColor()<<endl;
cat.setColor("Black");
cat.setName("Tommy");
cat.setAge(4);
cout<<"Cat name: "<<cat.getName()<<"\tcolor:
"<<cat.getColor()<<"\t age:
"<<cat.getAge()<<endl<<endl;
}
output: