C Object class

0 votes
There is a generic class in Java called "Object" that all classes are subclasses of. I'm trying to make a linked list library (for a school project), and I've only gotten it to work for one type, not multiple, so is there anything similar?

EDIT: I'd post the code, but I don't have it with me right now.
Jun 2, 2022 in C++ by Nicholas
• 7,760 points
499 views

1 answer to this question.

0 votes

No, there is no generic base class in C++.

You can create your own and derive classes from it, but in order to take advantage of polymorphism, you must keep collections of pointers (or smart pointers).

 After re-examining your question, I must mention std::list.

If you want a list that can specialize on multiple types, you use templates (of which std::list is one):

std::list<classA> a;
std::list<classB> b;

If you need a list that can hold multiple types in a single instance, use the base class approach:

std::list<Base*> x;
answered Jun 2, 2022 by Damon
• 4,960 points

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, 2022 in C++ by Damon
• 4,960 points
519 views
0 votes
0 answers

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

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> ...READ MORE

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

Container Class vs Class - C++

I'm new to programming and have only ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
358 views
0 votes
1 answer

What is dynamic initialization of object in c++?

Dynamic initialization occurs when the initialization value is unknown at compile time.  To initialise the variable, it is calculated at runtime. Example, int factorial(int n) { ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
2,088 views
0 votes
1 answer

The difference between Classes, Objects, and Instances

A class is a type of blueprint ...READ MORE

answered Jun 7, 2022 in Java by pranav
• 2,590 points
8,025 views
0 votes
1 answer

Python class inherits object

Python 3.x: class MyClass(object): = new-style class class MyClass: = new-style ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,020 points
805 views
0 votes
1 answer

What are the prerequisites to learn Hadoop in java perspective?

In my day job, I've just spent ...READ MORE

answered Oct 11, 2018 in Big Data Hadoop by Frankie
• 9,830 points
784 views
0 votes
1 answer

How can we compare objects by multiple fields in Java?

You can implement a Comparator which compares two Person objects, and ...READ MORE

answered Jan 9, 2019 in Java by Daisy
• 8,140 points
4,144 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, 2022 in C++ by Damon
• 4,960 points
497 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
566 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