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 6639177
In Process

The Archive Base Latest Questions

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

I have four .cpp files, Animal, Cattle, Sheep, DrugAdmin . Animal is parent class

  • 0

I have four .cpp files, Animal, Cattle, Sheep, DrugAdmin.
Animal is parent class of Cattle, and it has calcDose(), which calculates the amount of dose. DrugAdmin is main function.
The thing is, I want to use calcDose() function differently (Cattle, Sheep) and no calcDose() function is needed for Animal class. However, every time I try to use calcDose(), it automatically calls function in Animal class even when I want to use under Cattle class. This is the code I have done so far. (I’ve cut it down)

Animal.cpp

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

Animal::Animal(int newid, double newweight, int yy, int mm, int dd, char newsex, vector<Treatment> treatArray)
{
    id = newid;
    weight = newweight;
    yy = yy;
    mm = mm;
    dd = dd;
    accDose = 0;
    sex = newsex;
}
double Animal::calcDose(){
    return 0;
}

Cattle.cpp

#include "Cattle.h"
using namespace std;


Cattle::Cattle(int newid, double newweight, int yy, int mm, int dd, 
           char newsex, vector<Treatment> newtreatArray, string newcategory)
    : Animal(newid, newweight, yy,mm,dd, newsex, newtreatArray)
{
    id = newid;
    weight = newweight;
    accDose = 0;
    sex = newsex;
    Cattle::category = newcategory;
}

Cattle::~Cattle(){}

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

Sheep class is pretty much same but the contents of calcDose()

DrugAdmin.cpp

#include "DrugAdmin.h"
using namespace std;

vector<Animal*> vec_Animal;

void addAnimal(){
    int select=0;
    int id;
    double weight;
    int yy;
    int mm;
    int dd;
    char sex;
    string category;
    vector<Treatment> treatArray;

        //user inputs all the values (i've cut it down)

     Animal* c1 = new Cattle(id,weight,yy,mm,dd,sex,treatArray,category);

        vec_Animal.push_back(c1);
}

void administerDose(int id)  //Main Problem
{


   vector<Animal*>::iterator ite_Animal = vec_Animal.begin();
    for(ite_Animal; ite_Animal != vec_Animal.end(); ++ite_Animal)
        cout<<"\nVector contains:"<< (*ite_Animal)->calcDose();
}

I’m sorry for the long and messed up question. Final question is, Cattle has extra data member which is category but the system doesn’t recognise this as well. It recognises as if it is Animal object. Can I have a piece of advice 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:31:25+00:00Added an answer on May 25, 2026 at 11:31 pm

    Unlike Java (and many other languages), C++ uses what is called “static binding” by default. Static binding means the function called is based on the declared type of the object (or pointer) at compile time, rather than what the object actually is (or what the pointer is actually pointing to).

    The alternative to static binding is “dynamic binding”. In dynamic binding, the function called is based on what the object actually is (or what the pointer is actually pointing to) at runtime.

    To enable dynamic binding, which is what you’ll want if you have an inheritance hierarchy set up and are maintaining base class pointers (Animal*s), you have to use the keyword virtual. virtual makes a function dynamically bound, so that calls to that function will reflect the actual runtime type of the object (Cattle) rather than the compile time declared pointer type (Animal*).

    To declare a function virtual, you put the virtual keyword before the return value on the function declaration (in the class declaration / .h file):

    class Animal
    {
        virtual void calcDose();
        // ...
    };
    

    It is also good style to put virtual on the overridden function in the derived class (calcDose() in Cattle), although the virtual is implied for inherited functions, so putting it there is not strictly necessary.

    The one last thing to make sure you to get dynamic binding, is to make sure you always have pointers (or references) to objects rather than objects on the stack (when you want dynamic binding). An object on the stack (i.e. Animal a) can only be it’s declared type, it can’t point to a derived class type. So the vector<Animal*> you have is perfect, because those pointers can point to Cattle objects (or Animal objects), while a vector could only hold actual animal objects.

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

Sidebar

Related Questions

I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property
I have four classes which share some arrangement of four properties. I have currently
I have some databases that have four files each; one for PRIMARY, IDX, IMAGE,
I have four buttons on my page <button id=opener >Submit Timecard</button> <button id=cancel class=cancel>Cancel</button>
I have four branches in my git repository, which is managed using GitHub: Production
I have four files containing C code. Headers.h - (contain all necessary) headers AddStudent.h
I have four buttons, which have to push the same uiTableViewController, with different data
I have four nodes which are acting as a redundancy quartet of nodes. My
I have four tables containing exactly the same columns, and want to create a
I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All

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.