Skip to content

Instantly share code, notes, and snippets.

@dsapoetra
Last active May 13, 2016 00:05
Show Gist options
  • Save dsapoetra/5cbd045832f322ac25727a851432bef2 to your computer and use it in GitHub Desktop.
Save dsapoetra/5cbd045832f322ac25727a851432bef2 to your computer and use it in GitHub Desktop.
#include "A.h"
#include <iostream>
using namespace std;
A::A(){
cout<<"ctor default A"<<endl;
n = 0;
}
A::A(int nn){
cout<<"ctor param A"<<endl;
n = nn;
}
A::A(const A& a){
cout<<"cctor A"<<endl;
n = a.n;
}
A::~A(){
cout<<"dtor A"<<endl;
}
A& A::operator=(const A& a){
cout <<"opeator = a"<<endl;
this->n = a.n;
return *this;
}
A A::operator+(const A& a){
cout <<"opeator + a"<<endl;
A A1(this->n+a.n);
return A1;
}
A A::operator-(const A& a){
cout <<"opeator - a"<<endl;
A A1(this->n-a.n);
return A1;
}
int A::getN(){
return n;
}
#ifndef _A_H
#define _A_H
class A {
private:
int n;
public :
A();
A(int nn);
A(const A& a);
~A();
A& operator=(const A& a);
A operator+(const A& a);
A operator-(const A& a);
virtual int getN();
};
#endif
#include "B.h"
#include <iostream>
using namespace std;
B::B():A(){
cout<<"ctor default B"<<endl;
m = 0;
}
B::B(int mm,int nn):A(nn){
cout<<"ctor param B"<<endl;
m = mm;
}
B::B(const B& b):A(b){
cout<<"cctor B"<<endl;
m = b.m;
}
B::~B(){
cout<<"dtor B"<<endl;
}
B& B::operator=(const B& b){
cout <<"opeator = b"<<endl;
A a = b;
a.operator=(b);
this->m = b.m;
return *this;
}
B B::operator+(const B& b){
cout <<"opeator + b"<<endl;
A a = b;
a.operator+(b);
B B1(this->m+b.m,b.getN());
return B1;
}
B B::operator-(const B& b){
cout <<"opeator - b"<<endl;
A a = b;
a.operator-(b);
B B1(this->m-b.m,b.getN());
return B1;
}
int B::getN(){
return A::getN();
}
int main(){
A A1;
A A2(5);
A A3(8);
A A4 = A2;
A4 = A2 + A3 - A4;
return 0;
}
#ifndef _B_H
#define _B_H
#include "A.h"
class B:public A {
private:
int m;
public :
B();
B(int mm,int nn);
B(const B& b);
~B();
B& operator=(const B& b);
B operator+(const B& b);
B operator-(const B& b);
int getN();
};
#endif
@dsapoetra
Copy link
Author

ini :

B B::operator+(const B& b){
cout <<"opeator + b"<<endl;
//A a = b;
A::operator+(b);
B B1(this->m+b.m,A::n);
return B1;
}

Jalan, syaratnya member kelas A harus bukan private

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment