Class Problem

+1 Deni C++ · January 22, 2015
Hi, I have a "little" problem in my program, so if somebody have time to see it, i would appreciate it. Thanks :)


#include <iostream>
using namespace std;

class Person {
    private:
        char* name;
        int count1;
        int count2;

    public:
        Person(const char* a) {
            cout << "Constructor!" << endl;
            count1=0;
            count2=0;
            name = new char[80];

            for(int i=0; name[80]; i++) {
                name = '\0';
            }


            for(int i=0; a; i++) {
                count1++;
                if(count1 > 80) {
                    throw "Error!";
                }
            //a[count1] = '\0';         //Damit funktioniert es nicht
            //a[count1+1] = '\0';
            }


            for(int i=0; name != '\0'; i++) {
                count2++;
                if(count2 > 80) {
                    throw "Error!";
                }
            //name[count2] = '\0';      //Damit funktioniert es nicht
            //name[count2+1] = '\0';
            }

            for(int i=0; a != '\0'; i++) {
                name = a;
            }
        }

        Person(const Person &obj) {
            cout << "Copy Constructor! " << endl;
            name = new char[80];
            for(int i=0; name != '\0'; i++) {
                name = obj.name;  //*(name+i) = *(obj.name+i) ist gleich
            }
        }

         ~Person() {
            cout << "Free!" << endl;
            delete[] name;
        }

        void taufen(const char* a) {
            cout << "Taufen!" << endl;
            for(int i=0; a != '\0'; i++) {
                *(name+i) = *(a+i);     //name = a ist gleich
            }
        }

        void print() const {
            cout << "Print!" << endl;
            cout << "Name: ";
            for(int i=0; name != '\0'; i++) {
            cout << name;
            }
            cout << endl;
        }

        int strlen(const char* text) {      //Die Berechnung der Länge des Strings
            int size = 0;
            while (text)
                size++;
            return size;
        }

        Person operator+ (const Person& obj) const {
            Person help;
            int i;
            int j;
            int len1 = strlen(name);
            int len2 = strlen(obj.name);

            help = new char[len1+len2+1];

            for(i=0; i<len1; i++) {
                *(help.name+i) = *(name+i);
            }
            
            for(j=0; j<len2; j++, i++) {
                *(help.name+i) = *(obj.name+j);
            }

            return help;
        }
};



int main () {
            //Objekten
    try {
        Person ob1("Carl");
        ob1.print(); cout << endl;

        ob1.taufen("Carlos");
        ob1.print(); cout << endl;

        Person ob2(ob1);
        ob1.print(); cout << endl;

        ob2.taufen("Writing");
        ob2.print(); cout << endl;
        ob1.print(); cout << endl;

        Person ob3 = ob2;
        ob2.print(); cout << endl;

        int size = ob1.strlen("Programming");
        cout << size << endl;

        
        Person ob4 = ob1 + ob2;
        ob4.print(); cout << endl;

    }
    catch(const char* error) {
        cout << error<< endl;
    }

        return 0;
}

by the way,  I study on German, and because of that are my comments on German. :)

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jake Scaife · January 23, 2015
I ran your code and it threw a few errors, I would recommend downloading an IDE such as Codeblocks or Qt. These will let you know if there are any errors in your code when you try to compile.
0 Deni C++ · January 23, 2015
I use Sublime Text Editor, and I saw that there missing few sings, I'm gonna upload it again. But thx any way :)
  • 1

C++

107,289 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator