Sunday, January 8, 2012

Multiple Inheritance − Sample Custom String class in C++

C++ is one of the most powerful language and will be used for a long time in the future inspite of emergen
of Java. C++ runs extremely fast and is in fact 10 to 20 times FASTER than Java. Java runs very slow
because it is a byte−code−interpreted language running on top of "virtual machine". Java runs faster with
(Just−In−Time) compiler, but it is still slower than C++. And optimized C++ program is about 3 to 4 tim
faster than Java (with JIT compiler). Then, why do people use Java? Because it is pure object oriented and
easier to program in Java, as Java automates memory management, and programmers do not directly deal
with memory allocations. This document attempts to automate the memory management in C++ to make i
much more easy to use. The library given here will make C++ look like Java and will enable "C++" to
compete with Java language.
Because of manual memory allocations, debugging the C++ programs consumes a major portion of time.
This document will give you some better ideas and tips to reduce the debugging time.

Language choice is very difficult. There are too many parameters − people, people skills, cost, tools, politics
(even national politics) and influence of businessmen/commercial companies. The best language based on
technical merits does not get selected simply due to political decisions!
Java is much closer to Ada95 than C++. Java is derived from Ada95. Ada95 gets the maximum points as per
David Wheeler's Ada comparison chart. Ada got 93%, Java 72%, C++ 68% and C got 53%. C++ and Java are
closer in points(only 4% difference), hence Java is not a very big revolution as compared to C++. On other
hand, Ada is a very big revolution and improvement over C++. The scores are like 4 students taking exams
and student with highest score is Ada (93%). Who knows? Perhaps in future Ada95 will replace Java!!
Development costs of Ada is half of C++ as per Stephen F. Zeigler. Ada95 is available at −
Ada home http://www.gnuada.org. •
Google Ada index •
Since C++ programmers are abundant, it is recommended you do programming in object−oriented "C++" for
all your application programming or general purpose programming. You can take full advantage of object
oriented facilities of C++. The C++ compiler is lot more complex than "C" compiler and C++ programs may
run bit slower than "C" programs. But speed difference between "C" and "C++" is very minute − it could be
few milli−seconds which may have little impact for real−time programming. Since computer hardware is
becoming cheaper and faster and memory 'RAM' is getting faster and cheaper, it is worth doing code in C++
rather than "C" as time saved in clarity and re−usability of C++ code offsets the slow speed. Compiler
optimizer options like −O or −O3 can speed up C++/C which is not available in Java.
Nowadays, "C" language is primarily used for "systems programming" to develop operating systems, device
drivers etc..
Running Codes
http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-3.html



#ifndef __STRING_MULTI_H_ALDEV_
#define __STRING_MULTI_H_ALDEV_

#include
#include "String.h"
#include "StringBuffer.h"

#ifdef NOT_MSWINDOWS
#else
using namespace std; // required for MS Visual C++ compiler Version 6.0
#endif

// Important Notes: In C++ the constructors, destructors and copy
// operator are NOT inherited by the derived classes!!
// Hence, if the operators like =, + etc.. are defined in
// base class and those operators use the base class's contructors
// then you MUST define equivalent constructors in the derived
// class. See the sample given below where constructors mystring(),
// mystring(char[]) are defined.
//
// Also when you use operator as in atmpstr + mstr, what you are really
// calling is atmpstr.operator+(mstr). The atmpstr is declared a mystring

class mystring:public String, string
{
public:
mystring():String() {} // These are needed for operator=, +
mystring(char bb[]):String(bb) {} // These are needed for operator=, +
mystring(char bb[], int start, int slength):String(bb, start, slength) {}
mystring(int bb):String(bb) {} // needed by operator+
mystring(unsigned long bb):String(bb) {} // needed by operator+
mystring(long bb):String(bb) {} // needed by operator+
mystring(float bb):String(bb) {} // needed by operator+
mystring(double bb):String(bb) {} // needed by operator+
mystring(const String & rhs):String(rhs) {} // Copy Constructor needed by operator+
mystring(StringBuffer sb):String(sb) {} // Java compatibility
mystring(int bb, bool dummy):String(bb, dummy) {} // for StringBuffer class

int mystraa; // customizations of mystring
private:
int mystrbb; // customizations of mystring
};

#endif // __STRING_MULTI_H_ALDEV_

No comments:

Post a Comment