C-plus-plus: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
Zeile 5: Zeile 5:


= Beispiel Klasse Complex =
= Beispiel Klasse Complex =
<pre>class Complex
<syntaxhighlight lang="c++">
class Complex
{
{
public:
public:
Zeile 20: Zeile 21:
     }
     }
};
};
 
</syntaxhighlight>
</pre>
= Exception =
<syntaxhighlight lang="c++">
class ReException {
public:
        ReException(const char* message);
        ReException(const char* message, const char* file, int line);
        virtual ~ReException();
public:
        ReException(const ReException& source);
        ReException& operator =(const ReException& source);
protected:
        ReException();
public:
        /** @brief Sets the message.
        * @param message The new message.
        */
        inline void setMessage(const char* message) {
                if (m_message != NULL)
                        free((void *) m_message);
                m_message = _strdup(message);
        }
        /** @brief Returns the message.
        * @returns The description of the exception.
        */
        inline const char* getMessage() const {
                return m_message;
        }
protected:
        const char* m_message;
};
</syntaxhighlight>

Version vom 5. Dezember 2022, 18:44 Uhr


Links

Beispiel Klasse Complex

class Complex
{
public:
    double _real;
    double _imag;
public:
    Complex(): _real(0), _imag(0) {}
    Complex(double real, double imag):  _real(real), _imag(imag) {}
    Complex(const Complex &src):  _real(src._real), _imag(src._imag) {}
    Complex& operator =(const Complex& source){
        _real = source._real;
        _imag = source._imag;
        return *this;
    }
};

Exception

class ReException {
public:
        ReException(const char* message);
        ReException(const char* message, const char* file, int line);
        virtual ~ReException();
public:
        ReException(const ReException& source);
        ReException& operator =(const ReException& source);
protected:
        ReException();
public:
        /** @brief Sets the message.
         * @param message The new message.
         */
        inline void setMessage(const char* message) {
                if (m_message != NULL)
                        free((void *) m_message);
                m_message = _strdup(message);
        }
        /** @brief Returns the message.
         * @returns The description of the exception.
         */
        inline const char* getMessage() const {
                return m_message;
        }
protected:
        const char* m_message;
};