Hej witajcie :)
Mam problem gdyz musze w moim programie zastosowac operator przypisania i konstruktor kopiujacy do obiektu dynamicznego karta nie mam pojecia jak to zrobic. Prosze o pomoc :)pierwsza klasa to glowna Komputer
#include <iostream>
#include <cstdio>
#include <cstring>
#include "procesor.cpp"
#include "Karta.cpp"
using namespace std;
typedef int TypDanychT;
class Komputer{
private:
string wykonawca;
string producent;
Procesor procesor;
static int licznik;
int dysk;
Karta *karta;
int ram;
public:
Komputer(int x, int y){
this->x = x; this-> y = y;
}
static int zwrocLicznik() { return licznik; }
int x,y;
Komputer () {
karta = NULL;
//cout << "konstruktor" << endl;
x=3;
y=5;
dysk=500;
ram=2000;
++licznik;
#ifdef TESTPR
cout << "Dziala konstruktor domyslny Komputer()" << endl;
#endif
}
void dodajkarta() {
if( karta==NULL)
karta= new Karta();
cout << "dodana karta" << endl;
}
void usunkarta() {
if(karta)
delete karta;
cout << "usunieto karta"<< endl;
}
void infokomp(){
cout<< "dysk ma pojemnosc " << dysk<< endl;
cout << "pamiec ram wynosci " << ram << endl;
if (karta)
karta->wyswietlinfo();
procesor.infoprocesor();
}
Komputer (const Komputer &o) {
#ifdef TESTPR
cout << "Dziala konstruktor kopiujacy Komputer()" << endl;
#endif
producent=o.producent;
wykonawca=o.wykonawca;
procesor=o.procesor;
dysk=o.dysk;
cout << "konstruktor kopiujacy sie wywolal " << endl;
cout << "pojemnosc dysku komputera wynosi " << dysk << endl;
}
~Komputer () {
--licznik;
#ifdef TESTPR
cout << "Dziala desturktor ~Komputer()" << endl;
#endif
}
bool operator==(const Komputer &k){ return producent == k.producent; }
void setProducent(string producent){ this->producent = producent; }
Komputer& operator=(const Komputer &p){
cout << "operator przypisania" << endl;
dysk=p.dysk;
ram=p.ram;
return *this;
}
Komputer& operator+(Komputer &m){
cout << "operator + " << endl;
Komputer temp;
temp.x = x+m.x;
temp.y = y+m.y;
return temp;
}
};
Teraz klasa Karta jako obiekt dynamiczny
#include <iostream>
using namespace std;
class Karta
{
private:
string nazwa;
int pamiec;
public:
Karta() :nazwa("Nvidia"),pamiec(2000){
//cout << "Karta " << endl;
#ifdef TESTPR
cout << "Dziala konstruktor dynamiczny Karta()" << endl;
#endif
}
~Karta(){
#ifdef TESTPR
cout << "Dziala destruktor ~Karta()" << endl;
#endif
}
void wyswietlinfo(){
cout << "nazwa karty" << nazwa << endl;
cout << " pamiec karty" << pamiec << endl;
}
};
Program glowny
#include "komputer.cpp"
using namespace std;
int Komputer::licznik = 0;
int main(){
//Karta *karta;
//karta = new Karta();
cout << "Jest " << Komputer::zwrocLicznik() << " komputerow" << endl;
Komputer k1;
cout << "Jest " << Komputer::zwrocLicznik() << " komputer" << endl;
Komputer k2;
cout << "Sa " << Komputer::zwrocLicznik() << " komputery" << endl;
Komputer *k3;
cout << "Jestesmy po deklaracjach/definicjach" << endl;
k3 = new Komputer();
cout << "Sa " << Komputer::zwrocLicznik() << " komputery" << endl;
cout << (k1 == k2) << endl;
cout << "procesory sa takie same wiec zwrocilo 1" << endl;
k2.setProducent("Intel");
cout << (k1.operator==(k2)) << endl;
cout << "procesory nie sa takie same wiec zwrocilo 0" << endl;
cout << "Jest " << Komputer::zwrocLicznik() << " komputerow" << endl;
delete k3;
cout << "Jest " << Komputer::zwrocLicznik() << " komputerow bo usunelam jeden hah" << endl;
Komputer k5;
cout << "Jest " << Komputer::zwrocLicznik() << " komputerow" << endl;
Komputer k6(k5); // konstruktor kopiujacy
Komputer k7;
Komputer k8;
k7 = k8;
k7=k2+k1;
k2.dodajkarta();
k2.usunkarta();
k1.infokomp();
return 0;
}
klasa procesor
#include <iostream>
#include <cstring>
using namespace std;
class Procesor{
private:
string marka;
int czestotliwosc;
public:
Procesor():marka("Radeon"),czestotliwosc(1000){
#ifdef TESTPR
cout << "Dziala konstruktor domyslny Procesor()" << endl;
#endif
}
~Procesor(){
#ifdef TESTPR
cout << "Dziala destruktor ~Procesor()" << endl;
#endif
}
void infoprocesor(){
cout << "marka procesora " << marka << endl;
cout << " czestotliwosc procesora : " << czestotliwosc << endl;
}
};