Question
Asked 11th Feb, 2020

Questions in Object oriented programming C++?

Dear all,
I have some questions in C++ which I could not get clear answers through internet. It would be very helpful if you could help with it.
1. How to instantiate a non-pure abstract class ( only one of the data members is made pure virtual) in the main(). In internet it is mentioned that, it is possible to create my own constructor to initialize the property members and to use the non-virtual methods. is it possible?. or should the non-pure virtual members can be made static?
2. Some say, that it is even possible to create a non-pure abstract class just my making the destructor as pure virtual, it is possible?
3. What is the actual useof pure virtual destructor in the program actually?
4. Is it possible to use the auto type as return value in case of abstract function? Is it possible to know the type of data carried by auto variable?
5. why sometimes the constructor is made protected, is it liberately done to prevent direct instantiation? what are all the use cases of such practice?
6.If I call a virtual function in the derived class, it takes the default value of that function from base class always, how to override it? why it happens?
7. In multiple inheritance, why I make a class virtual to avoid multiple copies of a same base class, it does not work?
8. Interestingly, if I call the grandchildren class (in multiple inheritance)
class A(contains virtual members) : class B ( non virtual members)
class B : class C ( non virtual members)
class C: class D ( non virtual members)
9. If there is a non-pure abstract class( only one of the members is pure virutal), can I again define it as pure virtual if there is no body/definition literally available in one of the derived class, instead of defining it just for the sake of abstract class instantiation?
if I access the class D object, using the class B/C type pointer, provided that the base class A member function made virtual, the members of the class D is accessed instead of class B. Can I understand that any base class member irrespective of what you use becomes virtual even if one of the base class instance of that member is made virtual?
Hope my questions were clear. Would be glad to provide an example code if required.
Thank you and I look forward to hearing from you.

Most recent answer

25th Feb, 2020
Raam Kasinathan
Anna University of Technology, Chennai
Denise Skidmore
Judicael A. Zounmevo Thank you so much. I am beginner in C++ and it is a completely new topic. Some would have been silly, sorry for that. But I have now understood the things clearly. Thank you for such a concise explanations

All Answers (7)

11th Feb, 2020
Anton Vrdoljak
University of Mostar
I recommend to ask this Q at: https://stackoverflow.com/
2 Recommendations
12th Feb, 2020
Charles Wilson
Motional
These are fundamental questions. They would be better answered by a review of Bjarne's C++ book.
1 Recommendation
12th Feb, 2020
Raam Kasinathan
Anna University of Technology, Chennai
thank you
13th Feb, 2020
Judicael A. Zounmevo
N/A
Consider the code snippet below for the sake of the discussion
<code_start>
#include <iostream>
#define __TRACE_FUNC(func_name, line) \
do { \
std::cout<<"Invoking "<<func_name<<" at line "<<line<<std::endl;\
} while(false)
#define TRACE_FUNC() __TRACE_FUNC(__func__, __LINE__)
class A
{
public:
virtual void func() const = 0;
int get_zero() const { return 0; }
virtual ~A() { TRACE_FUNC(); } // Destructor SHOULD be virtual if I want
//to inherit from A (and use polymorphism)
};
class B final: public A
{
public:
void func() const override { TRACE_FUNC(); }
~B() { TRACE_FUNC(); }
};
int main(int argc, char** argv)
{
#if 0
A a; // This will not compile because you cannot instantiate an abstract class
// even if it is not pure abstract.
#endif
A* pointer = new B;
pointer->func();
delete pointer;
pointer = nullptr;
return 0;
}
//If using g++, you can compile with:
//g++ -Wall -o a abstract.cpp
//
//With clang++, you can compile with
//clang++ -Wall -o a abstract.cpp
//
//With visual studio, create a C++ project, paste the code in a new C++
//source and visually compile.
//
//etc.
//
//But, if you're not familiar with the methods mentioned above,
//use an online compiler like one of these:
</code_end>
1-) You simply cannot instantiate an abstract class; it doesn't matter if only
one of its methods is pure virtual.
While planning polymorphic behaviors, you can use a pointer to an abstract
base class A, and instantiate a derived class B with it (if the derived class is
not abstract all; i.e., it provides non-pure virtual override of all the pure
virtual methods in the base class). Check example above.
The thing you read on Internet is wrong or very confusing
(or I'm simply misunderstanding it). The simple answer is "It is not possible"
2-) Yes. ANY method that is pure virtual makes the class abstract.
Don't try with constructors!
Pure virtual destructor is something I would do in a base class that I want
to make abstract and where there is no other obvious method to make pure virtual
(i.e., you see a reusable implementation for all the other methods).
3-) A destructor of a class meant to be derived from should be declared
virtual otherwise you can get undefined behaviors if you use polymorphism the
way I used it in the main class above.
Now, if your class is meant to be abstract (i.e., its destructor should be
virtual anyway (pure or not)), you might as well make your destructor pure
(especially if no other method stands out as candidate for being pure virtual).
So to answer your question, A pure virtual destructor is as good (not better or
not worse) as any other method to make your class abstract. Now, if there is no
reason to make any of your other methods abstract in the class, the destructor
is an excellent candidate (especially since it should be virtual anyway if your
class is meant to be a polymorphic base class).
4-) My general advice for this is: please write a code snippet yourself,
compile and see what the compiler tells you. You'll learn way more and grow
more efficiently by trying things yourself ... plus it is exciting!^_^
You can even try online (http://cpp.sh or
https://godbolt.org; there are other online compilers). I usually just open a
shell and try locally on my machine :)
5-) If you don't want the class to be instantiated (at least through the
specific constructor that is made protected); but you want derived classes to
invoke that protected constructor if needed (by their own constructors for
instance). When the use case shows up, you'll naturally feel it!
6-) Provide an override implementation in the derived class (e.g. func0 in
class B above)
7-) You can't make a class virtual in C++! I guess you're talking about virtual
inheritance. What exactly "does not work"?
8-) I struggle to see what the question is here, sorry.
... plus I think your inheritance tree is defined backward? ...
9-) By default, it remains pure virtual if it is not overridden in derived
classes.
That's polymorphism (for the second question).
Please google C++ polymorphism online and read about it to learn way more.
Good luck. Check this too: https://isocpp.org/faq
1 Recommendation
25th Feb, 2020
Raam Kasinathan
Anna University of Technology, Chennai
Denise Skidmore
Judicael A. Zounmevo Thank you so much. I am beginner in C++ and it is a completely new topic. Some would have been silly, sorry for that. But I have now understood the things clearly. Thank you for such a concise explanations

Similar questions and discussions

Related Publications

Chapter
This Exploration takes a break from C++ programming to turn to the topic of object-oriented programming (OOP). You may already be familiar with this topic, but I urge you to continue reading. You may learn something new. To everyone else, this Exploration introduces some of the foundations of OOP in general terms. Later Explorations will show how C...
Book
This book was written for readers those who are interested to learn C++ programming language with easy way and effective to enhance their knowledge of C++. It is the goal of our career beginning to design this text with the capabilities of text book and also reference book.The chapter one gives a thorough description of the basic characteristic of...
Chapter
This Exploration takes a break from C++ programming to turn to the topic of object-oriented programming (OOP). You may already be familiar with this topic, but I urge you to continue reading. You may learn something new. To everyone else, this Exploration introduces some of the foundations of OOP in general terms. Later Explorations will show how C...
Got a technical question?
Get high-quality answers from experts.