Thomas Kramer

IT-COW | All posts tagged 'C-Aufgaben'

C++-Aufgaben - Konstruktoraufrufe

By Administrator at Mai 25, 2011 14:14
Filed Under: C(++), Programmierung allgemein, Studium

Eine weitere Aufgabe war das Nachvollziehen der Reihenfolge von Konstruktoraufrufen. Nachfolgend mein Quellcode dazu.

 

/*
 * =====================================================================================
 *
 *       Filename: 
 *    Description:
 *
 *        Version:
 *        Created:
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Thomas Kramer
 *        Company:
 *
 * =====================================================================================
 */


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#include <iostream>
#include <iomanip>  // für setw()
#include <string.h>

using namespace std;

class Class
{
  public:
    Class ( int val=0 )
    {
      x=val;

      cout << " ====      constructor (" << this << ")\n";
    };
    Class ( const Class &other )
    {
      x=other.x;

      cout << " ====      copyconstructor (" << this << ")\n";
    };
    ~Class ()
    {
      cout << " ====      destructor (" << this << ")\n";
    };

    Class& operator = ( const Class &other );
    Class operator + ( const Class &other );

    void print (void);

    Class& set (int val);
  private:
    int x;
};

Class& Class::operator = ( const Class &other )
{
  x=other.x;

  cout << " ====      operator = (" << this << ")\n";

  return *this;
}

Class Class::operator + ( const Class &other )
{
  x+=other.x;

  cout << " ====      operator + (" << this << ")\n";

  return *this;
}

void Class::print(void)
{
  cout << " Class-Object : x = " << setw(9) << x << '\n';
}

Class& Class::set(int val)
{
  x=val;
}

Class& f (Class& arg);

int main(int argc, char *argv[])
{
  cout << "\n ######### Class x1(5)               ######### \n\n"  ;
  Class x1(5);
  x1.print();
  cout << "\n ######### Class x2=x1               ######### \n\n"  ;
  Class x2=x1;
  x2.print();
  cout << "\n ######### x2=x1                     ######### \n\n"  ;
  x2=x1;
  x2.print();
  cout << "\n ######### Class x3=23               ######### \n\n"  ;
  Class x3=23;
  x3.print();

  cout << "\n ######### x3=x1                     ######### \n\n"  ;
  x3=x1;
  x3.print();
  cout << "\n ######### x3=5                      ######### \n\n"  ;
  x3=5;
  x3.print();

  cout << "\n ######### x3=x1+4                   ######### \n\n"  ;
  x3=x1+4;
  x3.print();
  cout << "\n ######### x3=x1+x2                  ######### \n\n"  ;
  x3=x1+x2;
  x3.print();

  cout << "\n ######### x3=f(x2)                  ######### \n\n"  ;
  x3=f(x2);
  x3.print();
  cout << "\n ######### x3=x1+f(x2)               ######### \n\n"  ;
  x3=x1+f(x2);
  x3.print();

  cout << "\n ######### x3=234;                   ######### \n\n"  ;
  x3=234;
  x3.print();
  cout << "\n ######### x3.set(333);              ######### \n\n"  ;
  x3.set(333);
  x3.print();

  cout << "\n ######### ENDE                      ######### \n\n"  ;

  return 0;
}

Class& f (Class& arg)
{
  return arg;
}

 

Monats-Liste