What type of members can I add in a c abstract class

0 votes

Hello, let's say I have an abstract class with a few pure abstract functions and a few classes that derive from it, and all of the data from these classes eventually becomes similar, I was wondering if it would be wise, or even possible, to declare a vector under protected in the abstract class to collect the data so something like that.

class A
{
protected:
    vector <string> str;
public:
    virtual function x();
    virtual function y(); //etc etc
    virtual ~A(){;}
};

class B : public A
{
public:
    function x();
    function y();
};

class C: public A
{
    //similar to class B
} ;

I'm having trouble understanding how pure virtual functions operate in C++; I understand polymorphism because I've done a lot of java projects with it. 

Is it even feasible to define and utilise that vector in C++ because abstract classes cannot be instantiated?

Jul 27 in C++ by Nicholas
• 5,020 points
10 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

In C++, what is a virtual base class?

When employing multiple inheritance, virtual base classes are used to prevent several "instances" of a particular class from appearing in an inheritance hierarchy. Consider the following example: class A { public: void Foo() {} ...READ MORE

answered Jun 10 in C++ by Damon
• 4,960 points
24 views
0 votes
1 answer

What is a Class and Object in C++?

A Class is like a blueprint, an ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
21 views
0 votes
1 answer

What range of values can integer types store in C++

You may rely on the following minimal ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
20 views
0 votes
0 answers

How can I get the size of a C++ function?

In C++, how can I find the size& ...READ MORE

Aug 5 in C++ by Nicholas
• 5,020 points
13 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
• 4,960 points
31 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 4,960 points
47 views
0 votes
1 answer

Why do we need virtual functions in C++?

A virtual function is a base class member function that we expect to redefine in derived classes. In the base class, a virtual function is used to ensure that the function is overridden.  This is especially true when a pointer from a base class points to an object from a derived class. For example, consider the code below: class Base ...READ MORE

answered May 27 in Others by Damon
• 4,960 points
24 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7 in C++ by Damon
• 4,960 points
23 views
0 votes
1 answer

When to use virtual destructors?

When you want to delete an instance of a derived class using a pointer to the base class, virtual destructors come in handy: class Base { // ...READ MORE

answered Jun 7 in C++ by Damon
• 4,960 points
32 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
31 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