473,791 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New in C++:Abstract Data Types(ADT) - Base Class

Hi all,

I would like to help with the following:

// my conventions:
// BC = Base Class
// DC = Derived Class

class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}

class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }

dcFunc() { // Do something }
}

int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;

myBC->bcFunc1();
myBC->bcFunc2();

// now the tricky part - the following line will not compile
// myBC->dcFunc();

// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC *> (myBC);

// now I can call the DC function
myDC->dcFunc();

delete myBC;
delete myDC;
}

I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?
Thanx in Advance,
Sun
Jul 22 '05 #1
2 2762

"SunScreen" <al*****@yahoo. com> wrote in message
news:b3******** *************** *@posting.googl e.com...
Hi all,

I would like to help with the following:

// my conventions:
// BC = Base Class
// DC = Derived Class

class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}

class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }

dcFunc() { // Do something }
}

int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;

myBC->bcFunc1();
myBC->bcFunc2();

// now the tricky part - the following line will not compile
// myBC->dcFunc();

// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC *> (myBC);

// now I can call the DC function
myDC->dcFunc();

delete myBC;
delete myDC;
}

I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?


Well you could use static_cast, but that is equally 'bad programming code'.
These things are necessary sometimes but the reason they are considered bad
practice is because the need to call a dervied class member function though
a base class pointer when the member function is not present and virtual in
the base class, is normally considered bad design.

What need do you have to do this? I'm not saying that you shouldn't, I'm
just saying that you should think carefully about it. It may be that you can
avoid this by redesigning your code.

john
Jul 22 '05 #2

"SunScreen" <al*****@yahoo. com> wrote in message
news:b3******** *************** *@posting.googl e.com...
Hi all,

I would like to help with the following:

// my conventions:
// BC = Base Class
// DC = Derived Class

class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}

class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }

dcFunc() { // Do something }
}

int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;

myBC->bcFunc1();
myBC->bcFunc2();

// now the tricky part - the following line will not compile
// myBC->dcFunc();

// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC *> (myBC);

// now I can call the DC function
myDC->dcFunc();

delete myBC;
delete myDC;
}

I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?
Thanx in Advance,
Sun


Yes, a couple of ways. For one, you could make a virtual function dcFunc()
in the base class that does nothing (or is abstract virtual). Or, you could
make a new virtual function in both classes, then have it do nothing (or be
abstract virtual) in the base class but have it call dcFunc() in the DC
class. Either way, you use a virtual function in the base class in order to
call its overridden function in the derived class.

-Howard

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
21726
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA. Dev
4
3555
by: Chuck Bowling | last post by:
I am using CodeDOM to generate source files. The classes being generated have a const string member. This member is referenced in an abstract base class but declared in the inheriting class. I can't declare the string in the abstract class because it would require initialization. Is there a way to require that any class inheriting from the base class contain this string without a specific declaration in the base class? I tried putting a...
5
2097
by: aa7im | last post by:
I am attempting to create a base collection that I can use to derive strongly typed collection. My base collection class will derive from CollectionBase and I want my typed collections to be forced to implement certain methods: public abstract class MyCollectionBase: CollectionBase { public void Sort() {.... SORT IMPLEMENTATION } public void Filter() {...> FILTER IMPLEMENTATION }
9
2427
by: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for example unmanagedObject). How can i convert this to the managed abstract basetype? Hope somebody can help me Thanx
9
5200
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
1
2232
by: D Witherspoon | last post by:
Coming up with a scenario here. For example there is the standard .NET MailMessage class. I am creating a project (let's call it CommonBase) that has the following 2 classes EmailMessage_Base ( inherits System.Net.Mail.MailMessage and provides additional methods and properties) EmailMessage_Abstract ( inherits EmailMessage_Base and adds some business logic including what default return addresses are and
0
2837
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 magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
2
1897
by: Dom Jackson | last post by:
Hello - I have a problem where I need to test some numeric code using a variety of built-in integer types: obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct? If I test with 10 built-in integer types, then I get 1000 permutations of the above statement. If I then test a dozen different operators, I get over 10,000 test operations.
5
1521
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract Class. It seems to work pretty well. I have the actual data memory variables stored as an object as each class will use it as a different type of object (not sure if this is a problem). The system can tell if the variable is a string, boolean or...
20
4044
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.