473,394 Members | 1,841 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,394 software developers and data experts.

Constants in base classes

Hi All,
I have two classes that have the same functionality, but with
different constants, e.g. database table name.
I've defined a base class that gets the constants in the constructor,
and maintain the functionality. It works fine, but not very
efficiently.
Here's an example:
class base {
public:
base (string &const1, string @const2) {s1 = const1; s2 = const2;}
private:
string s1, s2;
};
class kid1 : public base {
public:
kid1 () {base ("Child 1", "Table 1");}
};
class kid2 : public base {
public:
kid2 () {base ("Child 2", "Table 2");}
};

My problem is that each object has all the constants as variables.
Is there any way to improve the efficiency?
Thanks a lot in advance,
Omer.
Nov 18 '08 #1
2 1800
On Nov 18, 12:26*pm, Omer <omer...@yahoo.comwrote:
Hi All,
I have two classes that have the same functionality, but with
different constants, e.g. database table name.
I've defined a base class that gets the constants in the constructor,
and maintain the functionality. It works fine, but not very
efficiently.
Here's an example:
class base {
public:
* * base (string &const1, string @const2) {s1 = const1; s2 = const2;}
@ is not a valid member of C++ character set.
private:
* * string s1, s2;};

class kid1 : public base {
public:
* * kid1 () {base ("Child 1", "Table 1");}};

class kid2 : public base {
public:
* * kid2 () {base ("Child 2", "Table 2");}

};

My problem is that each object has all the constants as variables.
Is there any way to improve the efficiency?
Thanks a lot in advance,
Omer.

Before improving efficiency, you should do about the correctness of
your program. Correctness is far important than efficiency. Your code
can't be compiled. You have to use initialization list for
constructing base class subobjects in derived classes objects.

After that, for efficiency improvement, you should think about the
role of base class. I think the base class is redundant.

Regards,
Saeed Amrollahi
Nov 18 '08 #2
On 18 nov, 07:26, Omer <omer...@yahoo.comwrote:
Hi All,
I have two classes that have the same functionality, but with
different constants, e.g. database table name.
I've defined a base class that gets the constants in the constructor,
and maintain the functionality. It works fine, but not very
efficiently.
Here's an example:
class base {
public:
* * base (string &const1, string @const2) {s1 = const1; s2 = const2;}
private:
* * string s1, s2;};

class kid1 : public base {
public:
* * kid1 () {base ("Child 1", "Table 1");}};

class kid2 : public base {
public:
* * kid2 () {base ("Child 2", "Table 2");}

};

My problem is that each object has all the constants as variables.
Is there any way to improve the efficiency?
What exactly do you mean? Those strings belong to the base class part
of derived classes objects. Can you define them to be protected
instead of private? By the way, you should use the initialization list
in the constructor:

class kid2 : public base {
public:
kid2() : base("Child 2", "Table 2") {}
};

In this case it's probably a good idea for the base class constructor
to declare parameters by const-ref. (Again, use the initialization
list.)

class base {
public:
base (string const& const1, string const& const2) : s1(const1), s2
(const2) {}
protected:
string s1, s2;
};

Notice that you don't need the semi-colon (;) at the closing braces of
constructors bodies, function bodies or namespaces.
--
Leandro T. C. Melo
Nov 18 '08 #3

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

Similar topics

1
by: Vikas | last post by:
Hi y'all, I have an existing GUI application where various windows has columns which are mapped to data members of various classes. Now, I have to implement a new functionality to all the...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
11
by: Bill Nguyen | last post by:
I need to make a set of constants to be available for the whole application. Public const is confined to the form from which constants are declared. Is there a way to declare once and all forms can...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
3
by: boris | last post by:
Hi. If somebody can help out with this design question, thank you very much. Suppose I have a global constant, like an images directory. This constant will be referenced from multiple classes. How...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.