473,413 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Use of abstract classes

Can anyone please tell me the applications of an abstract class( a class which contains atleast one pure virtual function)?
Sep 4 '07 #1
4 1250
kreagan
153 100+
Can anyone please tell me the applications of an abstract class( a class which contains atleast one pure virtual function)?
There are a lot of articles about abstract. I would suggest google.

If you have any confusions about what you read, or you want a different view, then come back.
Sep 4 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
First, a class with a virtual funxction is not an abstract class.

Second, an abstract class is a class that has at least one pure virtual function.

A pure virrtual function means that you cannot call the method with an object of that class. Therefore, the compiler won't let you create an object. Hence the class is abstract.

Read up on polymorphism and the meaning of object-oriented programming.
Sep 5 '07 #3
dmjpro
2,476 2GB
Can anyone please tell me the applications of an abstract class( a class which contains atleast one pure virtual function)?
Ok.
Let me explain.
Suppose you are implementing a class say A.
So the code will be:
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. virtual void test(){
  4. //some code
  5. }
  6. }
  7. //Now i am using ur class
  8. A a;
  9. a.test();//Here test method gets called.
  10.  
But in the mean time you think that your test method is incomplete and you don't want to add anything in that class then simply declare the class Abstract.
Your next Developer or may you will inherit it and there it will implementation.
Abstract means a class which is incomplete.

Kind regards,
Dmjpro.
Sep 5 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
But in the mean time you think that your test method is incomplete and you don't want to add anything in that class then simply declare the class Abstract.
Your next Developer or may you will inherit it and there it will implementation.
Abstract means a class which is incomplete.
You cannot declare a class abstract. What you can do is declare a class with a pure virtual function. That prevents the method from being called with an object of that class. This, in turn, cause the compiler to prevent you from creating objects of that class. Mayvbe that's what you meant by abstract. You cannot have an incomplete test() method in the base class and add to it in a derived class.

The derived class needs to override the bast class test method and thereby loses the code in the base class test method. To recover it you must code in the derived class:

[code=cpp]
Derived::test()
{
this->A::test(); //recover base class functionality
}
[/code}


This is bad design since it is not clear of the A::test() should be called before (a pre-condition) or called after ( a post-condition) the derived class test() logic. Now you have ambiguity. Ambiguity is a sign of bad design.

As a general rule: Do not derive from concrete classes.

More, class A has no virtual destructor. A class with no virtual destructor cannot be used as a base class having virtual functions. When you create a derived object, only the destructor of the derived object will be called. The base class destructor will never be called so the object is not properly cleaned up.

Even more, virtual functions should not be public. They declare how the class is implemented. The functions of the base class declare the interface to the hierarchy. By making then virtual, you destroy the interface in favor of the implementation. Worse, it forces all derived classes tio implement methods they may not be able to support.

The base class public methods are not to be virtual. Instead, they can call base class private virtual methods and it is these that the derived class overrides. This separates the interface from the implmentation.

Check out the design pattern called Template Method for more information.
Sep 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.