Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6636315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:10:14+00:00 2026-05-25T23:10:14+00:00

I’m trying to store things into vector but there’re few problems. These are my

  • 0

I’m trying to store things into vector but there’re few problems.
These are my code.

Animal.cpp

#include "Animal.h"
#include "Cattle.h"
#include "Sheep.h"
#include <iostream>
#include <string>
using namespace std;

Animal::Animal(int newid, double newweight, int yy, int mm, int dd, double newaccDose, char newsex)
{
    id = newid;
    weight = newweight;
    yy = yy;
    mm = mm;
    dd = dd;
    dose = newaccDose;
    sex = newsex;
}

Animal::Animal()
{
    id = 0;
    weight = 0;
    yy = 0;
    mm = 0;
    dd = 0;
    dose = 0;
    sex = ' ';
}

Animal::~Animal(){}

double Animal::getDaysDifference(){
    jdate dateOfBirth(dd,mm,yy);
    jdate now;
    double diff = now-dateOfBirth;
    return diff;
}

void Animal::addAnimal(){
    int select=0;
    int id;

    cout << "1.  Cattle    2.  Sheep" << endl;
    cout << "Select a type of animal: ";
    cin >> select;

    if(select==1){
        Cattle* c1;
        cout << "Please answer following questions" << endl;
        cout << "Type the animal's ID: ";
        cin >> id;

        c1->setID(id);

        //vector<Animal> vec;
        //vector<Animal>::iterator vec_ite;

    }else if(select==2){
        Sheep* s1;
        cout << "Please answer following questions" << endl;
        cout << "Type the animal's ID: ";
        cin >> id;

        s1->setID(id);
    }
    else{
        cout << "Invalid number please try again" << endl;  
    }
}

First off, in the code above, Cattle* c1 and Sheep* s1, it doesn’t have an error.
and even if I code like s1->setID(id) it’s fine. However,

Cattle.cpp

#include "Cattle.h"

Cattle::Cattle(int newid, double newweight, int yy, int mm, int dd, 
           double newdose, char newsex, string newcategory)
    : Animal(id, weight, yy,mm,dd, dose, sex)
{
    id = newid;
    weight = newweight;
    dose = newdose;
    sex = newsex;
    Cattle::category = newcategory;
}

Cattle::Cattle(){
    id = 0;
    weight = 0;
    dose = 0;
    sex = ' ';
    Cattle::category = "";
}

Cattle::~Cattle(){}

string Cattle::getCategory(){
    return category;
}

double Cattle::calcDose(){
    Cattle* c1;
    if(c1->getDaysDifference() < 90 || c1->getCategory() == "Meat"){
        c1->dose = 0;
        return c1->dose;
    }
    else if(c1->getCategory() == "Dairy"){
        if (c1->weight < 250 || c1->dose > 200){
            c1->dose = 0;
        }
        else{
            c1->dose = c1->weight * 0.013 + 46;
        }
        return c1->dose;
    }
    else if(c1->getCategory() == "Breeding"){
        if (c1->weight < 250 || c1->dose > 250){
            c1->dose = 0;
        }
        else{
            c1->dose = c1->weight * 0.021 + 81;
        }
        return c1->dose;
    }
    else
    {
        cout << "It is not valid category" << endl;
    }
}

Animal.h

#ifndef ANI_H
#define ANI_H
#include <vector>
#include "Treatment.h"
#include "jdate.h"

class Animal{
protected:
    int id;
    double weight;
    int yy;
    int mm;
    int dd;
    double dose;
    char sex;
    //Treatment treatArray[];
public:
    Animal();
    Animal(int newid, double newweight, int yy, int mm, int dd, double newdose, char newsex);
    ~Animal();

    virtual double calcDose() = 0;
    void addAnimal();
    double getDaysDifference();
};
#endif

DrugAdmin.cpp (main)

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

int main(){
    Animal* a1=new Animal;
    delete a1;

    a1->addAnimal();
}

The problem is here, when I try to define Animal* a1=new Animal;
delete a1;
it says Object of abstract class type "Animal" is not allowed:
I’ve Googled and Binged here and there, and I figured out I cannot define the abstract base class itself but subclasses.
So I tried the same thing in Cattle.cpp using Cattle c1=new Cattle; but it doesn’t work either..

I’m really not sure what’s wrong with that..
Could you help me please?

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:10:14+00:00Added an answer on May 25, 2026 at 11:10 pm

    virtual double calcDose()

    This says “It is possible to calcDose() for everything which is an Animal, and different Animals may do it differently…”

    = 0;

    “… but there is no ‘generic’ way to do it for Animals; each one must specify its own method”.

    You may not construct an unspecified sort of Animal because, by definition, a key piece of information is missing: how to calcDose for it.

    This is all well and good. Chances are, you don’t really want to construct an unspecified Animal, for the same reason that in real life, you wouldn’t go to a farm (Zoo? Pet store?) to buy “an animal” without having any specific preconception of what kind of Animal you want.

    The type Animal exists so that you can omit the information about what kind of Animal something is when it is appropriate to do so. Creating the instance is not one of those times, in your case.


    Also, addAnimal makes absolutely no sense as a member function of Animal, no matter how much you might think it does. There are three fundamental problems:

    1) since it’s a non-static member function, you already require an Animal to call the function. That’s kind of a catch-22.

    2) You don’t have anywhere to add the animal to. You need some kind of container.

    3) Conceptually, making a list of Animals is not the animals’ responsibility. It’s the responsibility of whoever needs the list.

    4) In general, it is good policy to have clean separation between the code that asks the user for information, and the code which acts upon that information. Write a free (meaning, not part of a class) function that asks for the information, constructs the Animal and returns that pointer; then in the calling code, store the pointer in a container.

    Except you don’t really want to use raw pointers for this anyway. You should be using some kind of smart pointer, or else putting the pointers into one of the boost ptr_container s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.