abstract class and virtual functions

0 votes

Because it only contains one virtual function, would this class be deemed abstract? I was still able to build an Animal object and use getFoodCost() to calculate the cost of food; Abstract classes, I assumed, couldn't be instantiated. Does this imply that objects with virtual functions are not abstract classes?

class Animal
{
public:
    Animal();
    int getBabies();
    double getFoodCost();
    virtual double getPayOff();
    int getQty();

    void incAge();
    void setAge(int);

protected:
    int counter=0;
    int age;
    int cost;   
};

Jun 15, 2022 in C++ by Nicholas
• 7,760 points
744 views

1 answer to this question.

0 votes

Is this class considered abstract because it only contains one virtual function? 

I could still build an Animal object and use getFoodCost();

No. In C++, a "Abstract Class" is often a class having a single pure virtual function:

class Abstract {
public:
    virtual void PureVirtual() = 0; // no implementation
};

class NotAbstract {
    virtual void NotPureVirutal() {
        // Some implementation
    }
};

I assumed abstract classes couldn't be instantiated.


 

Yes, you are accurate. 

This is due to the fact that abstract classes have functions that do not require implementation. 

It has no mechanism to instantiate an object directly. 

It refers to an abstract class using inheritance and pointers/references.

Does this imply that objects may contain virtual functions while still being considered abstract classes?

Yes. 

Again, abstract classes are those with only virtual functions. 

This is different from a regular virtual function. 

As previously stated, pure virtual functions do not require implementation. 

Regular virtual functions, on the other hand, must be associated with an implementation in order to be instantiated.

answered Jun 20, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

What is the difference between a concrete class and an abstract class?

I'm learning C++, but I'm having trouble ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
484 views
0 votes
0 answers

Making a class abstract without any pure virtual methods

I take a class where we watch ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
751 views
0 votes
1 answer

functions in c++ call by value and call by reference

Calling a function by value copies the argument and stores it in a local variable for use by the function, so the argument is unaffected if the value of the local variable changes.  The argument is passed to the function as a reference rather than a copy, so if the function changes the value of the argument, the argument is also changed.   The void change(int); function prototype informs the compiler that there is a function named change that takes a single int argument and returns void (i.e. nothing).  Because there is no & with the argument, it is called by value.  You have the line change(orig); later in your code, which actually calls the function with. Take a look at the output of ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
622 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
583 views
0 votes
1 answer

Can Abstract Class have a Constructor?

Yes, an abstract class can have a ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
1,164 views
0 votes
1 answer

What is abstract class in java?

A class that is declared with abstract ...READ MORE

answered Jun 11, 2018 in Java by Daisy
• 8,140 points
962 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
686 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,384 views
0 votes
1 answer

Declare abstract class in c++

An abstract class is one that is intended to be used as a base class .  At least one pure virtual function exists in an abstract class.  A pure virtual function is declared in the class declaration by using a pure specifier (= 0) in the declaration of a virtual member function. Here is an example of an abstract class: class AB { public: virtual void f() ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
562 views
0 votes
1 answer

std::greater on a an std::pair of a double and a class

std::greater is simply a wrapper for a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
1,196 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP