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 in C++ by Nicholas
• 2,460 points
7 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 by Damon
• 3,580 points

Related Questions In C++

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 in C++ by Damon
• 3,580 points
18 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 in C++ by Nicholas
• 2,460 points
21 views
0 votes
0 answers

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

The code below shows how to call a function in both methods.  Please explain the major differences or meanings of call by value and call by reference to me.  1.Make a value-based call.  2.Call based on a reference.  The call by value method is demonstrated in the following code. In a comment, I expressed my reservations. #include<iostream> int main(){ void change(int);//why function prototype is before ...READ MORE

Jun 6 in C++ by Nicholas
• 2,460 points
28 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21 in C++ by Damon
• 3,580 points
14 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,350 points
357 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,120 points
383 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,080 points
190 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,080 points
1,372 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 in C++ by Damon
• 3,580 points
20 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 in C++ by Damon
• 3,580 points
16 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP