Question
solve it in c++ 10
1. Define a Pet class that stores the pets name, age, and weight. Add appropriate constructors, accessor functions, and muta





note:
please do not give me same answer like this

#include<iostream> //C++ program using namespace std; class Pet{ protected: string name; int age; double weight; public: Pet(
9:08turn unknown lifespan; class Dog:public Pet{ private: string breed; public: Dog(string name, int age, double weight, st
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM CODE (C++)

Note:

Both interface (header) and implementation files are provided for each class and one cpp file for test program.

Pet.h

// Interface file of Pet Class

#ifndef PET_H   // Header Guards
#define PET_H

#include <iostream>
using namespace std;

class Pet {
  
   private:
      
       // All required member variables
      
       string name;
       int age;
       float weight;
      
   public:
      
       // All required member functions
      
       Pet();                       // Empty constructor
       Pet(string, int, float);   // Constructor to initialize all fields
      
       // Mutator and Accessor functions
      
       void setName(string);
       void setAge(int);
       void setWeight(float);
      
       string getName();
       int getAge();
       float getWeight();
      
       // getLifeSpan function
      
       string getLifeSpan();
};

#endif

Pet.cpp

// Implementation file of Pet Class

#include "Pet.h"

Pet::Pet() {
  
   // Setting default values
  
   name = "tommy";
   age = 15;
   weight = 100;
}

Pet::Pet(string n, int a, float w) {
   name = n;
   age = a;
   weight = w;
}

void Pet::setName(string n){
   name = n;
}

void Pet::setAge(int a){
   age = a;
}
void Pet::setWeight(float w){
   weight = w;
}

string Pet::getName(){
   return name;
}
int Pet::getAge(){
   return age;
}
float Pet::getWeight(){
   return weight;
}

string Pet::getLifeSpan(){
   return "unknown lifespan";
}

Dog.h

// Interface file of Dog Class

#ifndef DOG_H
#define DOG_H

#include "Pet.h"

class Dog : public Pet {
  
   private:
      
       // A member variable needed
      
       string breed;
  
   public:
      
       // Adding constructor and accessor and mutator functions
      
       Dog(string, int, float, string);
      
       void setBreed(string);
       string getBreed();
      
       // Redefining getLifeSpan function
      
       string getLifeSpan();
};
#endif

Dog.cpp

// Implementation file of Dog class

#include "Dog.h"

Dog::Dog(string n, int a, float w, string b) : Pet(n, a, w) {
   //Pet(n, a, w);
   breed = b;
}

void Dog::setBreed(string b){
   breed = b;
}
string Dog::getBreed(){
   return breed;
}

string Dog::getLifeSpan(){
  
   string str = "";
  
   // Following conditions mentioned in question
  
   if(getWeight() > 100)
       str = "Approximately 7 years";
   else if(getWeight() < 100)
       str = "Approximately 13 years";
      
   return str;
}

Rock.h

// Interface file of Rock Class

#ifndef ROCK_H
#define Rock_H

#include "Pet.h"

class Rock : public Pet {
  
   public:
      
       Rock();
          
       // Redefining getLifeSpan function
      
       string getLifeSpan();
};
#endif

Rock.cpp

// Implementation of Rock Class

#include "Rock.h"

Rock::Rock() : Pet(){
}

string Rock::getLifeSpan() {
   return "Thousands of years.";
}

main.cpp

// A Test program

#include <iostream>
#include "Pet.h"
#include "Dog.h"
#include "Rock.h"

using namespace std;

int main() {
  
   Pet a("Bob", 2, 50);
   cout << a.getLifeSpan() << endl;
  
   Dog b("Sam", 5, 150, "Boxer");
   cout << b.getLifeSpan() << endl;
  
   Rock c;
   cout << c.getLifeSpan() << endl;
  
   return 0;
}

SAMPLE OUTPUT

C:\Users\Dell OneDrive\Desktop\CPP July 2020 Pet\Project1.exe unknown lifespan Approximately 7 years Thousands of years. Proc

-----------------------------------------------------------------------------------------------------

COMMENT DOWN FOR ANY QUESTIONS...........
HIT A THUMBS UP IF YOU DO LIKE IT!!!

Add a comment
Know the answer?
Add Answer to:
solve it in c++ 10 note: please do not give me same answer like this 1....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB: Pet information (derived classes)

    LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information:     Name: Dobby    Age: 2 Pet Information:     Name: Kreacher    Age: 3    Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Pet myPet = new Pet();       Dog myDog = new Dog();              String petName, dogName, dogBreed;       int petAge, dogAge;       petName = scnr.nextLine();       petAge = scnr.nextInt();       scnr.nextLine();...

  • 1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7...

    1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7 class Pet 8 { 9   public: 10       string name; 11       virtual void print( ) const; 12}; 13 14 class Dog:public Pet 15 { 16   public: 17       string breed; 18       virtual void print( ) const; 19}; 20 21 int main( ) 22 { 23   Dog vdog; 24   Pet vpet; 25   vdog.name = "Tiny"; 26   vdog.breed = "Great Dane"; 27   vpet =...

  • [c++] Given the following simplified classes, which of the statements are not legal? Explain your answer...

    [c++] Given the following simplified classes, which of the statements are not legal? Explain your answer your answer in detail. class Pet { public:     virtual void print();     string name; private: }; class Dog: public Pet { public:    void print();    string breed; }; //code below is in the main() Dog dog; Pet   pet; dog.name = "rover"; dog.breed = "Collie"; a. pet = dog; cout << dog.name; b. pet = dog; cout << dog.breed; c. pet = dog;...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello...

    // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello virtual function (hint this will be done in Person class // 2) Change main to create two Person pointer variables // 3) Set each pointer to one of the two objects already instantiated below // 4) Use the pointer variable to invoke the two sayHello() methods #include <iostream > #include <string > using namespace std ; class Person { protected : string name; int...

  • C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement...

    C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement the constructors and member function of each of the classes (Marks 15) class Fraction{ private: int numerator; int denominator; public: Fraction(int, int); float fractionValue();//determines the value of numerator/denominator }; class Problem{ private: Fraction f[3]; public: Problem(int, int, int, int, int, int); Fraction largestFraction();//Returns the fraction having largest fraction value }; Question 2: In the following Inheritance problem #include<iostream> #include<string> using namespace std; class Animal{...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT