Einzelnen Beitrag anzeigen
Ungelesen 30.11.15, 15:58   #3
serking
Anfänger
 
Registriert seit: Apr 2012
Beiträge: 11
Bedankt: 0
serking ist noch neu hier! | 0 Respekt Punkte
Standard

Code:
#include <iostream>

#include <string>
using namespace std;



class Computer{
    
    private :
    
    char* op_name;
    char* hardwarename;
    
    
    public :
    
    Computer();
    
    Computer(const char* op_name,const char* hardwarename);
    
    Computer(const Computer& anothercomputer);
    
    Computer& operator=(const Computer& other);
    
    void print();
    
};



Computer::Computer(){
  
    op_name=NULL;
    hardwarename=NULL;
    
};

Computer::Computer(const char* op_name,const char* hardwarename){
    
    /*

    if(op_name!=NULL&&hardwarename!=NULL){
        
        delete(this->op_name);
        delete(this->hardwarename);
        
        this->op_name=strdup(op_name);
        this->hardwarename=strdup(hardwarename);
        
    }*/
    
    
    
    int len1=strlen(op_name);
    int len2=strlen(hardwarename);
    
    this->op_name=new char[len1+1];
    this->hardwarename=new char[len2+1];
    
    strcpy(this->hardwarename,hardwarename);
    strcpy(this->op_name,op_name);
    
    
    
    
};


Computer::Computer(const Computer& anothercomputer){
    
    
    int len1=strlen(anothercomputer.op_name);
    int len2=strlen(anothercomputer.hardwarename);
    
    this->op_name=new char[len1+1];
    this->hardwarename=new char[len2+1];
    
    strcpy(this->hardwarename,anothercomputer.hardwarename);
    strcpy(this->op_name,anothercomputer.op_name);
};


Computer& Computer::operator=(const Computer& other){
    
    int len1=strlen(other.op_name);
    int len2=strlen(other.hardwarename);
    
    this->op_name=new char[len1+1];
    this->hardwarename=new char[len2+1];
    
    strcpy(this->hardwarename,other.hardwarename);
    strcpy(this->op_name,other.op_name);
    
    
    return *this;
    
};


void Computer::print(){
    
    cout<<"op_name : "<<op_name<<" hardwarename : "<<hardwarename<<endl;
}


int main(){
    
    
    Computer serotik("Mac","Serotik");

    serotik.print();
    
    Computer enis=serotik;
    
    enis.print();
    
    
    
}
serking ist offline   Mit Zitat antworten